python 编译的 python 文件 (py2exe) 的命令行选项

python command line options to a compiled python file (py2exe)

我和许多 python 程序员一样发现 python 的命令行参数描述为 here very useful. specifically "-i" which keeps the python interpreter running after a program finishes for a stack trace. How can I use these with an exe file compiled with py2exe? Not to be confused with regular argument parsing in an exe file. If you came looking for that, find it here。 我的第一个想法是尝试:

pyprogram.exe -i -foo -bar

但这没有用。 需要注意的是

pyprogram.exe -foo -bar

实际上对我有用。 我正在寻找的是

的 .exe 等价物
python -i pyprogram.py foo bar

未能找到适用于所有 python 命令行选项的实现,我该怎么做才能使“-i”参数起作用?在我的可执行文件中作为一个选项是最重要的。

我在py2exe wiki about passing arguments like -i上没有找到任何东西(执行后进入交互模式)


人们可能能够在 py2exe source files.
中发现有关参数处理的信息 更新: 看起来 py2exe 确实不像普通解释器那样处理任何命令行选项,而只是 passes them to the script. But it does handle 相应的环境变量,可以如下所示使用.


但是,作为解决方法,您可以尝试 set the PYTHONINSPECT Environment variable:

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.

这是 os.environ (also os.putenv 的小测试脚本):

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()) )

行为有点奇怪:似乎没有区别,而且似乎只持续到您退出交互模式:

G:\>py test.py > test.txt
>>> exit()

G:\>set PYTHONINSPECT
Environment variable PYTHONINSPECT not defined

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

尝试使用 py2exe 和 Python 3.4.3 (newer versions are apparently not supported and you get an IndexError):

setup.py:

from distutils.core import setup
import py2exe
setup(console=['test.py'])

获取py2exe

G:\>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

运行 py2exe

G:\>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 defined

也适用于 single exe:

from distutils.core import setup
import py2exe
setup(
    options = {'py2exe': {'bundle_files': 1, 'compressed': True}},
    console = [{'script': "test.py"}],
    zipfile = None,
)