cx_freeze: ModuleNotFoundError and program stopped working - python

I have a console mode program that I would like to distribute with cx_Freeze. I have just downloaded python 3.6.3 (both amd64 and win32 version) on a newly installed win 10 machine. I have installed cx_Freeze 5.1 from wheels (not from pypi). Then I have built both amd64 and win32 versions of the program. If I try to execute that exe file on the same machine where I have built it, I get this error:
Fatal Python error: Py_Initialize: unable to load the file system codec
Traceback (most recent call last):
File "C:\Program Files\Python36\lib\encodings\__init__.py", line 31, in <module>
ModuleNotFoundError: No module named 'codecs'
I have the same error on both architectures. The setup file looks like this:
#!/usr/bin/env python3
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {
"packages": [
"os", "io", "copy", "struct", "hashlib", "random",
"urllib", "pycurl", "json", "multiprocessing",
"cryptography",
"tornado", "watchdog", "pathtools"
],
"includes": [
"cryptography", "urllib.parse",
],
"include_files": ["tmp", "server.ini", "client.ini"],
"excludes": ["tkinter"],
}
setup(
name="BlindBackup",
version="1.0",
description="BlindBackup",
options={"build_exe": build_exe_options},
executables=[
Executable("backup.py", base=None),
Executable("bsync.py", base=None),
Executable("server.py", base=None),
]
)
In other words, I have specified the "codecs" package explicitly.
What is wrong?
UPDATE There is a file called library.zip in the distribution dir. It contains codecs.pyc but the exe file does not see it. If I extract all files from that zip, then I get a different error message:
C:\Python\Projects\blindbackup\build\exe.win-amd64-3.6>backup.exe
Traceback (most recent call last):
File "C:\Program Files\Python36\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 14, in run
module.run()
File "C:\Program Files\Python36\lib\site-packages\cx_Freeze\initscripts\Console.py", line 22, in run
importer = zipimport.zipimporter(os.path.dirname(os.__file__))
zipimport.ZipImportError: not a Zip file
I guess that the problem is with the code that tries to import modules from the library.zip file? But not sure how to fix it.

The solution was this:
install cx_freeze 6, because version 5 does not support python 3.6 (apparently)
install pywin32 (although it was not required for my program)
add all missing pyd files to "includes" and all missing packages to "packages" section it the setup script.

Related

py2exe missing distutils modules in virtualenv

I am trying to build executable file of my project(Python 2.7.5) but getting this below error when trying to run the executable.
ERROR \ProjectPython\library.zip\distutils\__init__.py:14: UserWarning: The virtualenv
distutils package at %s appears to be in the same location as the system distutils?
Traceback (most recent call last):
File "main.py", line 130, in <module>
...
File "pandas\compat\__init__.pyo", line 32,in <module>
File "zipextimporter.pyo", line 82, in load_module
File "distutils\__init__.pyo", line 25, in <module>
ERROR ImportError: cannot import name dist
After searching for solutions to this issue, I got to know that this must be because of distutils in virtualenv is different from whats in Python installation Lib directory. So, I have been trying to exclude/remove distutils in virtualenv and add distutils package of my origin interpreter during executable build.
That exact solution exists for cxfreeze, but couldn't find alternate solution for py2exe. My setup.py options looks something like
distutils.core.setup(
options = {"py2exe": {
"compressed": 1,
"optimize": 1,
"bundle_files": 1,
"packages": ['psycopg2', 'lxml', 'sqlalchemy', 'openpyxl', 'pandas', 'numpy']
}}
Any help on this is much appreciated. Thanks !
Given the information you've shared, it seems that you might be going the wrong direction with excluding distutils. The error (warning) is stating that distutils package at %s appears to be in the same location as the system distutils.
Try creating a fresh virtual environment with the needed libraries and building again.

PyCharm: importing cython module that uses dynamic libraries itself - ImportError & Code Completion

I've got a cython module that is a wrapper around a c++ library that communicates with a database. That library is called libpersistence.so. That library itself is dynamically linked with libhiredis.so.
I wrote a cython wrapper around the libpersistence.so to create a py_persistence.cpython-35m-x86_64-linux-gnu.so file to import as a module in my python scripts to use the cpp persistence layer.
However, upon importing the py_persistence module there's two issues in PyCharm:
Importing the Persistence class from the module does not get recognized by PyCharm. This also means there is no code completing
Running my tests leads to an ImportError where the libpersistence.so file shared object file can not be opened.
I use the following directory structure.
top_level/
indexing_service/
bin/
libhiredis.so.0.13
libpersistence.so
kCore/
include/
hiredis/
persistence/
query_service/
__init__
runpython.sh
py_persistence/
__init__
py_persistence.cpython-35m-x86_64-linux-gnu.so
py_persistence.pxd
py_persistence.pyx
setup.py
setup.sh
test/
test_persistence.py
The setup.sh file just specifies python3 setup.py build_ext --inplace
The setup.py file is the following:
from distutils.core import setup
from Cython.Distutils import build_ext, Extension
setup(
cmdclass = { 'build_ext' : build_ext },
ext_modules = [
Extension(
'py_persistence',
sources=['py_persistence.pyx'],
libraries=['persistence'],
language='c++',
extra_link_args=['-L../../indexing_service/bin/'],
runtime_library_dirs=['../../indexing_service/bin/'],
extra_compile_args=[
'-std=c++14',
'-I../../indexing_service/kCore/include/persistence',
'-I../../indexing_service/kCore/include/hiredis'
],
cython_directives={
'c_string_type':'unicode',
'c_string_encoding':'utf8',
},
)],
)
So when building the cython py_persistence library it links to the both the libpersistence.so and the libhiredis.so.0.13 files for dynamic linking. This works, this compiles and it builds the py_persistence.cpython-35m-x86_64-linux-gnu.so file correctly.
When I run my tests using the runpython.py helper script I wrote everything works perfectly fine.
The runpython.py explicitely adds the required directories to the runtime library path:
export PYTHONPATH=$PYTHONPATH:.
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:../indexing_service/bin/:.
python3 ${#:1}
Running the tests:
./runpython.sh -m unittest discover py_persistence/test -v
This works perfectly.
However getting this setup working so that I can run it from PyCharm is giving me a lot of trouble. I don't seem to be able to correctly specfify the paths. In the test_persistence.py file I get an ImportError while importing the py_persistence library.
Import code:
from py_persistence.py_persistence import Persistence
The error:
Error
Traceback (most recent call last):
File "/usr/lib/python3.5/unittest/case.py", line 58, in testPartExecutor
yield
File "/usr/lib/python3.5/unittest/case.py", line 600, in run
testMethod()
File "/usr/lib/python3.5/unittest/loader.py", line 34, in testFailure
raise self._exception
ImportError: Failed to import test module: test_persistence
Traceback (most recent call last):
File "/usr/lib/python3.5/unittest/loader.py", line 428, in _find_test_path
module = self._get_module_from_name(name)
File "/usr/lib/python3.5/unittest/loader.py", line 369, in _get_module_from_name
__import__(name)
File "/home/tom/work/dev/alexandria.works/query_service/py_persistence/test/test_persistence.py", line 3, in <module>
from py_persistence.py_persistence import Persistence
ImportError: libpersistence.so: cannot open shared object file: No such file or directory
I've already tried adding the indexing_service/bin/ folder to the path in PyCharm for the virtual environment:
Interpreter Paths in PyCharm
The run configuration used to run the tests is the following:
Run Configuration
The problems I'm experiencing are the ImportError and the fact that code completion isn't working.
I'm at a loss about what else to try here... Printing sys.path in the test_persistence.py file tells me that the indexing_service/bin/ folder is on the path.
['/home/tom/Applications/pycharm-2017.1.1/helpers/pycharm',
'/home/tom/work/dev/alexandria.works/query_service/py_persistence/test',
'/home/tom/work/dev/alexandria.works/indexing_service/bin',
'/home/tom/work/dev/alexandria.works/query_service',
'/home/tom/Applications/pycharm-2017.1.1/helpers/pycharm',
'/home/tom/work/dev/alexandria.works/query_service/dev-env/lib/python35.zip',
'/home/tom/work/dev/alexandria.works/query_service/dev-env/lib/python3.5',
'/home/tom/work/dev/alexandria.works/query_service/dev-env/lib/python3.5/plat-x86_64-linux-gnu',
'/home/tom/work/dev/alexandria.works/query_service/dev-env/lib/python3.5/lib-dynload', '/usr/lib/python3.5',
'/usr/lib/python3.5/plat-x86_64-linux-gnu',
'/home/tom/work/dev/alexandria.works/query_service/dev-env/lib/python3.5/site-packages']
Anybody know what I missed?

How to package psutil with py2exe?

The app is working fine on my development win8 environment, but when it is packaged with py2exe and run on the production machine it throw the exception:
"The procedure entry point RtlIdnToAscii could not be located in the dynamic link library ntdll.dll"
The detail content of the log file is
Traceback (most recent call last):
File "DataviewerBackupRestorer.py", line 6, in <module>
File "RestorController.pyc", line 7, in <module>
File "psutil\__init__.pyc", line 136, in <module>
File "psutil\_psmswindows.pyc", line 14, in <module>
File "_psutil_mswindows.pyc", line 12, in <module>
File "_psutil_mswindows.pyc", line 10, in __load
ImportError: DLL load failed: The specified procedure could not be found.
It seems that a dll required by psutil is missing during the package process. I have tried to add the py2exe options with
py2exe_options = {"py2exe":{"includes":['decimal', '_mssql', 'uuid', 'psutil', '_psutil_mswindows']}}
But it is not working. Any ideas? Thanks in advance!
The solution is to remove System DLLs from the project directory. When I added psutil to my application py2exe added a lot of system DLLs to my project. It worked correctly on my and some other computers, but failed on another one. Removing from the project the .dll files that were available in C:\Windows\System32 solved the issue.
Finally in my case the solution was to add:
'dll_excludes': [ "IPHLPAPI.DLL", "NSI.dll", "WINNSI.DLL", "WTSAPI32.dll"]
into the py2exe options in setup.py file.

entry_points path changed in development mode install

My console_scripts/entry_points are not working in "develop" mode install and produce a traceback saying ImportError cannot load the module containing the entry points. Need help understanding how entry_points figure out which module to load and what paths to setup and stuff. Help fixing my error.
I have a setup.py script that has some entry points as follows
entry_points = {'console_scripts':[
'pyjampiler=pyjs.pyjampiler:Builder',
'pyjscompile=pyjs.translator:main',
'pyjsbuild=pyjs.browser:build_script',
]}
my code is organized as
workspace/
setup.py
pyjs/
src/
pyjs/
browser.py
My setup.py makes use of packages and package_dir arguments to setup() in setup.py to make sure the pyjs package gets picked up from pyjs/src/pyjs and so a regular install produces the following console scripts containing the following and it runs fine. it is able to load the module and call the entry point fine.
sys.exit(
load_entry_point('pyjs==0.8.1', 'console_scripts', 'pyjsbuild')()
)
But when I install and run it in development as "python setup.py develop", the install goes fine and I see the egg.lnk files getting created. but executing the console script causes the following errors
localhost:pyjs sarvi$ lpython/bin/pyjsbuild
Traceback (most recent call last):
File "lpython/bin/pyjsbuild", line 8, in <module>
load_entry_point('pyjs==0.8.1', 'console_scripts', 'pyjsbuild')()
File "/Users/sarvi/Workspace/pyjs/lpython/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 318, in load_entry_point
File "/Users/sarvi/Workspace/pyjs/lpython/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 2221, in load_entry_point
File "/Users/sarvi/Workspace/pyjs/lpython/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 1954, in load
ImportError: No module named browser
I suspect it has something to do with sys.path and the directory structure in the development sources. How can I get this to work in "develop" mode install?

py2exe + sqlalchemy + sqlite problem

I am playing around with getting some basic stuff to work in Python before i go into full speed dev mode. Here are the specifics:
Python 2.5.4
PyQt4 4.4.3
SqlAlchemy 0.5.2
py2exe 0.6.9
setuptools 0.6c9
pysqlite 2.5.1
setup.py:
from distutils.core import setup
import py2exe
setup(windows=[{"script" : "main.py"}], options={"py2exe" : {"includes" : ["sip", "PyQt4.QtSql","sqlite3"],"packages":["sqlite3",]}})
py2exe appears to generate the .exe file correctly, but when i execute dist/main.exe i get this in the main.exe.log
Traceback (most recent call last):
File "main.py", line 18, in <module>
File "main.py", line 14, in main
File "db\manager.pyc", line 12, in __init__
File "sqlalchemy\engine\__init__.pyc", line 223, in create_engine
File "sqlalchemy\engine\strategies.pyc", line 48, in create
File "sqlalchemy\engine\url.pyc", line 91, in get_dialect
ImportError: No module named sqlite
I've been googling my heart out, but can't seem to find any solutions to this. If i can't get this to work now, my hopes of using Python for this project will be dashed and i will start over using Ruby... (not that there is anything wrong with Ruby, i just wanted to use this project as a good way to teach myself Python)
you need to include the sqlalchemy.databases.sqlite package
setup(
windows=[{"script" : "main.py"}],
options={"py2exe" : {
"includes": ["sip", "PyQt4.QtSql"],
"packages": ["sqlalchemy.databases.sqlite"]
}})
you need change to sqlalchemy.dialects.sqlite package
setup(
windows=[{"script" : "main.py"}],
options={"py2exe" : {
"includes": ["sip", "PyQt4.QtSql"],
"packages": ["sqlalchemy.dialects.sqlite"]
}})

Categories

Resources