Packaging with pyinstaller: PyQt4 module not found - python

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.

Related

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

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!

PyQt4 module not found

I am new to coding and trying to run a .py. I am using Koopatlas and Koopuzzle to create NSMBW Maps. I have installed PyQt4 (I checked brew list) and Python but when I run it it says this:
Traceback (most recent call last):
File "Koopuzzle.py", line 10, in <module>
from PyQt4 import QtCore, QtGui
ImportError: No module named PyQt4
They're both in .py format, and they both give the same results.
I've checked online sources but I've found nothing. Does someone know how to solve?
Thanks, Gunner

Import pexpect in child module - 'no module named pexpect'

I'm seeing weird behavior that I don't understand, so I'm turning to the experts here on SO for help. I have looked at similar questions, without finding anything that looked helpful.
I'm writing a program that imports two custom modules.
If I run the main program using the cli "./ld_config_automation.py" I get the following error:
laptop$ ./ld_config_automation.py
Traceback (most recent call last):
File "./ld_config_automation.py", line 34, in <module>
from modules.networkcomponent import NetworkComponent
File "/path/to/pprograms/ssh_telnet_automation/modules/networkcomponent.py", line 7, in <module>
import pexpect
ImportError: No module named pexpect
However, if I run the main program using the cli "python ld_config_automation.py" everything works fine and the program runs.
My main program uses the following import statements.
import sys
import getopt
from modules.networkcomponent import NetworkComponent
from modules.recordclass import Record
The module I'm having trouble with "NetworkComponent" uses the following import statements:
import re
import pexpect
Also, if I remove import pexpect from the NetworkComponent module it works as expected without python in front (up to when it has to use pexpect. of course). So it doesn't seem to be a problem with importing modules, as re works ok, just not pexpect.
I'm developing this on mac os X but will implement on CentOS.

Issues with pyinstaller and pyproj

I'm trying to do an standalone application with pyinstaller. The executable has just build fine, but when I´m trying to do some operations with functions integrated on library pyproj, the executable crashes.
The script runs fine on Pycharm, so I think that the problem is that pyinstaller is not linking with some kind of library of pyproj.
May I have to do something special with spec file or another thing to specify pyproj on the standalone application built with pyinstaller?
This is the error that Ihave obtained:
Traceback (most recent call last): File "<string>", line 6, in
<module> File "C:\pyproj\build\main\out00-PYZ.pyz\pyproj", line 343,
in __new__ File "_proj.pyx", line 85, in _proj.Proj.__cinit__
(_proj.c:1190)
RuntimeError: no system list, errno: 2
This is my "main.py"
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pyproj
print pyproj.__version__
p=pyproj.Proj(init='EPSG:4326')
Thanks in advance
The problem is that when using pyproj with PyInstaller, pyproj can not find the data files that are in the library folder.
The solution is to create a hook file, which will specify where the data files, so you can link them with our executable.
hook-pyproj.py
from PyInstaller.hooks.hookutils import collect_data_files
datas = collect_data_files('pyproj')
The hook file can be located on "hooks" folder on Pyinstaller installation or using the order --additional-hooks-dir, specifying a folder in which will be located "hook-pyproj.py"
Just threading on the previous answer, since 2014 there has been some refactoring on PyInstaller and here is the correct import line for the hook file above :
from PyInstaller.utils.hooks import collect_data_files
datas = collect_data_files('pyproj')
from PyInstaller.hooks.hookutils import collect_data_files
datas = collect_data_files('pyproj')
This didn't worked for me. There were some errors in the executable again.
But I found in another thread that the problem can be solved with this:
from mpl_toolkits.basemap import pyproj as pyproj
pyinstaller seem to have problems to integrate the pyproj module itself, but basemap includes pyproj and is not ignored by pyinstaller.
Just for update

Python: from import error

I'm running Python 2.6.6 on Ubuntu 10.10.
I understand that we can import a module and bind that module to a different name, e.g.
import spam as eggs
also,
from eggs import spam as foo
My problem is that when running the PySide examples, the following import code does not run:
import PySide as PyQt4
from PyQt4 import QtCore, QtGui
It generates an import error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named PyQt4
Clearly, according to the Python interpreter the above code is incorrect, my question is why is it incorrect or rather why doesn't this work?
import and from are a special syntax.
They look for a module name, which means a file in sys.path which starts with the module name.
And it seems like you don't have PyQt4 installed, so it will fail.
The fact that you have a variable called PyQt4 in your namespace after running import PySide as PyQt4 does not change anything, Python is still looking for an actual module called PyQt4 when you do from PyQt4 import QtCore, QtGui.
Try doing
import PySide as PyQt4
QtCore = PyQt4.QtCore
QtGui = PyQt4.QtGui
or
import PySide as PyQt4
from PySide import QtCore, QtGui
That should be equivalent.
I just installed PySide and was doing a tutorial where all the examples used PyQt4. I got tired of changing the imports from PyQt4 to PySide so I just made a symlink in my site-packages, using the following steps:
1) There's surely a better way but I found where my python packages were installed by opening a shell and running python, then at the interactive interpreter typed:
>>> import sys
>>> print sys.path
2) I then found PySide in one of the directories and cd'd to it (n.b. It's at /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages if you're using the macports PySide install for python 2.7 on Mac OSX Leopard 10.5.8).
3) Then I made a symlink with ln, in my case I had to use sudo:
sudo ln -s PySide PyQt4
That's it, now I can just use:
from PyQt4 import QtGui
as normal - happy days!
Obviously, if you ever want to install PyQt4 you should rm the PyQt4 symlink first.
Another caveat: What I've described above may well be wrong/bad in many ways - I am no expert at Python installs but so far it's ok for me. YMMV so use at your own risk. Hopefully someone will comment soon to say "no, very bad!" or ideally "yeah don't sweat it, we cool.."

Categories

Resources