python执行exe时提示找不到文件,python PyInstaller创建的可执行文件无法运行exe错误:找不到Temp\\_MEI175682(数据文件)...

Using PyInstaller created an exe for my script and while running the exe it throws the following error where as if i run the .py file no issues found.

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\my_username\\AppData\\Local\\Temp\\_MEI175682\\resources\\template.json'

[13396] Failed to execute script my_script

I've supplied all the proper flags for the data to be included by PyInstaller and the creation of the exe passed successfully.

I have used --add-data flag and I have used data field in the spec file.

[Edit this line and add exact command you have given for creating exe]

Did deepdive in the error given directory path where no such directory or file exist. Could not locate Temp\_MEI175682

'C:\\Users\\my_username\\AppData\\Local\\Temp\\_MEI175682\\resources\\template.json'

The pyinstaller is intalled in .\scripts\ directory ;why pyinstaller is looking the .json file in the above directory where the file does not exist.

Also the file(template.json') is there in the .\scripts\template.json directory.

To solve this issue what is the best way ?

1.Should something be added in the environment variable to resolve this?

2.Should i create single file .exe by running the script from .\scripts\pyinstaller folder only ?

3. Supplying any details to .spec file or adding certain details to .spec file after pyinstaller creating it would help solving this problem?

Any help that would solve the problem with details is much appreciated.Thanks in advance.

Thanks in advance.

解决方案

This will be the easy and clean solution,Check and like and vote.

When you're encountering following error.

IOError: [Errno 2] No such file or directory: 'C:\\Users\\username\\AppData\\Local\\Temp\\_MEI502322\\resources\\template.json'

This solution tested under Python 2.7.10 and Pyinstaller 3.0.

Put this file to your script folder.

where your .py script available example 123.py

Add this hook to your distribution it means add this in below in new file, example xyx.py and keep this file where the 123.py file available

from PyInstaller.utils.hooks import collect_data_files

#Instruct pyinstaller to collect data files from resources package.

datas = collect_data_files('resources')

since this is the error C:\Users\my_username\AppData\Local\Temp\_MEI175682\resources\template.json "resources" is the package where from you have to take the data file.

Now run the following in command window from your 123.py folder

pyinstaller --onefile --additional-hooks-dir=. 123.py

exe will be created run the exe and check it is taking the data file.

thanks to Panda1100

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值