简体   繁体   中英

How to choose file path in python script when using pyinstaller?

I have a python script that takes data from an excel file (database.xlsx). I want to freeze the script (ecobel.py) using pyinstaller.

I'd like to know if in the python script I should use the current path where the database file is or if I should reference where I'm putting it in the executable path. I'm currently just calling the path where I'd normally put the database file on my laptop.

In addition, after entering the right path, I'm currently typing this in the comand prompt:

--onefile --add-data database.xlsx;. ecobel.py

which returns "No such file or directory: 'database.xlsx'" when launching the formed executable file.

I Solved it thanks to this page: How do I include files with pyinstaller?

I put the database.xlsx in a file called database at the same directory level as where ecobel.py is.

I call database.xlsx this way in my ecobel.py script:

import sys
if getattr(sys, 'frozen', False):
    database = xlrd.open_workbook(os.path.join(sys._MEIPASS,'database/database.xlsx'))
else:
    database = xlrd.open_workbook('database/database.xlsx')

Then I type this in the command prompt:

--clean -y -n "ecobel" --add-data="database\database.xlsx;database" ecobel.py

"ecobel" will put the .exe file in a folder called ecobel. Don't know what -y and -n stand for, nor what --clean does, if someone can help.

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