F.K
F.K

Reputation: 11

Import error when using pyinstaller to create a single exe file

I have a python package that involves several python modules. The project has been created in pycharm. I have used pyinstaller to create a single executable file of my python package. When I run the executable using a batch file I get an import error, specifically pandas has failed to be imported. Is there a reason why pyinstaller has not also collected the package dependencies in my virtual environment?

Thanks in advance for any help!

I have the following 2 errors: (1) ModuleNotFoundError: No module named 'pandas._libs.tslibs.np_datetime'

(2) File "site-packages\pandas__init__.py", line 35, in ImportError: C extension: No module named 'pandas._libs.tslibs.np_datetime' not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace --force' to build the C extensions first.

Upvotes: 0

Views: 6151

Answers (1)

Nipun Thennakoon
Nipun Thennakoon

Reputation: 3734

There is one pip script for each virtual environment. So when you install a python module it get installed into the projectname\venv\Lib\site-packages directory.

When you run pyinstaller from terminal to make the executable, pyinstaller checks for dependencies in Sys.path . But that path does not include the projectname\venv\Lib\site-packages directory. Therefore pyinstaller cannot find those particular dependencies. In such cases it gives you warnings.Those warning can be found in 'warnname.txt' near your executable file.

EDIT: How to Configure pycharm to run pyinstaller

  1. First you need to add pyinstaller into project interpreter. enter image description here
  2. Then you need to setup running configurations. enter image description here

    enter image description here

    Script name: path to your python script
    working path: Project location

    leave interpreter options as it is in the image.

  3. Run pyinstaller. You can find your .exe in dist directory.

  4. If the "Module not found" error still persists. You can add a hidden import hook and specify the names of the missing modules.Navigate to

Project Path\venv\Lib\site-packages\PyInstaller\hooks

and create a new "hook-pandas.py"(hook-modulename.py) script and make a list of hidden import modules like this.

hiddenimports = ['pandas._libs.tslibs.np_datetime','pandas._libs.tslibs.nattype','pandas._libs.skiplist']
  1. And run pyinstaller again, and it should work now.

Upvotes: 1

Related Questions