0

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
  print(sys.argv)
  fname = sys.argv[1]
  print(fname)
#+end_src

will cause:

>>> 
['']
org_babel_python_eoe
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

0

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']
#+end_src

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.