How do I activate a conda env in a subshell?
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 pythonPython 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 linuxType "help", "copyright", "credits" or "license" for more information.>>> (root) ~/condaexpts$ source deactivate~/condaexpts$ which python/usr/bin/python~/condaexpts$ /usr/bin/env pythonPython 2.7.6 (default, Oct 26 2016, 20:30:19) [GCC 4.8.4] on linux2Type "help", "copyright", "credits" or "license" for more information.>>>
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.
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/pythonimport os,sysprint sys.executableprint 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.