Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sys.argv[0] always returns nothing [duplicate]

I was trying to use sys.argv[0] to get the name of the script but it returned nothing. I guess that was because no script name was passed to the Python interpreter but I had no idea how to fix it.

my code:

import sys
print ("This is the name of the script: ", sys.argv[0])
sys.argv[0]

outputs:

>>> import sys
>>> print ("This is the name of the script: ", sys.argv[0])
This is the name of the script:
>>> sys.argv[0]
''

Thank you

like image 904
Xiaoyu Avatar asked Sep 14 '25 13:09

Xiaoyu


1 Answers

Well, that's well expected,

you're running your code on the interpreter, which is not any module nor file, so sys.argv knows that and gives you an empty string.

It's a good sign.

If you run it in an actual module or file, it will work perfectly, as expected.

like image 90
U12-Forward Avatar answered Sep 16 '25 06:09

U12-Forward