Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I access sys.argv in python in interactive mode?

I'd like to do something like:

% python foo bar
import sys
sys.argv

to get:

%  ['foo', 'bar']

but of course python dies when you enter an argument which is not a script or goes into non interactive mode if you do.

Can I do this somehow?

like image 537
shigeta Avatar asked Feb 26 '14 20:02

shigeta


2 Answers

Use - as script name:

python - foo bar

Test:

>>> import sys
>>> sys.argv
['-', 'foo', 'bar']
like image 194
ndpu Avatar answered Sep 20 '22 18:09

ndpu


$ python - asdf asdf
Python 2.7.6 (default, Feb 15 2014, 23:06:13) 
[GCC 4.8.2 20140206 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.argv
['-', 'asdf', 'asdf']
>>> 
like image 40
Javier Avatar answered Sep 18 '22 18:09

Javier