Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set python env in org mode

When I write some python code in emacs org-mode, how can I set the python env to a specific env. I use the code as follow but it didn't work.

    #+BEGIN_SRC python :python /opt/local/bin/python2.7 :results output
    print "python 2"
    #+END_SRC
like image 773
Junnan Zhu Avatar asked Mar 09 '26 22:03

Junnan Zhu


2 Answers

You could go to the scratch buffer and execute (setq org-babel-python-command "python2")

Then in an org file try

#+BEGIN_SRC python
import platform
return platform.python_version()
#+END_SRC

And should get something like:

#+RESULTS:
: 2.7.13

If that works then add it to your init file.

like image 176
brittAnderson Avatar answered Mar 11 '26 11:03

brittAnderson


I also needed org-mode to run python from one of my anaconda environments. You can set the emacs variable, python-shell-virtualenv-root, to the anaconda environment's path. That is;

M-x customize-variable RET

python-shell-virtualenv-root RET

Click on the Value Menu button where you can then type in the box

String:/PATH/TO/VIRTUALENV

That solved it for me.

EDIT: I failed to mention the above was tested only on emacs 25.2 Additionally, I now realize the solution works only in session code blocks. If a session is not specified, then the python code is run with the default, system-wide, Python installation.

like image 29
rortms Avatar answered Mar 11 '26 10:03

rortms



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!