Pyinstaller not importing usercreated modules

24 Views Asked by At

I'm trying to compile my python app. The app has 5 scripts and a data folder.

  1. Three of those scripts are essentially modules
  2. The main file imports the three other scripts and
  3. There's a gui that imports the main script.
  4. The data folder contains CSVs that I'll be using during processing.

I've tested the app and it works fine calling gui.py from the CLI.

But there are two problems:

  1. When I compile the app and run the compiled app, I get an error along the lines of: "failed to execute because no module named generator," which is the name of my main file.

  2. After we solve the first problem, I'm not sure the app is packaging all the extra data I need (the csvs in the data folder.)

One thing at a time, though: how can I get the compiled app to recognize my modules?

I already have an "init.py" file in my directory. What should I do?

Any and all help is appreciated. Thank you.

2

There are 2 best solutions below

1
liaifat85 On

If you're using PyInstaller, you can create a spec file to customize the packaging process. This can help in specifying additional files or directories to include in the bundled application. You can generate a spec file using:

pyi-makespec your_script.py

0
fikunmi On

What I found from another answer is that pyinstall will import your modules if you
1. create a seperate package for them and
2. create a file that just calls your main file outside the package.

The file structure looks a little like this:

/dir
    call_main.py
    /app
        main.py
        script1.py
        script2.py
           .
           .
           .
        scriptn.py

Even after doing this, I still struggled because I had no idea that the module name was the name of the folder.

Running main.py by itself worked fine with

import script1, script2, ..., scriptn

But my .exe couldn't import script1 through n until I specified

from app import script1, script2, ..., scriptn

in the main.py file.

So the key takeaways are:

  1. Create a package for your entire app and then a simple file (or an entire GUI like I did) that calls the main file of your app.
  2. Be careful with module imports.