自动py-to-to-exe/pyinstaller base_library错误

发布于 2025-02-12 19:14:24 字数 572 浏览 0 评论 0原文

我刚刚完成了一个项目,需要将其转换为可执行文件。我使用自动py-to-exe,它为pyinstaller添加了一个不错的UI,使我的工作更容易。问题在于我的脚本正在使用位于同一目录中的TXT文件(ex:os.path.join(sys.path [0],“ file.txt”)),只是添加<代码> - add-data c:/path/to/file.txt; 。由于我得到“脚本中的“未经例外”)错误,似乎无法正常工作。这是完整的错误消息:

  File "GUI.py", line 141, in <module>
    frame = MyFrame()
  File "GUI.py", line 40, in __init__
    with open(os.path.join(sys.path[0],"file.txt")) as f:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\path\to\\\base_library.zip\\file.txt'

关于如何解决此问题的任何想法?(我必须将其评为代码)

I just finished a project and I need to convert it to a executable file.I'm using auto-py-to-exe which adds a nice UI to pyinstaller so it makes my work easier. The problem is that my scripts are using txt files located in the same directory(ex:os.path.join(sys.path[0],"file.txt")) and just adding by --add-data C:/path/to/file.txt;. doesn't seem to work as I get a "Unhandled exception in script" error. This is the full error message:

  File "GUI.py", line 141, in <module>
    frame = MyFrame()
  File "GUI.py", line 40, in __init__
    with open(os.path.join(sys.path[0],"file.txt")) as f:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\path\to\\\base_library.zip\\file.txt'

Any ideas on how to solve this problem?(I had to comment it as code)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梅倚清风 2025-02-19 19:14:24

刚刚找到了该解决方案,所以我将其发布给任何寻找它的人:似乎当前目录\ current directory \ base_library.zip sys.path [0]将返回该目录而不是可执行文件的目录(类似于类似的目录独立的 - file .exe,相同的解决方案)。该解决方案只是用来替换sys.path [0] sys.path [0])。替换('\ base_library.zip',''),它将允许脚本再次加速当前目录。

Just found the solution so I'm posting it for anyone looking for it:seems like the current directory becomes \current directory\base_library.zip so sys.path[0] will return that instead of the directory the executable resides(something similar for the standalone --onefile .exe,same solution).The solution is simply to replace sys.path[0] with sys.path[0]).replace('\base_library.zip',''),which will allow the script to acces the current directory again.

浅忆流年 2025-02-19 19:14:24

此问题的原因是以下行:

os.chdir(sys.path[0])

或者sys.path [0] ...

我将其删除,一切正常。

The cause of this problem is the line below:

os.chdir(sys.path[0])

Or maybe sys.path[0]...

I removed it, and everything worked fine.

み青杉依旧 2025-02-19 19:14:24

我遇到了同样的问题,但通过更改解决了

os.path.join(sys.path[0],"file.txt")

os.path.join(os.getcwd(),"file.txt")

I had the same issue, but solved it by changing:

os.path.join(sys.path[0],"file.txt")

with

os.path.join(os.getcwd(),"file.txt")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文