Runtime error when calling a .exe made with PyInstaller including PyQt4 - python

So, I have two .py files, one generated by QtDesigner and another which basically implements the functionality of the GUI. Using, pyinstaller, I generated a .exe file to use it on systems without python and the associated libraries.
The command: pyinstaller my_script.py runs fine without any errors.
The problem occurs when I run the .exe file.
Error:
Qt: Untested Windows version 10.0 detected!
Traceback (most recent call last):
File "site-packages\PyInstaller\loader\rthooks\pyi_rth_qt4plugins.py", line 41, in
ImportError: No module named 'PySide'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "site-packages\PyInstaller\loader\rthooks\pyi_rth_qt4plugins.py", line 43, in
File "", line 2237, in _find_and_load
File "", line 2226, in _find_and_load_unlocked
File "", line 1191, in _load_unlocked
File "", line 1161, in _load_backward_compatible
File "C:\python\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 714, in load_module
module = loader.load_module(fullname)
RuntimeError: the PyQt4.QtCore and PyQt5.QtCore modules both wrap the QObject class
[11364] Failed to execute script pyi_rth_qt4plugins
So I tried to find a solution to this. These are the solutions I tried:
How to force PyQt5 use for QObject class? - simply make the PyQt import as the first statement doesn't resolve the issue.
https://github.com/tzutalin/labelImg/issues/268 - Here it is recommended to remove PyQt4 and use only PyQt5. I do have both of them on my system, some projects rely on PyQt5 and some on PyQt4 hence I don't want remove the latter. Also, there has to be another solution, is making me not do this.
https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000551170-PyQt4-and-PyQt5-collisions-in-PyCharm-2017-2-1-when-debugging-QGIS-application - This was a similar error, so I added:
matplotlib.rcParams['backend'] = 'Qt4Agg'
matplotlib.rcParams['backend.qt4'] = 'PyQt4'
to my imports, still didn't work.
Note:
I am using:
PyCharm 2018.1 (Community Edition)
Build #PC-181.4203.547, built on March 26, 2018
JRE: 1.8.0_152-release-1136-b20 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0
and the code works fine in the IDE.
EDIT:
My imports are:
from PyQt4 import QtCore, QtGui
from matplotlib.backends.backend_qt4agg import FigureCanvasQTagg as Canvas
I am not adding any other import statements related to Qt.
EDIT - 2:
Trying cx_Freeze instead of PyInstaller, here is the setup file.
import sys
from cx_Freeze import setup, Executable
base = None
if sys.platform == "win32":
base = "Win32GUI"
additional_mods = ['numpy.core._methods', 'numpy.lib.format',
'numpy._distributor_init']
setup( name="ASCII2fig",
version = "0.1",
description = "GUI",
options = {'build_exe': {'includes': additional_mods}},
executables = [Executable("ASCII2figALL_main_edited.py", base=base)])
I added the additional_mods after executing the script once following ImportError, which are non-stop. Any ways to hack and find which libraries I should mention explicitly?
Additionally, I also tried to check which libraries are actually being imported when I run my main script with Qt using:
from modulefinder import ModuleFinder
filename = "ASCII2figALL_main_edited.py"
finder = ModuleFinder()
finder.run_script(filename)
for name, mod in finder.modules.items():
print(name)
and apparently, it is importing PyQt5 internally. As mentioned before, I have NO import statements mentioning PyQt5.
Edit - 3
So, I changed the code to pure PyQt5, updated the pyinstaller to the latest version - 3.4, and now there is a new issue where it doesn't find the Qt plugins. It is still somehow importing PyQt4 and I don't know where.

So, I finally made it to work. Not the ideal case, where I don't have to change the library to PyQt5 and make sure everything is in order with PyInstaller, but it works. So here is what I did:
Installed Python version 3.5 - This was because after I updated to the latest PyInstaller version (3.4) and tried to run on python 3.4, I was getting a new error where it was not able to find Qt plugins. After some searching, I figured that since I installed PyQt5 on Python version (3.4) using a .whl file I found on Python Extension Packages for Windows, the installation didn't come bundled with sip. Additionally, when I tried to install PyQt5 on Python 3.4 using pip, it wouldn't install.
Installed PyQt5 and all other libraries on the new Python version using pip. Note: This version of python doesn't have a PyQt4 installed, so it is quite possible that this was the reason. I will install PyQt4 on this python version and try making the .exe again using PyInstaller and see what happens.
So, to summarize, PyQt5 + Pyinstaller works only for Python version >= 3.5. Hope it helps others!

Related

Python PyQt5: AttributeError: 'QTabWidget' object has no attribute 'setTabVisible'

This is an error the debugger in PyCharm does not catch but when running the script from the command line or from 'idle', the interpreter catches it.
I put a dummy line in using the setToolTip attribute just ahead of the line that was failing and it worked ok.
I'm a little bumfuzzled. Please help.
# failure occurs here with
# Traceback (most recent call last):
# File "D:\PythonApps\Books_db_utility\walk_dir_tree.py", line 1584, in <module>
# main_window = MainWindow(ini_file=None, _path=path)
# File "D:\PythonApps\Books_db_utility\walk_dir_tree.py", line 126, in __init__
# self.initUi()
# File "D:\PythonApps\Books_db_utility\walk_dir_tree.py", line 209, in initUi
# self.main_tab_widget.setTabVisible(2, True)
# AttributeError: 'QTabWidget' object has no attribute 'setTabVisible'
self.main_tab_widget.setTabVisible(2, True)
self.main_tab_widget.setTabVisible(3, False)
Another question: I would like to include a zip file with this question with the .py files with the code and the on generated with pyuic5. Can I do that?
Thanks,
sherril
Well, it's magic. I was using the PyCharm 2021.2.3 (Community Edition), Python 3.10 interpreter and PyQt5 5.15.6 when I got the exception I mentioned in my original post.
I changed the interpreter to Python 3.8 with PyQt5 5.15.4 and voila it worked like I expected it should.
So, I changed the interpreter back to the Python 3.10 that I was originally using and voila it worked like I expected it should.
Thence, I ran it from the command line (Python 3.8 PyQt5 5.15.2 from an Anaconda3 distribution; this was the interpreter that I "did not" refer to in my original question and bummer it failed again.
So, I replace pyqt5 5.15.2 with 5.15.6 (using pip) in the Anaconda3 distribution's file and voila it worked like I expected it should. (pip may have been a no-no; I suppose I will learn in time.)
I do not intend to reinstall the Pyqt5 5.15.2 in the Anaconda3 unless someone is really interested in was it the version or was something corrupted.
Probably you didn't import correctly the package so it couldn't be found.
Did you import it in the beggining of the code:
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton, QWidget, QAction, QTabWidget, QVBoxLayout, QLabel
Reference: https://www.geeksforgeeks.org/pyqt5-qtabwidget/
Here is the documentation: https://doc.qt.io/qt-5/qtabwidget.html

I can`t import wxpython in anywhere except python console

I installed python 3.8.8 and installed wxpython using pip at terminal
pip install wxpython
and i run simple program
import wx
print(wx.version())
in pycharm and pycharm`s python console, I got
ModuleNotFoundError: No module named 'wx'
in IDLE, I got
Traceback (most recent call last):
File "C:/Users/tasoo/OneDrive/Desktop/wx.py", line 1, in <module>
import wx
File "C:/Users/tasoo/OneDrive/Desktop\wx.py", line 2, in <module>
print(wx.version())
AttributeError: partially initialized module 'wx' has no attribute 'version' (most likely due to a circular import)
in python.exe code works
I want to import wx in pycharm project.
I tried add python in system path but it didn`t work.
You have problem because you saved code in file wx.py and now import wx loads your file wx.py instead of module wx. Rename your file - ie. main.py instead of wx.py
PyCharm may have own Python installed and it may need to install wx in this Python.
Check
import sys
print(sys.executable)
to get full path to Python used by PyCharm and then use this path
/full/path/to/python -m pip install wx
Or search in PyCharm settings (in menu File) and change Python Interpreter.
In PyCharm for every project you may set different Python - if you have installed many versions.

Pyqtgraph error when running built in examples

I'm trying to work out how to use pyqtgraph so that I can use it.
I've tried to run this block of code which I found on the pyqtgraph website
import pyqtgraph.examples
pyqtgraph.examples.run()
But it always throws this error
Traceback (most recent call last):
File "/Users/willemhandreck/Code/Misc.Projects/heater_alert/python/plotly_test.py", line 1, in <module>
import pyqtgraph.examples
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyqtgraph/__init__.py", line 13, in <module>
from .Qt import QtGui
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyqtgraph/Qt.py", line 35, in <module>
from PySide import QtGui, QtCore, QtOpenGL, QtSvg
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PySide/QtGui.so, 2): Library not loaded: libpyside-python2.7.1.2.dylib
Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PySide/QtGui.so
Reason: image not found
How would I fix this?
Yep, same problem here if I try to use the built-in python on the mac.
My solution is to use the anaconda python distribution:
1) Download & install Anaconda python, which already has all the right scientific modules installed. http://continuum.io/downloads#all
2) Restart your terminal and ensure you have anaconda as your python:
$ which python
/Users/caleb/anaconda/bin/python
3) Then install pyqtgraph:
$ pip install pyqtgraph
...
Successfully installed pyqtgraph-0.9.10
4) Run the examples from the command line:
$ python -m pyqtgraph.examples
or use your import... run method above.
-Caleb
This is actually a good thing.
The system python ships installed and configured to run system utilities. It's a good idea to have a separate playpen for special packages, tweaks, upgrades, experiments.
Anaconda makes it autoeasy by containing itself and its packages all within its own directory and prevents messing up anything else.
http://www.reddit.com/r/Python/comments/39qeq6/anaconda_pros_cons_mac_os_x/cs5mxwk

Packaging with pyinstaller: PyQt4 module not found

Hi everyone and thanks for reading. I'm packaging my python code in a single file using pyinstaller, but when I run my packaged file I get the following error:
Traceback (most recent call last):
File "<string>", line 21, in <module>
File "C:\Users\****\Desktop\pyinstaller-2.0\pyinstaller-2.0\PyInstaller\loader\iu.py", line 409, in importHook
ImportError: No module named PyQt4.QtCore
I don't know what this error is telling me, especially since there is no dir name pyinstaller-2.0 on my desktop and I did not use PyQt4 at all.
Imported modules: Tkinter, tkFileDialog, tkMessageBox, multiprocessing, os, sys, time, numpy, scipy.weave, pywt, matplotlib.pyplot
I think the problem is related to multiprocessing, since I did not experience this error before. I used this recipe to implement the multiprocessing module correctly.
If you were using PyQt then the only way to import the modules with PyInstaller is to use
from PyQt4 import QtCore, QtGui
rather than
import PyQt4.QtCore, PyQt4.QtGui
which your error implies. However, you say that you're not using PyQt.
PyQt is an optional dependency of matplotlib so there is a chance that PyInstaller is checking the matplotlib module and consequently including PyQt.
I would suggest excluding the PyQt module from the build; in your .spec file, search out the line for the Analysis class - something like
Analysis( ..., excludes=['PyQt4', 'PyQt4.QtCore', 'PyQt4.QtGui'])
and edit the excludes keyword arg as suggested above.

Where does ActivePython install pyqt4?

I am currently using a Windows XP machine (32 bit). I am new to Scribus, and have enjoyed using it over the other expensive program for desktop publishing. In Scribus I have a prewritten script I would like to run. The beginning of the script contains:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class DockDialog(QDockWidget):
I have installed Python 3.2 via ActivePython (after trying to install from python.org). After the install I then opened a DOS window and typed pypm install pyqt4, which, as far as I could tell installed properly. I then tried to run the script in Scribus and received the following message:
Traceback (most recent call last):
File "<string>", line 8, in ?
File "C:/Program Files/Scribus 1.3.3.14/share/scripts/Scribus-DockWidgets/DockWidgets.py", line 4, in ?
from PyQt4.QtCore import *
ImportError: No module named PyQt4.QtCore
I then tried reinstalling PYQT4, by typing what I had before, and I was told that PYQT4 was already installed via:
skipping "pyqt4" already installed at "%APPDATA%\Python" (3.2)
After looking in c:\Python32 in all of the directories I can not find any PYQT files. Where were they installed? How do I make sure that script is able to fing the PYQT4 files necessary to run?
My PATH variable is:
C:\Python32\;C:\Python32\Scripts;
Ultimately I would like help in getting the scribus script to work.
Thank you for your help!
Type this command to see where PyQt is installed:
pypm files --full-path pyqt4
By default, PyPM installs packages into your %APPDATA%\Python directory. If you want to install globally into C:\Python32 use the -g option (like: pypm -g install pyqt4)
How do I make sure that script is able to fing the PYQT4 files necessary to run
Are you sure that you are invoking Python 3.2? Try running pypm -g install pyqt4 if all else fails.

Categories

Resources