PyInstaller: "导入错误:没有名为os的模块

0 投票
1 回答
3048 浏览
提问于 2025-04-16 19:20

我正在学习PyInstaller。我创建了两个简单的文件,分别是Test.py:

import os

还有Test.spec:

anal = Analysis (["Test.py"])
pyz = PYZ(anal.pure)
exe = EXE(anal.scripts, pyz, name="Test.exe", exclude_binaries=1, debug=1)
coll = COLLECT(exe, anal.binaries, name="dist")

然后我运行了:

Build.py Test.spec

这个过程没有出现任何错误信息,生成了一个名为dist的文件夹,里面有几个文件,包括Test.exe。当我运行Test.exe时,它失败了,输出了:

Found embedded PKG: C:\Documents and Settings\Rade\My Documents\Development\Test\Test.exe
Extracting binaries
manifestpath: C:\Documents and Settings\Rade\My Documents\Development\Test\Test.
exe.manifest
Error activating the context
python27.dll
Manipulating evironment
PYTHONPATH=C:/Documents and Settings/Rade/My Documents/Development/Test
importing modules from CArchive
extracted iu
extracted struct
extracted archive
Installing import hooks
outPYZ1.pyz
Running scripts
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named os
RC: -1 from Test
OK.

我肯定我犯了一些初学者的低级错误,但到底是什么呢?

1 个回答

2

对于像这样的简单文件,你应该使用 Makespec.py 来创建规格文件,而不是手动编写。对于较大的项目,你可以把 Makespec.py 生成的文件当作模板,然后进行编辑。

http://www.pyinstaller.org/export/latest/tags/1.4/doc/Manual.html#create-a-spec-file-for-your-project

撰写回答