Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Imported module not found in PyInstaller

I'm working in Windows, using PyInstaller to package a python file. But some error is occuring:

Traceback (most recent call last):
  File "<string>", line 2, in <module>
  File "D:\Useful Apps\pyinstaller-2.0\PyInstaller\loader\iu.py", line 386, in importHook
    mod = _self_doimport(nm, ctx, fqname)
  File "D:\Useful Apps\pyinstaller-2.0\PyInstaller\loader\iu.py", line 480, in doimport
    exec co in mod.__dict__
  File "D:\Useful Apps\pyinstaller-2.0\server\build\pyi.win32\server\out00-PYZ.pyz\SocketServer", line 132, in <module>
  File "D:\Useful Apps\pyinstaller-2.0\PyInstaller\loader\iu.py", line 386, in importHook
    mod = _self_doimport(nm, ctx, fqname)
  File "D:\Useful Apps\pyinstaller-2.0\PyInstaller\loader\iu.py", line 480, in doimport
    exec co in mod.__dict__
  File "D:\Useful Apps\pyinstaller-2.0\server\build\pyi.win32\server\out00-PYZ.pyz\socket", line 47, in <module>
  File "D:\Useful Apps\pyinstaller-2.0\PyInstaller\loader\iu.py", line 409, in importHook
    raise ImportError("No module named %s" % fqname)
ImportError: No module named _socket

I know that _socket is in path C:\Python27\libs\_socket.lib, but how can let the generated EXE find that file?

like image 736
Searene Avatar asked Feb 27 '13 14:02

Searene


2 Answers

If you are using virtualenv you should use the "-p" or "--path='D:...'" option. Like this:

pyinstaller.exe --onefile --paths=D:\env\Lib\site-packages  .\foo.py

What this does is generates foo.spec file with this pathex path

like image 180
karantan Avatar answered Sep 18 '22 17:09

karantan


If you are using an virtual environment, then problem is because of environment.

SOLUTION Just activate the environment and run the pyinstaller command. For example, If you are using environment of pipenv then run commands in following order.

pipenv shell # To activate environment

pyintaller --onefile youscript.py # Command to generate executable  
like image 23
Sami Ullah Avatar answered Sep 17 '22 17:09

Sami Ullah