I want to run my python code (that'll eventually become standalone python script) in org-mode. This script requires command line arguments (provided by sys lib's sys.argv).

How do I execute python code-block that contain sys.argv in org-mode? (and give appropriate arguments)

For example:

#+begin_src python :session mysess :results output
  # this part opens file
  fname = sys.argv[1]

will cause:

Traceback (most recent call last):
  File "<string>", line 17, in __PYTHON_EL_eval
  File "<string>", line 3, in <module>
  File "/tmp/babel-vHlVQW/python-Ua2ZbY", line 3, in <module>
    fname = sys.argv[1]
IndexError: list index out of range

1 Answer 1


Apparently one could just use separate code blocks (like below) that won't tangled into the actual output standalone file.

#+begin_src python :session mysess :results output :tangle no
  sys.argv= ['python', '/home/user/dir/to/file']

I didn't know sys.argv can directly changed with sys.argv= ['python', '/home/user/dir/to/file']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.