python - PyInstaller: IOError: [Errno 2] 没有这样的文件或目录:

标签 python pyinstaller

我正在尝试使用 pyinstaller 和 scientific,MMTK 等模块编译一个 python 脚本。 Pyinstaller 无法包含一些 .pyd 模块,所以我手动将它们复制到 dist 文件夹中。当我执行编译后的 exe 时,它​​给了我以下错误:-

C:\Python27\hello\dist\hello>hello.exe
Traceback (most recent call last):
  File "", line 21, in 
  File "C:\Python27\iu.py", line 436, in importHook
    mod = _self_doimport(nm, ctx, fqname)
  File "C:\Python27\iu.py", line 521, in doimport
    exec co in mod.__dict__
  File "c:\Python27\hello\build\pyi.win32\hello\outPYZ1.pyz/visual", line 1, in <module>
  File "C:\Python27\iu.py", line 436, in importHook
    mod = _self_doimport(nm, ctx, fqname)
  File "C:\Python27\iu.py", line 521, in doimport
    exec co in mod.__dict__
  File "c:\Python27\hello\build\pyi.win32\hello\outPYZ1.pyz/visual.visual_all", line 1, in <module>
  File "C:\Python27\iu.py", line 436, in importHook
    mod = _self_doimport(nm, ctx, fqname)
  File "C:\Python27\iu.py", line 521, in doimport
    exec co in mod.__dict__
  File "c:\Python27\hello\build\pyi.win32\hello\outPYZ1.pyz/vis", line 13, in <module>
  File "C:\Python27\iu.py", line 436, in importHook
    mod = _self_doimport(nm, ctx, fqname)
  File "C:\Python27\iu.py", line 521, in doimport
    exec co in mod.__dict__
  File "c:\Python27\hello\build\pyi.win32\hello\outPYZ1.pyz/vis.ui", line 3, in <module>
  File "C:\Python27\iu.py", line 477, in importHook
    mod = self.doimport(nm, ctx, ctx+'.'+nm)
  File "C:\Python27\iu.py", line 521, in doimport
    exec co in mod.__dict__
  File "c:\Python27\hello\build\pyi.win32\hello\outPYZ1.pyz/vis.materials", line 159, in <module>
  File "c:\Python27\hello\build\pyi.win32\hello\outPYZ1.pyz/vis.materials", line 129, in loadTGA
IOError: [Errno 2] No such file or directory: 'c:\\Python27\\hello\\build\\pyi.win32\\hello\\outPYZ1.pyz/turbulence3.tga'

顺便说一句,我可以在该位置看到 outPYZ1.pyz 文件。有什么想法吗?

最佳答案

这不是关于 pyd 文件,而是关于找不到的 TGA 文件。当应用程序被 pyinstaller 打包时,您需要调整您的软件以查看不同的位置。根据Accessing to data files :

In a --onedir distribution, this is easy: pass a list of your data files (in TOC format) to the COLLECT, and they will show up in the distribution directory tree. The name in the (name, path, 'DATA') tuple can be a relative path name. Then, at runtime, you can use code like this to find the file:

os.path.join(os.path.dirname(sys.executable), relativename))

In a --onefile distribution, data files are bundled within the executable and then extracted at runtime into the work directory by the C code (which is also able to reconstruct directory trees). The work directory is best found by os.environ['_MEIPASS2']. So, you can access those files through:

os.path.join(os.environ["_MEIPASS2"], relativename))

因此,如果您在程序中打开一个文件,请不要这样做:

fd = open('myfilename.tga', 'rb')

此方法是从当前目录打开文件。所以它对 pyinstaller 不起作用,因为当前目录与放置数据的位置不同。

根据您是否使用--onefile,您必须更改为:

import os
filename = 'myfilename.tga' 
if '_MEIPASS2' in os.environ:
    filename = os.path.join(os.environ['_MEIPASS2'], filename))
fd = open(filename, 'rb')

或者如果是--onedir:

import os, sys
filename = os.path.join(os.path.dirname(sys.executable), 'myfilename.tga'))
fd = open(filename, 'rb')

关于python - PyInstaller: IOError: [Errno 2] 没有这样的文件或目录:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9553262/

相关文章:

python-3.x - PyInstaller 和 Nuitka 生成大得离谱的文件。如何缩小尺寸?

python 使用 Pyyaml 并保持格式

python - 使用 python netCDF4 将多字符字符串转换为 netcdf

python - 自编码器网络中的数组形状

python - 递归 Python 神经网络 - Reshape () 错误

python - 启动 exe 时加载 Python dll : python35. dll 时出错(错误代码 193)

python - 使用多处理队列从进程获取信息

python - OSX 下的 pygst + pyinstaller

python - 如何在另一台未安装 Python 的计算机上运行 Python.exe?

python - pyinstaller生成的exe文件不能在别人的电脑上使用