这与pyd文件无关,但与找不到TGA文件有关。当pyinstaller打包应用程序时,您需要调整软件以使其位于其他位置。根据访问数据文件:
在–
onedir分发中,这很容易:将数据文件列表(以TOC格式)传递给COLLECT,它们将显示在分发目录树中。(名称,路径,“数据”)元组中的名称可以是相对路径名称。然后,在运行时,您可以使用如下代码查找文件:os.path.join(os.path.dirname(sys.executable), relativename))在–onefile分发中,数据文件被捆绑在可执行文件中,然后在运行时通过C代码(也能够重建目录树)提取到工作目录中。最好通过os.environ
[‘_ MEIPASS2’]找到工作目录。因此,您可以通过以下方式访问这些文件:os.path.join(os.environ["_MEIPASS2"], relativename))
因此,如果您在程序中打开文件,请不要执行以下操作:
fd = open('myfilename.tga', 'rb')此方法从当前目录打开文件。因此,它对于pyinstaller来说将不起作用,因为当前目录与放置数据的位置不同。
根据是否使用
--onefile,必须更改为:
import osfilename = 'myfilename.tga' if '_MEIPASS2' in os.environ: filename = os.path.join(os.environ['_MEIPASS2'], filename))fd = open(filename, 'rb')
或如果是
--onedir:
import os, sysfilename = os.path.join(os.path.dirname(sys.executable), 'myfilename.tga'))fd = open(filename, 'rb')


![PyInstaller:IO错误:[Errno 2]没有这样的文件或目录: PyInstaller:IO错误:[Errno 2]没有这样的文件或目录:](http://www.mshxw.com/aiimages/31/626050.png)
