简体   繁体   English

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

[英]Why am I getting Pyinstaller FileNotFoundError: [Errno 2] No such file or directory:

I am trying to create a standalone app from my Python script login.py although I am getting the error when running it:我正在尝试从我的 Python 脚本login.py创建一个独立的应用程序,尽管运行它时出现错误:

I have changed the login.spec and added the datas list as at the official documentation Using Spec Files我已经更改了login.spec并添加了数据列表,如官方文档Using Spec Files

/Users.../ is just the summarised path to make it readable here, it is complete in my file. /Users.../只是使其在此处可读的汇总路径,它在我的文件中是完整的。

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

Command to create the bundle:创建捆绑包的命令:

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]

That means that the .jpg and .png are not bundled.这意味着.jpg.png没有捆绑在一起。

login.py登录.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()

login.spec登录规范

# -*- 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)

[DUPLICATED] [重复]

I found the answer here Bundling data files with PyInstaller (--onefile)我在这里找到了答案Bundling data files with PyInstaller (--onefile)

The problem is: pyinstaller unpacks data into a temporary folder问题是: pyinstaller 将数据解压到一个临时文件夹

Just include the function:只需包括 function:

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)

and reference it in: load = Image.open(curPath + "/liberty.jpg")并在其中引用它: load = Image.open(curPath + "/liberty.jpg")

changed to: load = Image.open(resourcePath("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)

Output # in development Output # 开发中

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

Output # # in production Output ##生产中

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PyInstaller - FileNotFoundError: [Errno 2] 没有这样的文件或目录 - PyInstaller - FileNotFoundError: [Errno 2] No such file or directory PyInstaller + UI 文件 - FileNotFoundError: [Errno 2] 没有这样的文件或目录: - PyInstaller + UI Files - FileNotFoundError: [Errno 2] No such file or directory: 获取“FileNotFoundError: [Errno 2] 没有这样的文件或目录” - Getting "FileNotFoundError: [Errno 2] No such file or directory" 我收到 FileNotFoundError: [Errno 2] No such file or directory: 'user.txt'。 我怎么称呼它不同? - I am getting FileNotFoundError: [Errno 2] No such file or directory: 'user.txt'. How can I call it differently? 为什么FileNotFoundError:[错误2]没有这样的文件或目录? - Why FileNotFoundError: [Errno 2] No such file or directory? 当我运行`python ./train.py`时,为什么在终端中收到“FileNotFoundError: [Errno 2] No such file or directory:” - Why am I receiving ' FileNotFoundError: [Errno 2] No such file or directory:' in the terminal when i run `python ./train.py` 在存在的文件上获取“FileNotFoundError:[Errno 2] No such file or directory” - Getting “FileNotFoundError: [Errno 2] No such file or directory” on a file that exists 为什么 FileNotFoundError: [Errno 2] 如果我在 Python 中有它,则没有这样的文件或目录? - Why FileNotFoundError: [Errno 2] No such file or directory if I do have it in Python? Pyinstaller &amp; Matplotlib。 如何解决: FileNotFoundError: [Errno 2] No such file or directory:..? - Pyinstaller & Matplotlib. How to solve: FileNotFoundError: [Errno 2] No such file or directory:..? PyInstaller 错误:FileNotFoundError:[Errno 2] 没有这样的文件 ot 目录:'myKivyfile.kv' - PyInstaller Error: FileNotFoundError: [Errno 2] No such file ot directory: 'myKivyfile.kv'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM