[英]Get the command that executed the python script inside the script itself
I want the command that was used to invoke the python script to be available in the script itself.我希望用于调用 python 脚本的命令在脚本本身中可用。
Like:喜欢:
python3 foo.py -a -b
python foo.py -c
Is it possible in foo.py to get these whole commands.是否可以在 foo.py 中获取这些完整的命令。
Use sys.executable
and sys.argv
:使用sys.executable
和sys.argv
:
from __future__ import print_function
import sys
print(sys.executable)
print(sys.argv)
The above should work with Python 2 and Python 3 (below is from macOS):以上应该适用于 Python 2 和 Python 3(以下来自 macOS):
howes% python2 blah.py 1 -m 123 53131
/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
['blah.py', '1', '-m', '123', '53131']
howes% python3 blah.py 1 -m 123 53131
/usr/local/opt/python/bin/python3.7
['blah.py', '1', '-m', '123', '53131']
EDIT编辑
If that is not enough, you can try the psutil library (pip install psutil):如果这还不够,您可以尝试使用psutil库(pip install psutil):
from __future__ import print_function
import os, psutil
us = psutil.Process(os.getpid())
print(us.cmdline())
print(us.exe())
macOS 10.15.2 (Python 2.7 and Python 3.7*): macOS 10.15.2(Python 2.7 和 Python 3.7*):
howes% python blah.py -a 1 -b 2 3 4
['/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python', 'blah.py', '-a', '1', '-b', '2', '3', '4']
/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
howes% python3 blah.py -a 1 -b 2 3 4
['/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/Resources/Python.app/Contents/MacOS/Python', 'blah.py', '-a', '1', '-b', '2', '3', '4']
/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/Resources/Python.app/Contents/MacOS/Python
howes%
Windows 10 (Python 2.7 and Python 3.8): Windows 10(Python 2.7 和 Python 3.8):
C:\Users\b.howes>python2 blah.py -a 1 -b 2 3 4
['python2', 'blah.py', '-a', '1', '-b', '2', '3', '4']
C:\Python27\python2.exe
C:\Users\b.howes>python blah.py -a 1 -b 2 3 4
['python', 'blah.py', '-a', '1', '-b', '2', '3', '4']
C:\Users\b.howes\AppData\Local\Programs\Python\Python38\python.exe
The closest thing I can think of is this:我能想到的最接近的事情是这样的:
import sys
import os
s = ""
for arg in sys.argv:
s = s + arg + " "
print(os.path.basename(sys.executable) + " " + s)
Example output (on windows):示例输出(在 Windows 上):
$ python tmp.py -a -b
python.exe tmp.py -a -b
In Unix environments, you do not have access to the command line itself -- this is not python specific.在 Unix 环境中,您无权访问命令行本身——这不是 Python 特有的。 However, a shell or other well-behaved program executing another program will place the executable name or path in the first argument.但是,执行另一个程序的 shell 或其他行为良好的程序会将可执行文件名称或路径放在第一个参数中。
Python's sys.argv
is not giving you, however, the OS-level command line.然而,Python 的sys.argv
没有给你操作系统级的命令行。
You can obtain the python executable with sys.executable
, but neither this nor sys.argv
would give you the full command line:您可以使用sys.executable
获取 python 可执行文件,但无论是 this 还是sys.argv
都不会给您完整的命令行:
Say this is foo.py
:说这是foo.py
:
import sys
print(sys.argv)
print(sys.executable)
Then:然后:
$ python -O foo.py a b c
['foo.py', 'a', 'b', 'c']
/usr/bin/python
As you can see, the -O
flag is not present there.如您所见,那里没有-O
标志。
If you really need these, the way to obtain it is probably dependent on the OS (I do not know if python gives you access to these "low-level" command line arguments).如果你真的需要这些,获取它的方式可能取决于操作系统(我不知道 python 是否允许你访问这些“低级”命令行参数)。
If you are on Linux, you could do:如果你在 Linux 上,你可以这样做:
import os
with open("/proc/%s/cmdline" % os.getpid(), "rb") as f:
cmdline = f.read()
print(cmdline.split(b"\x00"))
With the result:结果:
$ python -O foo.py a b c
['python', '-O', 'foo.py', 'a', 'b', 'c', '']
Note the last empty string is due to how cmdline
has NUL-terminated strings, so the last character will be b"\\x00"
, hence split()
will give you the last empty string.请注意,最后一个空字符串是由于cmdline
具有以 NUL 结尾的字符串的方式,因此最后一个字符将是b"\\x00"
,因此split()
将为您提供最后一个空字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.