meh
meh

Reputation: 3123

How do I activate a conda env in a subshell?

I've written a python program. And if I have a shebang like this one:

#!/usr/bin/python

and I make the file executable with:

$ chmod 755 program.py

I can run the program like so:

$ ./program.py

Here is the issue. I use the conda virtual environments. When I run the program like above, the system creates a subshell that does not recognize the active environment:

(my_env) $ ./program.py
ImportError: No module named pymongo

If I do it this way, however...

(my_env) $ python program.py
# blah blah... runs great

How do I specify the right environment for use in the subshell? Is it possible? I'd like to save my fingers the effort of typing the six character string that is python.

Another post, Shebangs in conda managed environments, briefly touches on this but does not provide the right answer. Instead of activating the environment in the subshell, it just says, go ahead and ignore the shebang... just use the $ python program.py syntax.

Upvotes: 22

Views: 16975

Answers (5)

ZeroRin
ZeroRin

Reputation: 111

I tested the conda run method mentioned above, and got the following error

/usr/bin/env: ‘conda run -n my_env python’: No such file or directory
/usr/bin/env: use -[v]S to pass options in shebang lines

after a brief search i learned that here env treats conda run -n my_env python as a single long argument. and as the error message suggests, adding -S will solve this.

another problem is that conda run does not provide real-time output on the terminal by default, which might not be desired if you're just searching alternative to !/usr/bin/env python, adding --live-stream option will fix this.

so to put things together, the final shebang is like this

#! /usr/bin/env -S conda run --live-stream -n my_env python

Upvotes: 1

Da1ne
Da1ne

Reputation: 74

The accepted answer didn't run for me, however this did:

#!conda run -n your_conda_env_name python

This requires conda in $PATH, and was on Windows.

Upvotes: 2

merv
merv

Reputation: 76700

conda run

If you always plan to run the script from a shell session where conda is defined, then another alternative is let Conda load the env using the conda run command. In this case, the shebang would be

#!/usr/bin/env conda run -n my_env python

The advantage here is that you don't need the env to be activated when you call ./program.py and you don't have to hardcode the location of the interpreter.

Note: This command was added as a "preview" in Conda v4.6.0 (see Release Notes) to address the issue of running a command inside an env.

Upvotes: 24

Dave X
Dave X

Reputation: 5127

You can also point directly to the environment with the shebang line so you need not depend on something setting up the parent environment prior to calling the script.

First determine your preferred python environment's path:

$ . activate mypython
$ which python
/home/username/anaconda/envs/mypython/bin/python      # for example 

Then use it in a script:

#!/home/username/anaconda/envs/mypython/bin/python
import os,sys
print sys.executable
print os.__file__

The above script would give output like this:

/home/username/anaconda/envs/mypython/bin/python
/home/username/anaconda/envs/mypython/lib/python2.7/os.pyc

Having a non-generic command in the shebang makes the script less portable, but if you depend the specific packages in a particular virtual environment, this is what you want.

Upvotes: 3

Nehal J Wani
Nehal J Wani

Reputation: 16629

In your script, change...

#!/usr/bin/python

...to:

#!/usr/bin/env python

The python used by an activated conda environment is ${CONDA_PREFIX}/bin/python and not /usr/bin/python

Notice the difference?

(root) ~/condaexpts$ which python
/home/ubuntu/condaexpts/m3/bin/python

(root) ~/condaexpts$ /usr/bin/env python
Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:53:06) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

(root) ~/condaexpts$ source deactivate

~/condaexpts$ which python
/usr/bin/python

~/condaexpts$ /usr/bin/env python
Python 2.7.6 (default, Oct 26 2016, 20:30:19) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Upvotes: 29

Related Questions