我在py2exe wiki上没有找到任何关于传递^{}(执行后进入交互模式)的参数的内容。在
您可能可以在py2exe source files中发现有关参数处理的信息。
更新:看起来py2exe并不像普通解释器那样处理任何命令行选项,而是将它们转换到脚本中。但是它使用handle相应的环境变量,如下所示。
If this is set to a non-empty string it is equivalent to specifying the -i option.
例如,在运行程序之前运行set PYTHONINSPECT=TRUE。在
但是,可能更好的是,这可以在Python脚本中完成:This variable can also be modified by Python code using os.environ to force inspect mode on program termination.import os
one = os.environ
os.putenv("PYTHONINSPECT", "TRUE")
two = os.environ
os.environ["PYTHONINSPECT"] = "TRUE"
three = os.environ
print(one)
print(two)
print(three)
print( set(one.items()) ^ set(two.items()) )
print( set(one.items()) ^ set(three.items()) )
这种行为有点奇怪:似乎没有区别,而且似乎只会持续到退出交互模式:
^{pr2}$
test.txt的内容是:environ({'ALLUSERSPROFILE': 'C:\\ProgramData', ... 'PYTHONINSPECT': 'TRUE'})
environ({'ALLUSERSPROFILE': 'C:\\ProgramData', ... 'PYTHONINSPECT': 'TRUE'})
environ({'ALLUSERSPROFILE': 'C:\\ProgramData', ... 'PYTHONINSPECT': 'TRUE'})
set()
set()
但不管哪种方法都有效(请仔细检查文档以确保不会损坏环境变量),因此您甚至可以为自己实现一个-i参数,例如:import sys, os
if len(sys.argv) > 1 and sys.argv[1] == '-i':
os.putenv("PYTHONINSPECT", "TRUE")
#os.environ["PYTHONINSPECT"] = "TRUE"
print("interactive")
else:
print("normal")
其运行方式如下G:\>py test.py
normal
G:\>py test.py -i
interactive
>>> quit()
G:\>set PYTHONINSPECT
Environment variable PYTHONINSPECT not defined
在设置.py公司名称:from distutils.core import setup
import py2exe
setup(console=['test.py'])
获取py2exeG:\>c:\Python34\Scripts\pip.exe install py2exe
You are using pip version 6.0.8, however version 10.0.0b2 is available.
You should consider upgrading via the 'pip install upgrade pip' command.
Collecting py2exe
Using cached py2exe-0.9.2.2-py33.py34-none-any.whl
Installing collected packages: py2exe
Successfully installed py2exe-0.9.2.2
运行py2exeG:\>c:\Python34\python.exe setup.py py2exe
running py2exe
3 missing Modules
? readline imported from cmd, code, pdb
? win32api imported from platform
? win32con imported from platform
Building 'dist\test.exe'.
Building shared code archive 'dist\library.zip'.
Copy c:\windows\system32\python34.dll to dist
Copy c:\Python34\DLLs\_hashlib.pyd to dist\_hashlib.pyd
Copy c:\Python34\DLLs\pyexpat.pyd to dist\pyexpat.pyd
Copy c:\Python34\DLLs\select.pyd to dist\select.pyd
Copy c:\Python34\DLLs\unicodedata.pyd to dist\unicodedata.pyd
Copy c:\Python34\DLLs\_ctypes.pyd to dist\_ctypes.pyd
Copy c:\Python34\DLLs\_socket.pyd to dist\_socket.pyd
Copy c:\Python34\DLLs\_lzma.pyd to dist\_lzma.pyd
Copy c:\Python34\DLLs\_ssl.pyd to dist\_ssl.pyd
Copy c:\Python34\DLLs\_bz2.pyd to dist\_bz2.pyd
试验G:\>dist\test.exe
normal
G:\>dist\test.exe -i
interactive
>>> sys.exit()
似乎没有永久改变环境变量:G:\>set PYTHONINSPECT
Environment variable PYTHONINSPECT not definedfrom distutils.core import setup
import py2exe
setup(
options = {'py2exe': {'bundle_files': 1, 'compressed': True}},
console = [{'script': "test.py"}],
zipfile = None,
)