14

I am using Python 3.3.3 and I have been trying to build a .exe from a simple .py script.

My script looks like this:

import encodings

print('Test')

and executes correctly.

When I try to build it with PyInstaller with this command:

pyinstaller --onefile Testmodul.py

and try to open my .exe it shows up with this error: Fatal Python error: Py_Initialize: unable to load the file system codec, ImportError: No module named 'encodings'

I already tried importing the 'encodings' module in my testscript but it is still not working, I have also tried py2exe and it is also not working at all.

Is there anything I do wrong? Do I have to setup something in my PATH? the correct path to "C:\Python33" is included in there already.

EDIT: To everyone with this problem: I gave up, and after a fresh install of windows and python and all the other stuff, I tried it again, the same way as before and it worked without a problem.. It is worth a try if you are really desperate!

Zesa Rex
  • 412
  • 1
  • 4
  • 16
  • Did you have any idea `where is your SYS encoding data `, how to work python `encodings` ? All OS details in Public usage ? Initialize mean `not registered` . This question related to your `python information level`. So a trick : this code work on idle cos granted a lot permissions and registered(non public) on more service/class . Initialize for which style `permanent/temporary` ? – dsgdfg Feb 18 '17 at 12:51
  • Yes, i know `this is not low_level comment` ! – dsgdfg Feb 18 '17 at 12:53
  • Duplicate of http://stackoverflow.com/questions/5694706/py-initialize-fails-unable-to-load-the-file-system-codec – Alastair McCormack Feb 18 '17 at 22:07

3 Answers3

2

This is probably because pyinstaller did not include the module in the first place. Try one of the following solutions.

1) Specify the path to your module during compilation:

  • pyinstaller --onefile --paths=/path/to/module Testscript.py

2) Specify the path from the .spec file:

  • run this command first(in pyinstaller's directory):

    python Makespec.py --onefile /path/to/yourscript.py
    
  • now you have the .spec file. open it in your text editor, and add the path to your modules to the pathex.

    pathex=['C:\\path\\to\\module']
    
  • then, build your program:

     python Build.py /path/to/yourscript.spec
    

3) Use hidden imports:

  • pyinstaller --onefile --hidden-import=modulename Testscript.py
  • you can also specify hidden-import in the .spec file.
A.Sherif
  • 144
  • 2
  • 8
0

Add hook file with name hook-encodings.py to C:\Python\Lib\site-packages\PyInstaller\hooks location and add following line of code to collect encodings module in hook file

from PyInstaller.utils.hooks import collect_submodules

hiddenimports = collect_submodules('encodings')

this may work, this answer might help you Pyinstaller Error for Djnago project "ImportError: No module named 'django.contrib.admin.apps'"

Community
  • 1
  • 1
Ragini Dahihande
  • 636
  • 2
  • 9
  • 17
0

Which windows version are you using (7 or 10) ?

This issue seems to be relative to user privilege ... and assuming it is similar to this issue, you may first try to run your exe file with administrator privilege, and if it is failling again, try to run "Pyinstaller" from a cmd.exe running with administrator right.

Morse
  • 8,258
  • 7
  • 39
  • 64
A. STEFANI
  • 6,707
  • 1
  • 23
  • 48