Pyinstaller: ImportError: cannot import name QtGui - pyside

Using the latest pyinstaller on Windows 7 to make a standalone exe (-F), when running the exe:
ImportError: cannot import name QtGui
In the pyinstaller hooks directory there is special handling for PyQt4 though not PySide.
Hoping for a workaround for this or something to try.
Environment
Windows 7 64-bit
Python 2.7 32-bit
PYTHONHOME=c:\python27
PYTHONPATH=c:\python27\lib
PYTHONLIB=c:\python27\libs\python27.lib;c:\python27\lib\site-packages
Steps
1. Add PySide from http://releases.qt-project.org/pyside/1.1.1/PySide-1.1.1qt474.win32-py2.7.exe
2. Unzip https://github.com/pyinstaller/pyinstaller/zipball/develop to c:\pyinstaller1.5.1
3. Run the commands below against a .py file containing simply:
from PySide import QtGui
[...or QtCore or or.]
Run
c:\pyinstaller1.5.1>pyinstaller.py -F import_test.py
108 INFO: wrote c:\pyinstaller1.5.1\import_test.spec
171 INFO: Testing for ability to set icons, version resources...
296 INFO: ... resource update available
312 INFO: UPX is not available.
4321 INFO: checking Analysis
4382 INFO: checking PYZ
4430 INFO: checking PKG
4446 INFO: building because c:\pyinstaller1.5.1\build\pyi.win32\import_test\import_test.exe.manifest changed
4446 INFO: building PKG out00-PKG.pkg
16782 INFO: checking EXE
16782 INFO: rebuilding out00-EXE.toc because pkg is more recent
16782 INFO: building EXE from out00-EXE.toc
16799 INFO: Appending archive to EXE c:\pyinstaller1.5.1\dist\import_test.exe
c:\pyinstaller1.5.1>dist\import_test.exe
Traceback (most recent call last):
File "<string>", line 23, in <module>
ImportError: cannot import name QtGui
Note
At the end of the PySide install (as admin), this message:
close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr
If that is about post install it can be handled manually:
c:>python.exe c:\Python27\Scripts\pyside_postinstall.py -install
Generating file C:\python27\qt.conf...
PySide installed in c:/python27/Lib/site-packages/PySide...
The PySide extensions were successfully installed.

Workaround. This worked:
# Various imports, whatever, using normal sys.path, for example:
import os, sys, re, time, random
import subprocess, psutil
# Save sys.path
sys_path_saved = sys.path
# Limit sys.path for PySide import
sys.path = ['c:\\python27\\lib\\site-packages']
# PySide imports with limited sys.path
from PySide import QtGui, QtCore
from PySide.QtGui import QApplication, QLineEdit
from PySide.QtCore import QSettings, Qt
# Reset sys.path to original
sys.path = sys_path_saved
# Remainder of code...
Pyinstaller 1.5.1 should do a fine job of locating dependencies, and often does.
However, all of many many attempts to use its pathex or hiddenimports in .spec failed.
Modifying my environment variables also failed.
Extracting various module files manually from from .egg sometimes worked.
However for PySide imports, the sys.path temporary limitation above was the workaround that worked.
Update: Unfortunately the exe only works on a machine with Python/Pyside installed, doesn't work on XP without Python.

For me what worked straight away was to copy/paste the two folders PySide6 and shiboken6 from my python installation folder (PYTHON_FOLDER/Lib/site-packages) to the dist/APP_NAME folder resulting from pyinstaller SCRIPT_NAME.

Related

Python3 cannot find statsmodels.api but I can and so can my linter

I recently got into development with Python running on WSL (Ubuntu 18.04 LTS).
I followed the documentation from here and I'm able to run simple python scripts.
I started playing around with libraries that I installed using the pip3 command such as numpy and pandas and these work fine.
The problem arises when I try to use the statsmodels package.
I've installed it using pip3 install statsmodels
I can see the package in /home/username/.local/lib/python3.6/site-packages/statsmodels I can even see the api.py file in that directory, however, when I type import statsmodels.api as sm as recommended on the statsmodels website I get:
Console output:
username#DESKTOP-1JP4BIE:/mnt/c/users/username/dev/project/playground$ python3 statsmodels.py
Traceback (most recent call last):
File "statsmodels.py", line 5, in <module>
import statsmodels.api as sm
File "/mnt/c/username/chris/dev/project/playground/statsmodels.py", line 5, in <module>
import statsmodels.api as sm
ModuleNotFoundError: No module named 'statsmodels.api'; 'statsmodels' is not a package
I've tried uninstalling and reinstalling (did not work)
I really can't see anything that differentiates this package from the others that I've installed. Does anyone have any insights?
Thanks #Vorsprung durch Technik
The issue was that my file name was statsmodels.py.
I'll remember to be more careful when naming my python files.

exe built with cx_Freeze, PyQt5, Python3 can't import ExtensionLoader_PyQt5_QtWidgets.py and run

I'm trying to pack my app Python3, PyQt5 for Windows using cx_Freeze.
I've installed Python 3.4.3, Python-win32, PyQT5, cxfreeze.
Application itself, run in console, works fine.
I try to pack it with cx_freeze:
python setup.py build_exe.
It works on the same host. But when I move it to another clean installation WinXP it gives an error:
Traceback:
File "C:\Python34\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27 in <module>
File "pyftp1.py" in 7, in <module>
File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", in 2237, in _find_and_load
File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", in 2226, in _find_and_load_unlocked
File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", in 1191, in _load_unlocked
File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", in 1161, in _load_backward_compatible
File "ExtensionLoader_PyQt5_QtWidgets.py", line 22, in <module>
File "ExtensionLoader_PyQt5_QtWidgets.py", line 14, in __bootstrap__
ImportError: DLL load failed: Не найден указанный модуль.
English: "specified module was not found".
Here's my setup.py:
import sys
from cx_Freeze import setup, Executable
includes = ['sys', 'PyQt5', 'PyQt5.Core', 'PyQt5.QtGui', 'PyQt5.QtWidgets', 'os', 'os.path', 'ftplib',
'traceback', 'time',]
excludes = []
packages = ['os', 'PyQt5']
path = []
build_exe_options = {
'includes': includes,
'excludes': excludes,
'packages': packages,
'path' : path,
#'dll_includes': ['msvcr100.dll'],
'include_msvcr' : True,
'include_files': [
(r'C:\Windows\System32\msvcr100.dll', 'msvcr100.dll'),
],
}
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
setup(
name = "pyftp1",
version = "0.1",
description = "Foto uploader",
options = {'build_exe_options': build_exe_options},
executables = [Executable("pyftp1.py", base=base, appendScriptToLibrary=False, copyDependentFiles=True)]
)
Also I found it doesn't copy msvcr100.dll file, however I've stated it twice! I've copied it manually to target host.
Here's pyftp1.py:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys, time, os, hashlib
import ftplib
import traceback
from PyQt5.QtWidgets import QApplication, QWidget, QFileDialog # <-- Line 7
from PyQt5.QtWidgets import QPushButton, QHBoxLayout, QVBoxLayout, QScrollArea, QLineEdit, QCheckBox
from PyQt5 import QtGui
from PyQt5.QtGui import QIcon, QPalette, QLinearGradient, QColor, QBrush
from PyQt5.QtCore import QObject, QThread, pyqtSignal, pyqtSlot
Solution.
After struggling during a lot of time with a problem I've found the solution. Many how-to do not take an account specific versions of software, or some specific modules used, or even they do not check .exe on fresh system, which has no your libraries dlls and so on. Here's working and tested solution.
First, I removed cx_freeze and used py2exe.
I've built all the stuff on WinXP 32-bit - for the goal it could be run on nearly every system.
You need:
Win XP 32-bit (or another system which you consider as minimal requirements)
Python 3.4 -- install from www.python.ord/download Windows x86 msi installer
PyWin32 -- install from sf.net pywin32-219.win32.py3.4.exe
Qt5.5 -- install it from qt.io, also you get mingw, next add folder C:\Qt\Tools\mingw492_32\bin to your system env %PATH%.
Git for Windows -- install from git-scm.com
PyQt5 -- download from riverbankcomputing.co.uk win32 x86 installer and run it
SIP -- the worst intallation!
Download it from riverbankcomputing.co.uk,
unzip,
run Git Bash,
python configure.py -p win32-g++ ,
mingw32-make.exe install.
It fails (. You go manually to sipgen/ folder and copy sip.exe to c:\Python34. Next go to ../siplib and copy sip.pyd to c:\python34\Lib\site-packages, then
strip /c/Python34/Lib/site-packages/sip.pyd
cp sip.h /c/Python34/include/
py2exe -- python -m pip install py2exe
7-Zip -- install from 7-zip.org as x86 exe
7-zip extra, unpack into installed 7-zip folder, conflicts with .txt files could be ignored
Resource Hacker -- from angusj.com -- however it's not necessary if you don't want to set to patch your .exe finally and give it a nice icon
Next I added setup.py to the project:
from distutils.core import setup
import os, sys
import py2exe
from glob import glob
import PyQt5
NAME="ProgName"
qt_platform_plugins = [("platforms", glob(PyQt5.__path__[0] + r'\plugins\platforms\*.*'))]
data_files.extend(qt_platform_plugins)
msvc_dlls = [('.', glob(r'C:\Windows\System32\msvc?100.dll'))]
data_files.extend(msvc_dlls)
# print(data_files)
sys.argv.append('py2exe')
setup(
data_files=data_files,
# windows=["pyftp1.py",],
windows=[
{
"script": "pyftp1.py",
"icon_resources": [(0, "resources/favicon.ico")]
}
],
# zipfile=None,
options={
"py2exe": {
"includes":["sip", "atexit",],
# "packages": ['PyQt5'],
"compressed": True,
"dist_dir": "dist/" + NAME,
# "bundle_files": 0,
# "zipfile": None,
"optimize": 2,
}
}
)
Just not to run all the actions manually, you may add Makefile
# start settings
DIST=dist
# change NAME also in setup.py and resource/config.txt
NAME=ProgName
EXT=exe
# final name and location of the built program
FINAL=$(DIST)/$(NAME).$(EXT)
# external programs
7ZIPDIR="C:\Program Files\7-Zip"
RESHACKER="/c/Program\ Files/Resource\ Hacker/ResourceHacker.exe"
# intermediate steps
# no icon version of program
NOICON=$(DIST)/$(NAME)_no_icon.$(EXT)
# name of .7z archive
7Z_BASENAME=$(NAME).7z
7Z=$(DIST)/$(7Z_BASENAME)
# folder with ready .exe
PROGDIR=$(DIST)/$(NAME)
all: $(FINAL)
$(FINAL): $(NOICON)
# change icon
"$(RESHACKER)" -addoverwrite $(NOICON), $(FINAL), resources/favicon.ico, ICONGROUP, MAINICON, 0
$(NOICON): $(7Z)
#build autorunning sfx with default icon
cat $(7ZIPDIR)/7zS.sfx resources/config.txt $(7Z) > $(NOICON)
$(7Z): exe
# compress program folder to .7z
cd $(DIST); $(7ZIPDIR)\\\7z.exe a $(7Z_BASENAME) $(NAME)
exe: $(DIST)
# build program itself
python setup.py py2exe
$(DIST):
# create dist directory
# echo $(DIST)/
clean:
rm -rf $(DIST)/*
Now you can easily build your program itself - as a folder of files:
mingw32-make.exe exe
Or make a single file with correct icon:
mingw32-make.exe all
Or clean that all stuff:
mingw32-make.exe clean
you could try this:
add 'libEGL.dll' to your working folder. This is from Python3x\Lib\site-packages\PyQt5
In cx-freeze setup.py add:
if sys.platform == "win32":
includefiles = ['libEGL.dll'] #here you should add other files if you need to
In cx-freeze setup.py link "includefiles" to build_exe_options like this:
build_exe_options= {
...
...
'include_files': includefiles,
}

py2app throws [errno 35] Resource Not Available during unconditional imports

I'm using python 2.7 installed via macports, pyobjc, cocoa and notably scipy (via FlowCytometryTools) with py2app to create a small mac application. Py2app's setup.py run with sudo python setup.py py2app completes nicely with -A for testing, but is unable to complete when running without that parameter to create a full .app build.
The build process will get a variable distance through the unconditional imports and then give error: [Errno 35] Resource temporarily unavailable and immediately exit. The number of unconditional import lines that complete before the error occurs changes. The longest run so far was seen when running immediately after a reboot. I've tried running with and without removing the previous build/dist folders to no effect.
Modules not found (unconditional imports):
...
* builtins.int (Cython.Build.Inline, Cython.Compiler.ExprNodes, IPython.utils._tokenizeerror: [Errno 35] Resource temporarily unavailable
My setup.py looks like this:
import sys
sys.setrecursionlimit(1500) #required to avoid recursion triggered early exit in py2app while compiling scipy. (default is 1000)
from setuptools import setup
APP = ['FlowMac.py']
DATA_FILES = ['FlowMac_Main.xib']
OPTIONS = {'argv_emulation': True}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
and the imports section of my python file FlowMac.py looks like this:
from Cocoa import *
from Foundation import *
from AppKit import *
from FlowCytometryTools import * #file/data handling via scipy and pandas
from pylab import * #graphing histograms
Commenting out both the FlowCytometryTools and the pylab imports allows the py2app build to complete but of course renders the program nonfunctional.
What is happening?
How can I get more information on which resource is unavailable?
was hitting a recursion limit a clue to my problem?
I'm running Yosemite on a MacBook Pro with 8GB RAM, so I'm very surprised to be hitting a wall here.
Thanks for your time.
UPDATE 4/29/2015:
Importing everything works fine if I remove my .xib from the datafiles array of the py2app launcher setup.py. A blank file with just import Cocoa, Foundation and Appkit works fine. Importing the xib with any one of FlowCytometryTools, pylab, scipy, matplotlib, numpy does not work. pylab and FlowCytometryTools rely on the other three, and any one of scipy, matplotlib or numpy brings in py2app recipies for the other two. One of these recipies isn't working with the xib, but I don't know why...
No experience with py2app, but I am wondering if you tried generating a more minimal version of the code that would fail to aid troubleshooting?
Maybe having FlowMac.py include nothing but the import statements:
import Cocoa
import Foundation
import AppKit
import FlowCytometryTools
import pylab
Also could you narrow it down to whether the problem appears due to pylab or due to FlowCytometryTools? (By commenting them out individually?)
From my testing, this error seems to be caused by the log output itself from py2app. Try redirecting standard error to a file:
python setup.py py2app 2>error_log
This should work around the error.

Unable to import argparse 1.2.1 for python 2.5 on windows

So, I thought that I had correctly installed the setuptools and argparse 1.2.1.
C:\Python25_64bit>python ez_setup.py argparse
Searching for argparse
Best match: argparse 1.2.1
Processing argparse-1.2.1-py2.5.egg
argparse 1.2.1 is already the active version in easy-install.pth
Using c:\python25_64bit\lib\site-packages\argparse-1.2.1-py2.5.egg
Processing dependencies for argparse
Finished processing dependencies for argparse
C:\Python25_64bit>python
Python 2.5.4 (r254:67916, Dec 23 2008, 15:19:34) [MSC v.1400 64 bit (AMD64)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import argparse
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named argparse
>>> quit()
However, it seems that I can't import it, as you can see above. I also tried directly adding the egg to my Eclipse PyDev projects, but this was to no avail, either. As you can see, nothing is set in my environment. Perhaps, this is the problem?
C:\Python25_64bit>echo %PYTHONPATH%
%PYTHONPATH%
C:\Python25_64bit>echo %PYTHONHOME%
%PYTHONHOME%
I hope you paid attention while installing and noted where argparse was installed.
In your script (run from the commandline or from your IDE) include at the top:
import sys
for p in sys.path:
print repr(p)
to see all the paths python is searching through. Either add the path to argparse using:
sys.path.insert(0, '/your/path/to/argparse')
or move the install to something in the path.
The environment variables can be empty, there are compiled in elements for sys.path as well.

IPython fails to load win32api

I'm running Python 2.7 on windows and I've installed the pywin extension here.
Having done so, if I launch a normal python shell, I can import win32api perfectly.
If I do the same on IPython, I get this :
In [1]: import win32api
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
C:\Windows\system32\<ipython console> in <module>()
ImportError: DLL load failed: The specified module could not be found.
In [2]:
Why would it work in a normal python shell, but not IPython?
I had the same issue, and found a solution for my problem here. Apparently it's a conflict between pythoncom24.dll and pywintypes24.dll.
Look in your root Python install folder. If you find these two DLLs there, move them to \Python24\Lib\site-packages\win32 instead. This should fix your import conflict.
Alternately, you can control the imports explicitly. Add the following to your script in this order:
import pywintypes
import pythoncom
import win32api

Resources