Starting a python shell with arguments


is it possible to pass arguments to the python in linux without having a file? I’m currently not able to create a file or change permissions and I don’t want to write it inside my code like this:

import sys
sys.argv = ["arg1", "arg2", ...]

I’d like to hand over the arguments while I’m starting the shell:

python <arguments>
Asked By: Michael Koeppen



While is is questionable if passing commands line arguments to an interactive shell is best practice, it is indeed possible by passing - instead of the the scripts file name:

$ python - a1 a2
Python 2.7.14 (default, Sep 23 2017, 22:06:14) 
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.argv
['-', 'a1', 'a2']
Answered By: Klaus D.

If you are using IPython as interactive shell, you can pass arguments to scripts you run using the %run magic command:

In [1]: %run arg1 arg2 ...

(It’s not really clear to me what you are trying to achieve, but you probably want to pass the arguments to some script.)

Answered By: Sven Marnach
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.