Imported module not found in PyInstaller
Asked Answered
I

13

22

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?

Infection answered 27/2, 2013 at 14:34 Comment(0)
Q
34

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

Quadruple answered 27/2, 2013 at 14:35 Comment(1)
Yes, this helped. I used this --paths option on the pyi-makespec command which appears to create the .spec file with the pathex=['D:\env\Lib\site-packages'] in the analysis section of the .spec file.Sexagenarian
L
10

In my case I was trying to import a folder that I created, and ended up here. I solved that problem by removing __init__.py from the main folder, keeping the __init__.py in the subfolders that I was importing.

Latinity answered 4/8, 2022 at 19:3 Comment(0)
M
5

In my case, I had to delete all folders and files related to pyinstaller in my directory, i.e. __pycache__, build, dist, and *.spec. I re-ran the build and the exe worked.

Mangum answered 27/5, 2020 at 15:28 Comment(0)
L
4

This sounds like a job for hidden imports (only available in the latest builds).

From the docs

a = Analysis(['myscript.py'], 
             hiddenimports = ['_socket'], 
             <and everything else>)
Ljoka answered 5/3, 2013 at 18:15 Comment(0)
M
4

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  
Manana answered 4/1, 2021 at 19:52 Comment(2)
You are presuming they are using a virtual environmentKaisership
At least, I am giving a possible solution.Manana
D
4

just delete the '__pycache__' directory then run your exe file again. It worked out for me

Dismiss answered 3/8, 2021 at 14:15 Comment(1)
Thank it helped me. I needed to delete __pycache__ AND build again AND add --hidden-import option to my build script.Acidophil
E
3

Another "In my case" post.

pypdfium2 (an import in my file that I want to convert to an .exe) has a .dll that it calls called pdfium. pyinstaller doesn't import that .dll when you go to build the .exe by default.

Fix:

I think you can do the option --collect-all pypdfium2 ,but at least for me --add-data "C:\Program Files\Python39\Lib\site-packages\pypdfium2\pdfium.dll";. (The "." at the end is intentional and needed!) got the job done.

Eider answered 10/9, 2022 at 17:30 Comment(0)
T
2

You can add the path to your application spec file.

In the Analysis object you can specify pathex=['C:\Python27\libs\', 'C:\Python27\Lib\site-packages'], and any other path ...

Note that if the path is not found there is no problem ... I have paths from linux as well in there.

Tootle answered 5/3, 2013 at 15:6 Comment(0)
H
0

None of the above answers worked for me, but I did get it to work. I was using openpyxl and it required jdcal in the datetime.py module. None of the hidden imports or any of those methods helped, running the exe would still say jdcal not found. The work-around that I used was to just copy the few functions from jdcal directly into the datetime.py in the openpyxl code. Then ran pyinstaller -F program.py

and it worked!

Hairspring answered 21/10, 2015 at 13:52 Comment(2)
what a coincidence i am also using openpyxl, can you please clarify your solution more.Renfroe
this has been so long ago, I don't remember!Endocardium
U
0

Had similar issues. Here's my fix for PyQt5, cffi, python 3.4.3:

This fixes the 'sip' not found error and the '_cffi_backend' one if that comes up:

# -*- mode: python -*-

block_cipher = None


a = Analysis(['LightShowApp.py'],
             pathex=['c:\\MyProjects\\light-show-editor-36',
             'c:\\Python34\\libs\\', 'c:\\Python34\\Lib\\site-packages'],
             binaries=None,
             datas=None,
             hiddenimports=['sip', 'cffi'],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name='LightShowApp',
          debug=False,
          strip=False,
          upx=True,
          console=True )

Look at 'pathex' and 'hiddenimports' above. Those are the only changes from default generated. Build exe with:

pyinstaller LightShowApp.spec -F

I ran that outside of venv or pip-win - whateverTF that crap is for!

Untrimmed answered 19/4, 2016 at 20:32 Comment(0)
L
0

The executor does not know the location of the library, "C:\Python27\Lib\site-packages" etc. Thus, pyinstaller binds the module locations when creating the executable. Therefore, you need to import all the modules, you have used into your program.

Import the "_socket" module in your main file and recompile using pyinstaller.

I would probably work.

Note: But the versions of the modules installed in your system and used in the program must be compatible.

Liberticide answered 30/10, 2020 at 18:10 Comment(0)
T
0

I found out that if you make the setup.py for your code and run python setup.py install and then python setup.py build the pyinstaller will be able to find your packages. you can use the find_packages function from setuptools in your setup.py file so it automatically includes everything.

Toxin answered 4/12, 2022 at 16:14 Comment(1)
This works because once installed the package exists in site-packages and thus available to pyinstaller.Bema
M
0

I was using a conda environment, and in my case the module scipy.io couldn't be found when running my pyinstaller executable. In my code I had import scipy. Changing this to from scipy import io fixed the issue.

Monto answered 6/10, 2023 at 19:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.