PyQt5 stubs files - python-3.x

I've just started a PyQt5 project that is currently running in a virtualenv.
PyQt5 was installed using a classic pip install pyqt5.
I want to type check my application using mypy.
But when I run it, I get an error telling me that there are no stubs file for PyQt5.
myapp/__main__.py:3: error: No library stub file for module 'PyQt5.QtWidgets
I've checked the site-package of my virtualenv, and indeed, there aren't any .pyi file in it.
Checking the documentation, I see that if compiled, stub files can be generated (and could at least exists beginning with PyQt5.6, I'm using 5.10).
Is there a way to obtain those file without the need to manually compile the library ?

I had the same problem with PyQt5. So I decided to put up PyQt5-stubs which contains the stub files for the main PyQt5 modules.
You can install it with pip:
$ pip install pyqt5-stubs
Another solution for you would be to change to Qt for Python (PySide2). They provide proper type annotations.

Not currently.
PEP 561, which specifies how packages should indicate they supply type information, was recently accepted. (Full disclosure, I am the author).
PyQt5 will need to become compliant with the PEP, and then as long as you are using mypy >=0.600, everything should work as expected.

Related

pycharm has no Qt platform plugin could be initialized error when using matplotlib

I installed my python as an independent program into my personal user folder on windows 10:
C:\Users\My.Name\AppData\Local\Programs\Python\Python39\
Then I installed all packages that I want from inside PyCharm 2021.2.3 community version via
File->Settings->Add package
It's been ok and I can run my numpy pandas data project etc. But when I add the matplotlib library and tried to do simple plot, it gave the no Qt platform plugin could be initialized error.
I searched for answers and proceed to install the following packages:
PyQt5
PyQt5-Qt5
PyQt5-sip
PyQt5-stubs
pyqt5-plugins
pyqt5-tools
qt5-applications
qt5-tools
Then I checked in these package's bin\ folders to ensure the 'platforms' folder exists and the qwindows.dll is present.
But matplotlib still fails with that no Qt platform error.
Found out that I have to put the PyQt5's lib path into my PATH environment variable like below:
First, go open the 'Edit Environment Variables for your account' dialog
Then, add the following 3 entries on TOP of the path list:
C:\Users\My.Name\AppData\Local\Programs\Python\Python39\Lib\site-packages\PyQt5\Qt5\bin
C:\Users\My.Name\AppData\Local\Programs\Python\Python39\Lib\site-packages\PyQt5\Qt5\plugins
C:\Users\My.Name\AppData\Local\Programs\Python\Python39\Lib\site-packages\PyQt5\Qt5\plugins\platforms
Also, if you have a variable named: QT_PLUGIN_PATH, you want to put this line at the TOP of the list:
C:\Users\My.Name\AppData\Local\Programs\Python\Python39\Lib\site-packages\PyQt5\Qt5\plugins
After saving your environment variables, you need to restart PyCharm to have it "find" these libraries.
Well, this got me past this no Qt platform error in this case. But I have other python-qt programs that needs other python versions/install path, this will break those programs. I have found that pyqt programs usually have this problem, I still think (and sincerely hope) there is better approach than to mess with the PATH environment. I am still open to other suggestions of course.

ModuleNotFoundError: No module named '<module.name>' - Installation error

I am building a desktop app using Python and PySimpleGUI. So far, everything works just fine. Whilst I was working at the project, I realized I need to find a way to get the duration of some mp3 files and to display it in a certain way. I discovered mutagen module that is supposed to help me in this sense, I installed, and here the problem arise:
It throws me ModuleNotFoundError: No module named 'mutagen'.
Seeing this, I started to look for the problem, but I couldn't not understand why my interpretor did not find the module even though I Installed it CORRECTLY. (as PyCharm told me)
I have tried the following:
I am using a local virtual environment that has installed the dependecies for the project(and some extra) and I uninstalled and installed the package 3-4 times
I deleted the local virtual environment and I created another one. I installed the packages again and same issue.
I installed a random module (scipy) and I tried to import it somewhere in the project and it thrown me same error, but this time for scipy module
My guess is that I did not configured properly my interpreter, but to be honest, I have no idea what I am doing wrong, because I followed the same steps I've been using for creating a venv with its according interpreter and for other projects, it worked just fine.
Further details:
Using python3.9 base .exe
I installed the packages in two ways: one using the pycharm IDE, and one by running pip3 install mutagen
You may be using a different pip that is not the one that affects the Python you are using. Instead of using
pip install mutagen
Consider using pip as a module of the Python you are using:
python -m pip install mutagen
This way you'll be sure you are working on the same Python.
If you want to continue using plain pip, try which python and which pip to make sure they are referencing the same environment.

How to embed FreeCAD in a Python virtual environment?

How do I setup a Python virtual environment with the FreeCAD library embedded as to enable import as a module into scripts?
I would like to avoid using the FreeCAD GUI as well as being dependent on having FreeCAD installed on the system, when working with Python scripts that use FreeCAD libraries to create and modify 3D geometry. I hope a Python virtual environment can make that possible.
I am working in PyCharm 2021.1.1 with Python 3.8 in virtualenv on Debian 10.
I started out with FreeCAD documentation for embedding in scripts as a basis:
https://wiki.freecadweb.org/Embedding_FreeCAD
In one attempt, I downloaded and unpacked .deb packages from Debian, taking care to get the correct versions required by each dependency. In another attempt, I copied the contents of a FreeCAD flatpak install, as it should contain all the libraries that FreeCAD depends on.
After placing the libraries to be imported in the virtual maching folder, I have pointed to them with sys.path.append() as well as PyCharm's Project Structure tool in various attempts. In both cases the virtual environment detects where FreeCAD.so is located, but fails to find any of its dependencies, even when located in the same folder. When importing these dependencies explicitly, each of them have the same issue. This leads to a dead end when an import fails because it does not define a module export function according to Python:
ImportError: dynamic module does not define module export function (PyInit_libnghttp2)
I seem to be looking at a very long chain of broken dependencies, even though I make the required libraries available and inform Python where they are located.
I would appreciate either straight up instructions for how to do this or pointers to documentation that describes importing FreeCAD libraries in Python virtual environments, as I have not come across anything that specific yet.
I came across a few prior questions which seemed to have similar intent, but no answers:
Embedding FreeCAD in python script
Is it possible to embed Blender/Freecad in a python program?
Similar questions for Conda focus on importing libraries from the host system rather than embedding them in the virtual environment:
Incude FreeCAD in system path for just one conda virtual environment
Other people's questions on FreeCAD forums went unanswered:
https://forum.freecadweb.org/viewtopic.php?t=27929
EDIT:
Figuring this out was a great learning experience. The problem with piecing dependencies together is that for that approach to work out, everything from the FreeCAD and its dependencies to the Python interpreter and its dependencies seems to need to be built on the same versions of the libraries that they depend on to avoid causing segmentation faults that brings everything to a crashing halt. This means that the idea of grabbing FreeCAD modules and libraries it depends on from a Flatpak installation is in theory not horrible, as all parts are built together using the same library versions. I just couldn't make it work out, presumably due to how the included libraries are located and difficulty identifying an executable for the included Python interpreter. In the end, I looked into the contents of the FreeCAD AppImage, and that turned out to have everything needed in a folder structure that appears to be very friendly to what PyCharm and Python expects from modules and libraries.
This is what I did to get FreeCAD to work with PyCharm and virtualenv:
Download FreeCAD AppImage
https://www.freecadweb.org/downloads.php
Make AppImage executable
chmod -v +x ~/Downloads/FreeCAD_*.AppImage
Create folder for extracting AppImage
mkdir -v ~/Documents/freecad_appimage
Extract AppImage from folder (note: this expands to close to 30000 files requiring in excess of 2 GB disk space)
cd ~/Documents/freecad_appimage
~/Downloads/./FreeCAD_*.AppImage --appimage-extract
Create folder for PyCharm project
mkdir -v ~/Documents/pycharm_freecad_project
Create pycharm project using Python interpreter from extracted AppImage
Location: ~/Documents/pycharm_freecad_project
New environment using: Virtualenv
Location: ~/Documents/pycharm_freecad_project/venv
Base interpreter: ~/Documents/freecad_appimage/squashfs-root/usr/bin/python
Inherit global site-packages: False
Make available to all projects: False
Add folder containing FreeCAD.so library as Content Root to PyCharm Project Structure and mark as Sources (by doing so, you shouldn't have to set PYTHONPATH or sys.path values, as PyCharm provides module location information to the interpreter)
File: Settings: Project: Project Structure: Add Content Root
~/Documents/freecad_appimage/squashfs-root/usr/lib
After this PyCharm is busy indexing files for a while.
Open Python Console in PyCharm and run command to check basic functioning
import FreeCAD
Create python script with example functionality
import FreeCAD
vec = FreeCAD.Base.Vector(0, 0, 0)
print(vec)
Run script
Debug script
All FreeCAD functionality I have used in my scripts so far has worked. However, one kink seems to be that the FreeCAD module needs to be imported before the Path module. Otherwise the Python interpreter exits with code 139 (interrupted by signal 11: SIGSEGV).
There are a couple of issues with PyCharm: It is showing a red squiggly line under the import name and claiming that an error has happened because "No module named 'FreeCAD'", even though the script is running perfectly. Also, PyCharm fails to provide code completion in the code view, even though it manages to do so in it's Python Console. I am creating new questions to address those issues and will update info here if I find a solution.

ModuleNotFoundError when installing packages

I've been having a problem with installing packages in PyCharm. This started happening a few months ago, but before that it was fine. I can install it from the cmd using pip, but when I try to import it into the IDE it gives this error:
ModuleNotFoundError: No module named '_winreg'
and isn't available for use. The package I'm trying to install right now is pygame, but it has had trouble with other ones. I tried to make sure I have winreg installed by using 'pip3 install winreg' in the cmd, but it gives error text saying, "Could not find a version that satisfies the requirement winreg." What am I doing wrong?
Also, if it helps, I'm using Windows and the latest version of PyCharm, as well as python 3. Thanks for the help, I've been looking for a while now.
I think one of your problems possibly is that you didn't add pygame in your project interpreter. (To add it go to File -> Settings -> Project Interpreter then click the green plus sign and search for pygame.) I don't know if this will work for winreg, but tell me if it does.
I can't find _winreg anywhere, I've searched winreg's file, and modules , there don't seems like any of _winreg. I think you probably want to import winreg, not _winreg.
And also, if you are using PyCharm's venv, you need to use the project's prompt to import,or project interpreter.
Could not find a version that satisfies the requirement sometime means your python's version is too new for the module to use.

python-poppler-qt5 install error on win7

Direct installation of no, the named sipdistutils module python-poppler-qt5 problem, so I found a place on the Internet from the python-poppler-qt5 folder, there is a mistake in the image above
My environment: python3.4 pyqt5.4
I want to use pyqt5 to make a PDF reader.
You must also install the poppler-qt5 library. Python-poppler-qt5 is
only the python binding for the library.
See https://sourceforge.net/projects/poppler-win32/

Resources