ImportError: cannot import name packages - python

I am using cx_Freeze version 5.1.1 and I have a python 2.7.9 application to package using cx_Freeze. The application is using the python 'requests' module (the version of requests is 2.18.4)
Here is my cx_Freeze setup.py file (note I am specifically including 'requests' in the packages to include):
from cx_Freeze import setup, Executable
import sys
from cx_Freeze import setup, Executable
packages_to_include =['lib','lib/DB','encodings.ascii','requests']
buildOptions = dict(
optimize=1,
excludes = ['tkinter'],
bin_includes = [
'libcrypto.so.1.0.0',
'libcrypto.so.10',
'libgssapi_krb5.so.2',
'libk5crypto.so.3',
'libkeyutils.so.1',
'libssl.so.1.0.1e',
'libssl.so.10'
],
includes = packages_to_include,
packages= ['urllib3','idna']
)
executables = [
Executable(
'workapp.py',
targetName = 'workapp'
)
]
setup(
name='Sample Flask App',
version = '0.1',
description = 'Sample Flask App',
#requires = ["requests"],
options = dict(build_exe = buildOptions),
executables = executables
)
The build completed successfully creating the executable, but upon running my application I got the below error:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/cx_Freeze/initscripts/__startup__.py", line 14, in run
module.run()
File "/usr/local/lib/python2.7/site-packages/cx_Freeze/initscripts/Console.py", line 26, in run
exec(code, m.__dict__)
File "vaas.py", line 2, in <module>
import requests
File "/usr/local/lib/python2.7/site-packages/requests/__init__.py", line 113, in <module>
from . import packages
ImportError: cannot import name packages
I am thinking it is either an issue with my cx_Freeze setup or with requests, but since I tell cx_Freeze to include 'requests', this should work.
I checked in the build directory (that cx_Freeze builds that the requests modules has 'packages' defined and I can find it:
[user#centos-vm]$ ls build/exe.linux-x86_64-2.7/lib/requests/packages/
chardet idna urllib3
Any help is much appreciated!

Found a workaround to this issue:
edit:
/usr/local/lib/python2.7/site-packages/requests/__init__.py
and comment out:
from . import packages
Very strange issue indeed. My unfrozen application works just fine, but the above workaround is needed when using cx_freeze.

Try to add 'request' to the packages list option instead of adding it to the includes list options:
packages= ['urllib3', 'idna', 'requests']
According to the cx_Freeze documentation, the build_exe option includes is a
comma separated list of names of modules to include
whereas the build_exe option packages is a
comma separated list of packages to include, which includes all submodules in the package

Related

ImportError When Using cx_Freeze On a Script The Includes ffpyplayer

I successfully froze a Python 3.6 script with cx_Freeze v5.1.1. Then I added a new capability to the script by adding MediaPlayer from the ffpyplayer package and the script works perfectly. But when I freeze the new script, I receive an error "from ffpyplayer.player import MediaPlayer ImportError: DLL load failed: The specified module could not be found."
The import statement in my application script is
from ffpyplayer.player import MediaPlayer
From other posts, I have tried adding both ffpyplayer and ffpyplayer.player to the packages statement in the cx_Freeze setup.py script (in separate attempts) to fix the problem. I also tried adding player.cp36-win_amd64.pyd to the include statement in setup.py, but I still got the same error message.
cx_Freeze copied all ffpyplayer files from site-packages into the exe.win-amd64-3.6\lib\ffpyplayer except the __pycache__ folder containing __init__.cpython-36.pyc. However, manually adding this folder to exe.win-amd64-3.6\lib\ffpyplayer\ and to exe.win-amd64-3.6\lib\ffpyplayer\player\did not fix the issue (same error message).
Here is a minimal version of the setup.py file that cx_Freeze uses:
from cx_Freeze import setup, Executable
d = "C:\\Users\\Slalo_000\\Dropbox\\Python36Scripts\\AddtoBuild\\"
exe=Executable(script="VSWv300.py", base = "Win32GUI", icon = d + "VSWiconLarge.ico")
includes=[]
excludes=[]
packages=["numpy", "cv2", "ffpyplayer"]
setup(
version = "2.0.0.0",
description = "appName",
author = "me",
author_email = "",
name = "appName",
options ={'build_exe'{'excludes':excludes,'packages':packages,'include_files':includes}},
executables = [exe]
)
EDIT: The full error message traceback is:
File “C:\Users\slalo_000\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\initscripts\__startup__.py”, line 14, in run module.run()
File “C:\Users\slalo_000\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\initscripts\Console.py”, line 26, in run exec(code, m.__dict__
File “VSWv300.py”, line 20, in <module>
File “C:\Users\slalo_000\AppData\Local\Programs\Python\Python36\lib\site-packages\ffpyplayer\player\__init__.py”, line 10, in <module> from ffpyplayer.player.player import MediaPlayer
ImportError:DLL load failed: The specified module could not be found.
Any suggestions?

Py2exe and ConfigParser Error

I'm using Py2exe and ConfigParser but I have somo problems.
I have installed configparser from pip (pip install configparser) and it works fine. In my code I import the package like this import ConfigParser and when I execute works.
Now I have used py2exe to make a distribution and the output console message is:
The following modules appear to be missing
['Carbon', 'Carbon.Files', '_sysconfigdata', 'backports.configparser']
When I have executed the .exe file, the error message is:
File "GUI.py", line 14, in <module>
File "configparser.pyc", line 12, in <module>
ImportError: No module named backports.configparser
I tried use other kind of import like:
from backports import configparser
or
import backports, backports.configparser
And the results are the same.
My setup.py file:
from distutils.core import setup
import py2exe
opts = {
'py2exe': { 'includes' : ["sys","sip", "time", "decimal"],
'excludes': ['_gtkagg', '_tkagg', '_agg2', '_cairo', '_cocoaagg','_fltkagg', '_gtk', '_gtkcairo'],
'dll_excludes': ['oci.dll','libgdk-win32-2.0-0.dll','libgobject-2.0-0.dll']}
}
data_files=[]
setup(
name='Actualizador',
version='1.0',
package={'./img/*', './campos/*'},
scripts=['GUI.py'],
console=['GUI.py'],
#windows=["GUI.py"],
options=opts,
data_files=data_files,
zipfile=None
)
I can't make my distribution and I can't solve my issue. Any suggestion?
Thank You
I solved the problem :-)
py2exe use the directory build to skip the compilation for some package which are yet compiled. Well, I remove the directory, I add the backports path to system path and I make the distribution again. And magic! That works ...

cx_Freeze 5.0: ImportError: No module named 'scipy.__config__'

Problem
I get the following error when trying to run an .exe built with cx_Freeze:
File
"C:\\WinPython-64bit-3.5.2.3Qt5\python-3.5.2.amd64\lib\site-packages\scipy\__init__py", line 105 in <module>
from scipy.__config__ import show as show_config
ImportError: No module named 'scipy.__config__'
During handling of the above exception, another exception occurred:
...
File
"C:\\WinPython-64bit-3.5.2.3Qt5\python-3.5.2.amd64\lib\site-packages\scipy\__init__py", line 105 in <module>
raise ImportError(msg)
ImportError: Error importing scipy: you cannot import scipy while being in scipy source directory; please exit the scipy source tree first, and relaunch your python interpreter.
How could I troubleshoot this?
Additional Information
Setup
Windows 7 Enterprise 64-bit
WinPython-64bit-3.5.2.3Qt5 (Python 3.5.2 64-bit)
cx_Freeze 5.0 (*)
scipy 0.18.1
(*) With pythoncom fix in the hooks.py
setup.py:
import os
import sys
from cx_Freeze import setup, Executable
os.environ['TCL_LIBRARY'] = r"C:\WinPython-64bit-3.5.2.3Qt5\python-3.5.2.amd64\tcl\tcl8.6"
os.environ['TK_LIBRARY'] = r"C:\WinPython-64bit-3.5.2.3Qt5\python-3.5.2.amd64\tcl\tk8.6"
base = 'Console'
if sys.platform == 'win32':
base = 'Win32GUI'
options = {
'build_exe': {
'excludes': ['gtk', 'PyQt4', 'Tkinter'],
}
}
executables = [
Executable('GUI.py', base=base)
]
setup(name='GUI',
version='0.1',
description='GUI test',
executables=executables,
options=options
)
Errors during build
None.
What else have I tried?
1) Tried to add 'includes': ['scipy.__config__'] to the setup.py.
Result: error during build ImportError: No module named 'scipy.__config__'
2) Tried to add 'packages': ['scipy'], to the setup.py.
Result: error during build ImportError: No module named 'scipy'
3) Renamed the finder.IncludePackage("scipy.lib") in the cx_Freeze/hooks.py to finder.IncludePackage("scipy._lib") as instructed in the answer of the SO Question "Cx_freeze ImportError no module named scipy" + added 'packages': ['scipy'], to the setup.py.
Result: No build-time errors. When trying to run the .exe, it gives ImportError: No module named 'scipy.spatial.ckdtree'
Tried also with 'includes': ['scipy.spatial.ckdtree'] in the setup.py, but the problem persists.
The build output has the following lines:
m scipy.spatial.cKDTree C:\WinPython-64bit-3.5.2.3Qt5\python-3.5.2.amd64\lib\site-packages\scipy\spatial\cKDTree.cp35-win_amd64.pyd
m scipy.spatial.ckdtree C:\WinPython-64bit-3.5.2.3Qt5\python-3.5.2.amd64\lib\site-packages\scipy\spatial\ckdtree.cp35-win_amd64.pyd
I think I have a solution (not the greatest but it works). Go to the scipy\spatial directory (within the build directory) and change the file from cKDTree.cp36-win_amd64.pyd to ckdtree.cp36-win_amd64.pyd.
NOTE: your file name might be slightly different depending which python version but the main thing is to use lowercase kdt in the file name.
The capital letters is the problem. It worked for me.

py2exe fails with "No module named 'clr'" when trying to build exe from script using pythonnet

I created a python script that uses pythonnet. The script is in a file named main.py. When I run the script from the command line (simply typing main.py at the Windows command prompt), the imported pythonnet module clr works fine. But when I try to build an exe I get an error saying: No module named clr.
To isolate the cause of this, I have verified that building an executable (in my case a simple Tkinter app) using py2exe works. I only have Python 3.4 installed and have verified that where python points to C:\Python34\python.exe.
The error occurs at executable build time and seems to be triggered by including clr in the section {"includes":["sip","clr"]}} in my setup.py for py2exe. The full traceback is below:
Traceback (most recent call last):
File "setup.py", line 32, in <module>
windows = [{'script': "main.py"}],
File "C:\Python34\lib\distutils\core.py", line 148, in setup
dist.run_commands()
File "C:\Python34\lib\distutils\dist.py", line 917, in run_commands
self.run_command(cmd)
File "C:\Python34\lib\distutils\dist.py", line 936, in run_command
cmd_obj.run()
File "C:\Python34\lib\site-packages\py2exe\distutils_buildexe.py", line 188, i
n run
self._run()
File "C:\Python34\lib\site-packages\py2exe\distutils_buildexe.py", line 267, i
n _run
builder.analyze()
File "C:\Python34\lib\site-packages\py2exe\runtime.py", line 164, in analyze
mf.import_hook(modname)
File "C:\Python34\lib\site-packages\py2exe\mf3.py", line 120, in import_hook
module = self._gcd_import(name)
File "C:\Python34\lib\site-packages\py2exe\mf3.py", line 273, in _gcd_import
raise ImportError('No module named {!r}'.format(name), name=name)
ImportError: No module named 'clr'
I also read/tried these:
https://docs.python.org/2/distutils/setupscript.html
https://pythonhosted.org/setuptools/setuptools.html
http://sourceforge.net/p/py2exe/mailman/message/6937658
leading me to move clr.pyd and Python.Runtime.dll into various locations including the location of main.py, C:\Python34\Lib\site-packages (where they were originally) and C:\Python34\Lib\site-packages\py2exe
None of these have worked and I don't know what to try next. I can see that for some reason py2exe can't find either clr.pyd or Python.Runtime.dll or both, but can't see why. Does anyone have any ideas?
Code details
My main.py script looks like this:
import clr
clr.AddReference("name.xxxx")
from name.xxxx import aaa
from clr import System
# All my functioning code, that I've verified works when run from the command line
This is what my setup.py file contains (I've left some bits commented so you can see what I've tried):
from distutils.core import setup
import py2exe, sys, os
mydata_files = []
for files in os.listdir('C:\\d\\Project\\TOOLS\\data_acquisition\\trunk\\DLL'):
f1 = 'C:\\d\\Project\\TOOLS\\data_acquisition\\trunk\\DLL\\' + files
if os.path.isfile(f1): # skip directories
f2 = '.', [f1]
mydata_files.append(f2)
setup(
data_files=mydata_files,
# options = {"py2exe" : {"includes" : "module1,module2,module3"}}
options = {"py2exe": {"includes":["sip", "clr"]}},
# options = {'py2exe': {'bundle_files': 1 , 'compressed': True,"includes":["sip"]}},
#python setup.py py2exe
#CLR.dll and PythonRuntime.dll
# options = {'py2exe': {'bundle_files': 1, "skip_archive":1 ,"includes":["sip"]}},
windows = [{'script': "main.py"}],
# data_files=mydata_files,
# zipfile = None
)
If I change the line options = {"py2exe": {"includes":["sip", "clr"]}}, to options = {"py2exe": {"includes":["sip"]}}, then the .exe builds, but obviously does not function correctly.
###Install description
For reference, I performed standard install of py2exe using pip install py2exe. This puts py2exe into the Lib\site-packages for your python install. Next, I installed pythonnet by downloading the .whl from Christoph Gohlke's unofficial Windows binaries page, then using pip install path\to\pythonnet-2.0.0<version_numbers>.whl. This puts clr.pyd and Python.Runtime.dll into Lib\site-packages for your python install. This question and answers have further info.
###Problem
This is a rather strange behaviour of py2exe that is hard to debug. I think it is purely a bug in that tool. In addition, the error message is not helpful.
The problem is that the module clr is specifically excluded by the tool, via its hooks.py file. It is not clear why. You can see the line that does this exclusion here.
###Solution
The workaround is to delete the word clr from the windows_excludes variable hooks.py file in your py2exe installation. Assuming everything is in its standard place - that means deleting line 23 in the file hooks.py located in C:\Python34\Lib\site-packages\py2exe. you also need to make sure that Python.Runtime.dll is somehow packaged with your .exe - I tested this by adding it to data files. Here is the example that I tested and worked - I used a very simple main.py to illustrate the import and to assure myself that the program was actually working. I left your setup.py as close as possible to your version, commenting out the lines that did not suit my system
To actually make the .exe - use the following (you may not need the path to the python.exe if python is aliased to your Python 3 install)
C:\python34\python.exe setup.py py2exe
###main.py
import clr
# I import clr, but don't use it as this is not my
# expertise. The fact it imports without error means
# I'm pretty sure it will work
with open('out.txt','a') as f:
for i in range(30):
f.write(str(i))
###setup.py
from distutils.core import setup
import py2exe, sys, os
mydata_files = []
# I had to comment these out as they did not apply to my test environment
# for files in os.listdir('C:\\d\\Project\\TOOLS\\data_acquisition\\trunk\\DLL'):
# f1 = 'C:\\d\\Project\\TOOLS\\data_acquisition\\trunk\\DLL' + files
# if os.path.isfile(f1): # skip directories
# f2 = 'dll', [f1]
# mydata_files.append(f2)
# It's essential that the Python.Runtime.dll is packaged with main.exe
# This is how I've done it
mydata_files.append(('.',['C:\\Python34\\Lib\\site-packages\\Python.Runtime.dll']))
setup(
data_files=mydata_files,
# I've left all your commented lines in - they weren't necessary for my test
# options = {"py2exe" : {"includes" : "module1,module2,module3"}}
# I haven't included sip as I don't have it installed, but I think it will work
options = {"py2exe": {"includes":["clr"]}},
# options = {'py2exe': {'bundle_files': 1 , 'compressed': True,"includes":["sip"]}},
#python setup.py py2exe
#CLR.dll and PythonRuntime.dll
# options = {'py2exe': {'bundle_files': 1, "skip_archive":1 ,"includes":["sip"]}},
windows = [{'script': "main.py"}],
# data_files=mydata_files,
# zipfile = None
)
Edit: For anyone interested - I described how I isolated and found this bug in more detail in a chat conversation.

py2app will not include PySide modules

Simple hello world QT python script. Works fine from the command line. When I package it i get:
Traceback (most recent call last):
File "/Users/jquick/bin/dist/gui.app/Contents/Resources/__boot__.py", line 340, in <module>
_run('/Users/jquick/bin/gui.py')
File "/Users/jquick/bin/dist/gui.app/Contents/Resources/__boot__.py", line 336, in _run
execfile(scriptpath, globals(), globals())
File "/Users/jquick/bin/gui.py", line 3, in <module>
from PySide.QtCore import *
ImportError: No module named PySide.QtCore
2012-06-02 00:23:04.823 gui[4835:707] gui Error
So it sounds like its not including the module.. but ive tried including it in both the setup.py and the command line. nothing works :(
setup.py:
"""
This is a setup.py script generated by py2applet
Usage:
python setup.py py2app
"""
from setuptools import setup
APP = ['gui.py']
DATA_FILES = []
OPTIONS = {'argv_emulation': True, 'includes': ['PySide.QtCore', 'PySide.QtGui']}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
i've tried creating with both the --alias option and without. Even tried labeling them as packages. But nothing I do seems to include them.
Can Python find PySide.QtCore? At the command line type:
from PySide.QtCore import *
If (1) works then make sure the Python version your invoking when executing py2app at the command line is the same Python version you're using at step (1). Some operating systems such as Mac OS X come installed with an older version of Python, and if your application works correctly when invoking it at the command line, then be sure you're not invoking a totally different version of python when trying to build your app.

Categories

Resources