Ahm*_*imi 5 python pyinstaller
我正在用 python 构建一个程序,它读取用户提供的 csv 文件。
我正在使用 Pyinstaller 构建一个 MacOS 应用程序。
在 Windows 中,我使用了 CX_freeze,我只需要将 csv 文件放在与 .exe 相同的目录中,程序才能运行。但是使用PyInstaller,即使我将其放在同一目录中也找不到该文件。
那么用户必须把文件放在哪里才能让程序找到它?
该程序是一个非常基本的程序,如下所示:
import pandas as pd
df = pd.read_csv('file.csv')
print(df)
Run Code Online (Sandbox Code Playgroud)
谢谢。
当我使用 Pyinstaller 创建可执行文件时,我必须使用该--add-data参数。
http://pyinstaller.readthedocs.io/en/stable/spec-files.html#adding-data-files
有了这个,您可以为 .csv 等文件指定路径。
如果您正在创建单文件包,Pyinstaller 会在内部重命名路径[docs],因此如果您将 .csv 放在某个文件夹中,则每次访问项目中的文件时都需要执行以下操作:
def resource_path(relative):
#print(os.environ)
application_path = os.path.abspath(".")
if getattr(sys, 'frozen', False):
# If the application is run as a bundle, the pyInstaller bootloader
# extends the sys module by a flag frozen=True and sets the app
# path into variable _MEIPASS'.
application_path = sys._MEIPASS
#print(application_path)
return os.path.join(application_path, relative)
Run Code Online (Sandbox Code Playgroud)