Reputation: 1779
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?
Upvotes: 7
Views: 640
Reputation: 1955
Run python this way:
python - foo bar
Then
import sys
sys.argv
will work :)
Upvotes: 2
Reputation: 2776
$ 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']
>>>
Upvotes: 7
Reputation: 22571
Use -
as script name:
python - foo bar
Test:
>>> import sys
>>> sys.argv
['-', 'foo', 'bar']
Upvotes: 15
Reputation: 5072
You could just set it manually for testing in IDLE:
try:
__file__
except:
sys.argv = [sys.argv[0], 'foo', 'bar']
Upvotes: 0