简体   繁体   中英

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

I, like many python programmers find the command line arguments for python described 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 . My first thought was to try:

pyprogram.exe -i -foo -bar

but that didn't work. it should be noted that

pyprogram.exe -foo -bar

does in fact work for me. what I am looking for is the .exe equivalent of

python -i pyprogram.py foo bar

Failing to find an implementation that works for all of the python command line options, what could I do just to make the "-i" argument work? it is the most important to have as an option in my executable.

I did not find anything on the py2exe wiki about passing arguments like -i (to enter interactive mode after execution).


One might be able to discover information about the argument handling in the py2exe source files .
Update: It indeed looks like py2exe does not handle any command line options like the normal interpreter does, instead it just passes them to the script. But it does handle the respective environment variable, which can be used as shown below.


However, as a workaround, you could try to set the PYTHONINSPECT Environment variable :

If this is set to a non-empty string it is equivalent to specifying the -i option.

Eg run set PYTHONINSPECT=TRUE before running the program.

But, probably even better, this can be done from within the Python script:

This variable can also be modified by Python code using os.environ to force inspect mode on program termination.

Here's a little test script for os.environ (also os.putenv ):

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

G:\>set PYTHONINSPECT
Environment variable PYTHONINSPECT not defined

The behaviour is a little weird: there does not seem to be a difference, and it seems to only last until you exit the interactive mode:

environ({'ALLUSERSPROFILE': 'C:\\ProgramData', ... 'PYTHONINSPECT': 'TRUE'})
environ({'ALLUSERSPROFILE': 'C:\\ProgramData', ... 'PYTHONINSPECT': 'TRUE'})
environ({'ALLUSERSPROFILE': 'C:\\ProgramData', ... 'PYTHONINSPECT': 'TRUE'})
set()
set()

The contents of test.txt are:

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

But it seems to work either way (double check the documentation for yourself to ensure you are not corrupting your environment variables), so you could even implement an -i argument for yourself like:

G:\>py test.py
normal

G:\>py test.py -i
interactive
>>> quit()

G:\>set PYTHONINSPECT
Environment variable PYTHONINSPECT not defined

which runs as follows

 G:\\>py test.py normal G:\\>py test.py -i interactive >>> quit() G:\\>set PYTHONINSPECT Environment variable PYTHONINSPECT not defined 

Trying with py2exe and Python 3.4.3 (newer versions are apparently not supported and you get an IndexError ):

setup.py:

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

Get 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

Run py2exe

G:\>dist\test.exe
normal

G:\>dist\test.exe -i
interactive
>>> sys.exit()

Test

G:\>set PYTHONINSPECT
Environment variable PYTHONINSPECT not defined

Does not seem to have changed the environment variables permanently:

 G:\\>set PYTHONINSPECT Environment variable PYTHONINSPECT not defined 

Also works with single exe :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM