RuntimeError: the sip module implements API v8.0 to v8.1 but the PyQt4.QtCore module requires API v7.1 - pyqt4

I manually installed PyQt-4.9.1 and sip-4.13.2 under /tmp/yifli because the ones currently installed on the machine (running Fedora 13) are too old for my software.
After that, I added their locations to $PYTHONPATH and here is the output of sys.path:
>>> import sys
>>> print sys.path
['', '/tmp/yifli/lib/python/site-packages', '/tmp/yifli/lib/python/site-packages/PyQt4', '/usr/lib/python26.zip', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/site-packages', '/usr/lib/python2.6/site-packages/Numeric', '/usr/lib/python2.6/site-packages/PIL', '/usr/lib/python2.6/site-packages/gst-0.10', '/usr/lib/python2.6/site-packages/gtk-2.0', '/usr/lib/python2.6/site-packages/scim-0.1', '/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info', '/usr/lib/python2.6/site-packages/webkit-1.0']
However, the error I got is due to the fact that somehow the old PyQt4 is still being used:
>>> import sip
>>> sip.__file__
'/tmp/yifli/lib/python/site-packages/sip.so'
>>> import PyQt4
>>> PyQt4.__file__
'/usr/lib/python2.6/site-packages/PyQt4/__init__.pyc'
How come?
P.S., I did get errors when I compiled Qt which complained some header file for Qt Phonon module cannot be found. But since I don't use that module, I just ignored it.
Yifei

Firstly, installing things in /tmp is not a good idea, because it is intended only for temporary files (most systems will be set up to remove everything in /tmp during the boot or shutdown process).
Secondly, you should NEVER attempt to modify your system python or any of its packages - this will almost certainly lead to breakage of other applications that depend on python. If you need a different version of python and/or its packages, create a completely separate installation under /usr/local.
With that in place, you just need to ensure that your new python is specified whenever you are compiling packages for it.
So, to compile Sip you would do:
/usr/local/bin/python sip_source/configure.py
And for PyQt4, you would do the same - but also add a couple of other options that should avoid over-writing system files:
/usr/local/bin/python pyqt4_source/configure.py \
--qsci-api-destdir /usr/local/lib/qt4/qsci --no-designer-plugin
Once this has been set up, you can then create a simple wrapper script for running applications that need the upgraded python:
#!/bin/sh
exec /usr/local/bin/python myapp.py "$#"
Note that you do not need to change $PYTHONPATH for any of this to work, and so you should undo any changes you have made to it. (And you might also want to consider re-installing your fedora sip and pyqt packages to ensure everything is put back the way it was).
As for the errors regarding Phonon, the solution is simple: if the header files are missing, install the fedora package that contains them.

Related

Debian change dist-package path

I'm currently using Debian GNU/Linux bookworm, but I'm struggling with python3 package updates.
In fact, it uses :
/usr/lib/python3/dist-packages to check for packages installed for system software and this is not what i want. This directory contains python3.9 packages, and it later raises compatibility uses
I want to use this directory instead:
/usr/local/lib/python3.10/dist-packages/
That exists in my computer with the software in the version that i want but it doesn't works. I've tried the
PYTHON_INCLUDE_DIR=/usr/include/python3.10
PYTHON_LIBRARY=/home/me/.local/lib/
export PYTHON_INCLUDE_DIR
export PYTHON_LIBRARY
commands, but it did not work.
My py sys.path contains :
['', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '/home/me/.local/lib/python3.10/site-packages', '/usr/local/lib/python3.10/dist-packages', '/usr/lib/python3/dist-packages']
I would really appreciate some help on this one :,(
I even tried to reinstall python and reboot (which caused GNOME to crash), but it stils uses python3.9 dist package path, i don't know how to get rid of it, or even how to update it !

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.

How can I create a Python executable program that doesn't force the user to actually download Python libraries and that stuff in order to be executed?

Let's say that I already built a program that essentially takes some images from an user's path and make new ones using the following libraries/modules:
os itertools pandas PIL
What I need now is to make that program become an executable file that can be actually executed without having to have installed the Python environment and the libraries used in the code, because the user would not know how to code, and would not likely matter how the code works.
The program would run on Windows OS (PC) only , and would use the cmd.exe as the interpreter and medium for user inputs and results display.
Solved here
You can use wheel, and ship whole environment.
#Vincent Caeles:
"#rh979 a wheel is not meant to have all dependencies. Subsequently you could do pip install path/to/wheel.whl --target /path/to/some/folder and zip the contents of the 'folder' to have all your dependencies in the zip archive and ship that to the environment where you want to run your code.
"
Another options is pyinstaller library, according to documentation it should do what you need.

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.

PyQt5 stubs files

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.

Resources