我正在尝试使用pyinstaller创建一个可执行文件,其中包含可在任何计算机上运行的文件(json,徽标,驱动程序)。
我通过这一行在可执行文件中包含了几个文件-
pyinstaller.exe --onefile --add-data "jsonfile.json;." --add-data "chromedriver.exe;." --add-data "logo1.ico;." --windowed --icon=logo1.ico script.py
成功但 当我在另一台计算机上打开它时,它不起作用。
此外,我从计算机上的当前目录移动了文件,但也无法正常工作-与我假定的路径有关。
此代码用于包含的任何文件的路径-
def resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
为此功能,我的计算机上的路径已发送。 任何想法出了什么问题吗?
答案 0 :(得分:0)
4件事,它们都不能真正解决您的问题,但可能有帮助
--add-data "data/jsonfile.json;./data/jsonfile.json"
os.startfile(sys._MEIPASS)
之类的方法打开文件夹并查看该文件夹(请注意,您应该睡一会儿或暂停一下,因为它会在程序崩溃或关闭时清理文件)resource_path("data/myfile.json")