1

When I run python3 myscript.py foo bar 123 then sys.argv holds the value ['myscript.py', 'foo', 'bar', '123']. I'd like to access the name of the command itself (python3 in this case). How can I do that?

8
  • Isn' t it always python3 if you are running python 3? What could this be useful for? Commented Sep 14, 2019 at 12:44
  • 1
    @liakoyras It could be python3.7 or /usr/bin/python Commented Sep 14, 2019 at 12:49
  • 1
    I don't believe that's possible from the python script, though I could certainly be wrong. Do you just need to know which version of python you are running: sys.version_info? Commented Sep 14, 2019 at 12:51
  • Which operating system?? Which command shell? Commented Sep 14, 2019 at 13:04
  • 1
    @IanRehwinkel: Use the module psutil and get the commandline, find the pythonX process running your .py. Commented Sep 14, 2019 at 13:49

1 Answer 1

2

Using the psutil module (can be found here), it is possible to retrieve all command line arguments used:

psutil.Process().cmdline()

It returns a list of strings, including the command used to start the python interpreter (e.g. /usr/bin/python3).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.