为什么我收到 Pyinstaller FileNotFoundError: [Errno 2] No such file or directory:

时间:2021-01-30 20:25:27

标签: python tkinter pyinstaller

我正在尝试从我的 Python 脚本 login.py 创建一个独立的应用程序,尽管我在运行它时遇到了错误:

我已经更改了 login.spec 并添加了官方文档中的数据列表 Using Spec Files

/Users.../ 只是为了使其在此处可读的汇总路径,它在我的文件中是完整的。

datas=[ ('/Users.../tiktok/*.png', '.' ) , ('/Users/.../tiktok/*.jpg', '.' ) ],

创建包的命令:

<块引用>

pyinstaller -D -F -w login.spec login.py

Traceback (most recent call last):
  File "login.py", line 126, in <module>
  File "login.py", line 82, in main
  File "PIL/Image.py", line 2904, in open
FileNotFoundError: [Errno 2] No such file or directory: '/liberty.jpg'
[53518] Failed to execute script login
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

[Process completed]

这意味着 .jpg.png 没有捆绑在一起。

login.py

from tkinter import *
from PIL import Image, ImageTk
import requests
import json

def main():
    global rootLogin
    rootLogin = Tk()

    rootLogin.geometry("720x540")
    rootLogin.eval('tk::PlaceWindow . center')
    rootLogin.title("Login | bla bla bla")

    # Logo
    load = Image.open(curPath + "/liberty.jpg")
    load = load.resize((136, 84), Image.ANTIALIAS)
    render = ImageTk.PhotoImage(load)
    img = Label(rootLogin, image=render)
    img.image = render
    img.place(x=293, y=86)

    # Username
    ...
    # Password
    ...

    # Forgot password
    ...

    # Login
    ...

    rootLogin.mainloop()

if __name__ == '__main__':
    import os
    curPath = os.path.dirname(__file__)
    print(curPath)
    main()

登录规范

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None

a = Analysis(['login.py'],
             pathex=['/Users/.../tiktok'],
             binaries=[],
             datas=[ ('/Users.../tiktok/*.png', '.' ) , ('/Users/.../tiktok/*.jpg', '.' ) ],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='login',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          runtime_tmpdir=None,
          console=False )
app = BUNDLE(exe,
             name='login.app',
             icon=None,
             bundle_identifier=None)

1 个答案:

答案 0 :(得分:0)

[重复]

我在这里找到了答案Bundling data files with PyInstaller (--onefile)

问题是: pyinstaller 将数据解压到一个临时文件夹

只包含函数:

def resourcePath(relativePath):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        basePath = sys._MEIPASS
    except Exception:
        basePath = os.path.abspath(".")

    return os.path.join(basePath, relativePath)

并在:load = Image.open(curPath + "/liberty.jpg")

中引用它

改为:load = Image.open(resourcePath("liberty.jpg"))

# Logo
load = Image.open(resourcePath("liberty.jpg"))
load = load.resize((136, 84), Image.ANTIALIAS)
render = ImageTk.PhotoImage(load)
img = Label(rootLogin, image=render)
img.image = render
img.place(x=293, y=86)

输出 # 开发中

>>> resourcePath("liberty.jpg")
"/Users/marcelo/.../tiktok/liberty.jpg"

输出 ## 生产中

>>> resourcePath("liberty.jpg")
"/var/folders/yg/z47pdjvx3jg1y2f_58xmpd3m0000gn/T/_MEI4qD8I6/liberty.jpg"