Skip to content
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

PyInstaller - No module named 'codecs' - Windows 7 #5730

Closed
qianfeng2 opened this issue Apr 13, 2021 · 12 comments
Closed

PyInstaller - No module named 'codecs' - Windows 7 #5730

qianfeng2 opened this issue Apr 13, 2021 · 12 comments

Comments

@qianfeng2
Copy link

Dear whom it may concern,

My script literally looks like this (Python 3.6.7-embed-win32):

print("Hello world")

Building it with PyInstaller (version 5.0.dev.0) (pyinstaller --onefile hello.py or even pyinstaller hello.py) results in an error:

Fatal Python error: Py_Initialize: unable to load the file system codec
Traceback (most recent call last):
File "encodings_init_.py", line 31, in (module)
ImportError: No module named 'codecs'.

Is there anything I can do on my window 7 computer?

Thanks in advance.

@rokm
Copy link
Member

rokm commented Apr 13, 2021

Is that a build-time or a run-time error?

@bwoodsend
Copy link
Member

Python 3.6.7-embed-win32

The embedded Python builds aren't supported yet. See #4989.

@qianfeng2
Copy link
Author

Is that a build-time or a run-time error?

It's a run-time error. I have build successfully since there is a hello.exe in dist dir. When I run hello.exe, it outputs this error.

@rokm
Copy link
Member

rokm commented Apr 13, 2021

Did you explicitly work around the missing support for collecting files from embedded python3X.zip (#4989)? As Brenainn said, PyInstaller does not support embedded python, and that ends up in build-time error...

@qianfeng2
Copy link
Author

Did you explicitly work around the missing support for collecting files from embedded python3X.zip (#4989)? As Brenainn said, PyInstaller does not support embedded python, and that ends up in build-time error...

Thanks very much for your prompt response.
There is no build-time error in my computer using embeded python version since this website has saved me: https://dev.to/fpim/setting-up-python-s-windows-embeddable-distribution-properly-1081

Now it proceeds to a run-time error:) Is it possible to figure it out why? Alternatively I will then try the python-X.exe.

@rokm
Copy link
Member

rokm commented Apr 13, 2021

There is no build-time error in my computer using embeded python version since this website has saved me: https://dev.to/fpim/setting-up-python-s-windows-embeddable-distribution-properly-1081

Ah, that's a clever trick. :)

Now it proceeds to a run-time error:) Is it possible to figure it out why? Alternatively I will then try the python-X.exe.

Looks like PyInstaller does not construct base_library.zip correctly with this configuration (it ends up empty). As a very quick work-around, you can replace the generated base_library.zip with python36.zip (works only for onedir builds, though).

I'll check what's going on with base library generation...

@rokm
Copy link
Member

rokm commented Apr 13, 2021

Looks like our create_py3_base_library implementation is ignoring compiled modules:

if type(mod) in (modulegraph.SourceModule, modulegraph.Package):

Can you try modifying this file in your local PyInstaller installation, by adding modulegraph.CompiledModule to the above list? Since you're using python 3.6, that might do the trick without additional modifications...

@qianfeng2
Copy link
Author

qianfeng2 commented Apr 13, 2021 via email

@rokm
Copy link
Member

rokm commented Apr 13, 2021

Yes, open python-3.6.7-embed-win32\Lib\site-packages\PyInstaller\depend\utils.py and change that line into:

if type(mod) in (modulegraph.SourceModule, modulegraph.Package, modulegraph.CompiledModule): 

and try rebuilding your application. This time around, base_library.zip should contain collected modules, with codecs.pyc among them.

@bwoodsend
Copy link
Member

There's more broken in embedded Python than just that. There's several places where PyInstaller skips searching modules because it can't find them in the zipped base_library.zip. And I vaguely remember the archive writer not working either for some reason. I had a branch somewhere which I can't seem to find...

@rokm
Copy link
Member

rokm commented Apr 13, 2021

There's more broken in embedded Python than just that. There's several places where PyInstaller skips searching modules because it can't find them in the zipped base_library.zip. And I vaguely remember the archive writer not working either for some reason. I had a branch somewhere which I can't seem to find...

That's true, but as per article linked in #5730 (comment), you can avoid zip-related issues by simply renaming the python36.zip file into something else, and extracting its contents into folder called python36.zip.

@qianfeng2
Copy link
Author

Yes, open python-3.6.7-embed-win32\Lib\site-packages\PyInstaller\depend\utils.py and change that line into:

if type(mod) in (modulegraph.SourceModule, modulegraph.Package, modulegraph.CompiledModule): 

and try rebuilding your application. This time around, base_library.zip should contain collected modules, with codecs.pyc among them.

Haha!!! It works!!! Now, my first hello.exe works very good!!! That's magic! I have spent two weeks struggling to solve this codecs issue.
Many thanks @rokm

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants