简体   繁体   中英

python pyinstaller create single file import directory

i have used pyinstaller to create a single .exe for windows using a single .py file and it worked fine. now i have gotten a bit more complex and have created multiple .py files stored in folders beneath my main.py. i've read through the pyinstaller guides , yet something doesn't seem to be working on my part..

my folder structure in windows is as follows, i'm just using a simple example for illustration :

app_root\main.py
app_root\__init__.py
        \library\__init__.py
        \library\app_ext1.py
        \library\app_ext2.py
        \library\test\__init__.py
        \library\test\app3.py

in main.py i am importing code from

from library.app_ext1 import get_info
from library.app_ext2 import get_data
from library.test.app3 import get_test

so i run pyinstaller to my main.py using my .spec file. in the .spec file i have

hiddenimports=['library']

pyinstaller finishes no errors and creates my single .exe, but when i run my single .exe i get the following error

C:\Users\user1\Desktop\1_file\dist>main.exe
Traceback (most recent call last):
  File "app_root\main.py", line 2, in <module>
ImportError: No module named 'library'
Failed to execute script main

i wrote a test script just using a single .py file and it was doing some simple work with openpyxl (a python excel library) so in my test .py file i had "import openpyxl" , so in my spec file i used

hiddenimports=['openpyxl']

that worked fine but openpyxl is python lib that was installed using pip , i guess i'm not fully understanding how to import my own modules/scripts that i have created and stored in a folder structure beneath my main .py script file that i'm referencing between my .py script files as in my example above.

how can i make pyinstaller recognize my .py scripts i'm importing?

ok not sure why , but i just created a new folder in windows , copied all my .py files using same folder structure as before and then it worked. the only thing missing was all the Pycharm folders like .git, .idea, and pycache

i did notice in troubleshooting that if i renamed library to something else like "test" and updated my import lines it would still reference module "library" when i executed the .exe that was built.

i knew i was doing all this correctly but seems there might have been something in one of the other folders that py charm puts in there.

so i am just going to copy all my files to new folder each time and delete any folders/files i see other than the .py required.

In my case I replaced from library.app_ext1 import get_info to from your_newly_created_folder_name.library.app_ext1 and it worked :D

I noticed app_root name must be changed with new created folder name. just created a new folder, copy all .py files and keep folder structure. I hope you no one else will waste his time like me on this anymore

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM