ModuleNotFound Pyinstaller

Question:

I am using:

  • PyInstaller 5.3
  • Python 3.9
  • Windows 11

I have my directories set up like so:

Project
|
+ -- DoSomething
|    |
|    +-- doessomething.py
|
+ -- GUIs
     |
     +--GUI_main.py

The .py files contain the following code:

doessomething.py

def doing():
        print('Doing')

GUI_main.py

from DoSomething.doessomething import *
doing()
  1. I have tried the following commands
pyinstaller --onefile --noconsole GUIs/GUI_main.py
pyinstaller --onefile --noconsole -p /path/to/DoSomething/dir GUIs/GUI_main.py

Where the path to DoSomething is the absolute path on my computer.

  1. The working directory as the project directory
  2. The Working Directory as the GUI directory

The program compiles and runs fine in PyCharm but after packaging it with PyInstaller and running it, I get this error:

Traceback (most recent call last):
  File "GUI_main.py," line 1, in <module>
ModuleNotFoundError: No module named 'DoSomething'
Asked By: Asa

||

Answers:

The short answer is that you’re telling PyInstaller the wrong directory; you want to use -p /path/to/Project instead. This is because your DoSomething folder is inside the root Project directory. You’re telling PyInstaller where to look for modules/packages that you try to import.


For a little more context…

I don’t use PyCharm myself, but it would appear that it’s handling something for you automatically: it’s adding your top-level Project path to your Python Path. This means that when your code attempts to import a module, Project is one of the places it looks for that module name. This is the reason your code works as-is in PyCharm.

If you open a standard terminal, go to Project, and run python GUIs/GUI_main.py, you should find that you get an ModuleNotFoundError. In order to make it work, you can add the proper directory to the Python Path environment variable (for the current session) with:

export PYTHONPATH=/path/to/Project:$PYTHONPATH

After doing this, running the script directly should work. What’s also neat is that PyInstaller respects your Python Path… so you can then run PyInstaller, without specifying any search folders, and it will correctly find your other Python file.


Sidenote: standard practice is to keep Python package/module (essentially folder/file) names all lowercase.

Answered By: CrazyChucky

I figured out my structure is not the smartest. I was able to use pyinstaller without using the full path in the command. Keeping the main GUI python file in the main project folder fixes the issue. Something like this

ProjectFolder
|
+ -- DoSomething
|    | 
|    +-- __init__.py
|    |
|    +-- doessomething.py
|
+ -- GUIs
|    |
|    +--add_on_gui.py
|
+ GUI_main.py

Then in the command line in the project directory

pyinstaller --onefile --noconsole GUI_main.py

This did the trick for me.

Answered By: Asa
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.