Use anaconda environment without activate? (e.g. in Crontab)

Question:

Being reading this

http://conda.pydata.org/docs/using/envs.html

Is it possible to run a conda python directly without having to source activate xxx?

In VirtualEnv, you can find the exact location of the python executable and run something like this

./path/to/my/venv/bin/python xxx.py

Then xxx.py will be executed with environment on. It’s handy to write one-linders in Crontab.

Could I do the same with anaconda/miniconda environments?

I’ve been trying this on Centos 6.5, system has python 2.5 which is too old.

I installed python 2.7 with miniconda, now I pip installed uWSGI, when executing it directly says

Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
ImportError: No module named site

But when execute it under conda env it works as expected.

Asked By: est

||

Answers:

If conda is on your path:

source activate <env name> && python xxx.py && source deactivate

If conda isn’t on your path but is installed:

source /path/to/conda/bin/activate /path/to/desired/env_name/ && python xxx.py && source deactivate

Answered By: Eric Dill

Actually, you can do exactly the same as in venv in miniconda.
If path_to_miniconda is your installation dir of miniconda, then

/path_to_miniconda/envs/name_of_env/bin/python xxx.py

should work.

Answered By: GrigorisG

You can use conda run (since v3.6) to use a specified environemnt to run your command without activating the environment. For example

# run scripts or commands against specific environment
conda run -n myenv python start_server.py

# or run an python interactively 
conda run -n myenv --no-capture-output python
Answered By: link89