New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5.1: ImportError: No module named 'encodings' #324

Closed
ehlers opened this Issue Nov 9, 2017 · 17 comments

Comments

Projects
None yet
6 participants
@ehlers

ehlers commented Nov 9, 2017

Linux Debian 9.2 (Stretch) and Alpine 5.6.2

Previous versions are running fine, but cx_Freeze v5.1 doesn't work for me. The freeze phase looks good, but when starting the binary, it aborts with

Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'

Here the most simple example, it works fine in cx_Freeze v5.0.2:

root@debian:~# echo 'print("Hello World")' > hello.py
root@debian:~#
root@debian:~# python3 -V
Python 3.5.3
root@debian:~# pip3 install cx_Freeze==5.1
Collecting cx_Freeze==5.1
Installing collected packages: cx-Freeze
Successfully installed cx-Freeze-5.1
root@debian:~# cxfreeze --silent hello.py
Missing modules:
? __main__ imported from bdb, pdb
? _dummy_threading imported from dummy_threading
? _frozen_importlib imported from importlib, importlib.abc
? _frozen_importlib_external imported from importlib, importlib._bootstrap, importlib.abc
? _winapi imported from subprocess
? _winreg imported from platform
? ce imported from os
? java.lang imported from platform
? msvcrt imported from subprocess
? nt imported from ntpath, os, shutil
? org.python.core imported from copy
? os.path imported from os, pkgutil, py_compile, unittest, unittest.util
? vms_lib imported from platform
? winreg imported from mimetypes, platform
This is not necessarily a problem - the modules may not be needed on this platform.

Copying data from package collections...
Copying data from package email...
Copying data from package encodings...
Copying data from package html...
Copying data from package http...
Copying data from package importlib...
Copying data from package logging...
Copying data from package pydoc_data...
Copying data from package unittest...
Copying data from package urllib...
Copying data from package xml...
root@debian:~# dist/hello
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'

Current thread 0x00007fafa406cb40 (most recent call first):
Aborted
root@debian:~#
@johncf

This comment has been minimized.

johncf commented Nov 10, 2017

I had a similar problem in Windows. For me, it was codecs module. I solved it by extracting codecs.pyc from lib/library.zip directly into lib/ (along with _weakrefset.pyc, abc.pyc and io.pyc to finally get it to work).

@johncf

This comment has been minimized.

johncf commented Nov 10, 2017

Oh, I just found out that this was fixed in v6. Currently it's in beta. So do pip install cx-Freeze==6.0b1 to upgrade to the beta version.

@anthony-tuininga

This comment has been minimized.

Owner

anthony-tuininga commented Nov 11, 2017

Yes, this was corrected in 6.x and is now backported to the 5.x branch as well. Please confirm.

@ehlers

This comment has been minimized.

ehlers commented Nov 12, 2017

That's better, but still not usable. The basic hello.py is working now.

But when I use it with some simple python script, I get ImportError: No module named '__startup__'.

I'm using this setup.py with the python scripts at https://github.com/ehlers/IOUtools:

from cx_Freeze import setup, Executable
setup (
    options = { "build_exe": { "build_exe": "/usr/local/bin"} },
    executables = [ Executable("iou_import"), Executable("iou_export") ]
)

This setup works fine with 5.0.2 and 6.0b1.

@anthony-tuininga

This comment has been minimized.

Owner

anthony-tuininga commented Nov 18, 2017

I just tried this myself and it worked perfectly -- with both Python 2.7 and 3.6. Can you double check your environment?

@ehlers

This comment has been minimized.

ehlers commented Nov 19, 2017

I'm building it in a fresh docker container with debian 9.1 (stretch), here my log:
cx_Freeze-v5.x_log.txt

I'm always getting this ImportError: No module named '__startup__' error.

In the same environment, only using 5.0.2 and 6.0b1 it works fine.

@anthony-tuininga

This comment has been minimized.

Owner

anthony-tuininga commented Nov 19, 2017

Hmm, can you provide the docker command to get the docker container you are using?

Also, since you're running Python 3.5, can you comment out the PyRun_SimpleString command at line 247 in source/bases/Common.c? Perhaps that is causing problems in your configuration -- and that line should not be needed in Python 3, now that I have backported the code to set the path directly in C.

@ehlers

This comment has been minimized.

ehlers commented Nov 19, 2017

I'm starting the docker container with docker run -ti debian:stretch, then I'm issuing the following commands:

apt-get update
apt-get install python3-dev python3-pip zlib1g-dev git wget vim-tiny

cd
git clone https://github.com/anthony-tuininga/cx_Freeze.git
cd cx_Freeze
git checkout v5.x
vi source/bases/Common.c
python3 setup.py install
cd ..

wget https://raw.githubusercontent.com/ehlers/IOUtools/master/iou_export
wget https://raw.githubusercontent.com/ehlers/IOUtools/master/iou_import
cat > setup.py <<'EOF'
from cx_Freeze import setup, Executable
setup (
    options = { "build_exe": { "build_exe": "/usr/local/bin"} },
    executables = [ Executable("iou_import"), Executable("iou_export") ]
)
EOF

python3 setup.py build
iou_import

Your suggestion to comment out the PyRun_SimpleString command is very helpful, now it works. But isn't that needed for python 2?

@anthony-tuininga

This comment has been minimized.

Owner

anthony-tuininga commented Nov 20, 2017

That is indeed needed for Python 2, but it isn't for Python 3. I just pushed code to eliminate that for Python 3. I also used the docker image you suggested and tried with Python 2. It doesn't work with the same issue you noted for Python 3 (which does now work with the change I made). I'll look into that.

@kevinkjt2000

This comment has been minimized.

kevinkjt2000 commented Dec 9, 2017

I am seeing this error with Python 3 as well

└─▪ build/exe.linux-x86_64-3.6/mc-server-status-bot
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007f23368501c0 (most recent call first):
Aborted (core dumped)
└─▪ pip freeze | grep cx
cx-Freeze==5.1

Upgrading to 6.0b1 fixes this though.

@anthony-tuininga

This comment has been minimized.

Owner

anthony-tuininga commented Dec 9, 2017

I just pushed a change through to the v5.x branch that should resolve this issue for everyone. I have confirmed that it works as expected on Debian, Fedora and Windows. Please confirm. Thanks!

@kevinkjt2000

This comment has been minimized.

kevinkjt2000 commented Dec 10, 2017

5.1.1 on the v5.x branch seems to fix the issue.

@anthony-tuininga

This comment has been minimized.

Owner

anthony-tuininga commented Dec 10, 2017

Thanks for confirming that for me. I'll make a 5.1.1 release next week with this fix included.

@ehlers

This comment has been minimized.

ehlers commented Dec 10, 2017

Tested both with Debian and Alpine, works fine. Thanks a lot.

@anthony-tuininga

This comment has been minimized.

Owner

anthony-tuininga commented Dec 10, 2017

Thanks for confirming as well. New release coming up soon!

@patrickelectric

This comment has been minimized.

patrickelectric commented Mar 25, 2018

I'm using cx_freeze 5.1-1 and having the same problem.
Build output: http://ix.io/12yO

./main 
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007fab9b396040 (most recent call first):
[1]    19750 abort (core dumped)  ./main
@chetanjain2099

This comment has been minimized.

chetanjain2099 commented Aug 11, 2018

My cx_freeze build is working fine. But after build I get an error like this ( I am using cx_Freeze 5.1.1 ) :
Fatal Python error: Py_Initialize: unable to load the file system codec
Traceback (most recent call last):
File "C:\Python36\lib\encodings_init_.py", line 31, in
File "", line 971, in _find_and_load
File "", line 951, in _find_and_load_unlocked
File "", line 894, in _find_spec
File "", line 1157, in find_spec
File "", line 1131, in _get_spec
File "", line 1112, in _legacy_get_spec
File "", line 441, in spec_from_loader
File "", line 544, in spec_from_file_location
zlib.error: Error -3 while decompressing data: invalid code lengths set

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment