2

I have a wordpress website running with Apache. From that website, I need to run a python code, but the libraries I need are configured inside an Anaconda environment. To activate the anaconda environment, I need to run:

source activate my_environment

...on the terminal. On my .php code, on the other hand, it would go something like:

shell_exec('source activate my_environment');

The thing is: I can only activate my anaconda environment from terminal with the user that created it. How can I set it up so that my Apache server can activate it from a PHP snippet that I have on my website?

P Handler
  • 39
  • 3
  • Why do you need to activate the environment (which should be done with `conda activate my_environment` for recent versions of conda)? Why not just run that Python directly? `/path/to/my_environment/bin/python script_name.py` – darthbith Aug 07 '18 at 19:53
  • This works! Thank you very much :) – P Handler Aug 07 '18 at 20:29

2 Answers2

1

Since activating the environment has to occur in the same shell process, you can't activate an environment in the way that you're trying. However, you can run the version of Python installed in that environment directly, although if you have any packages that modify the environment, those changes won't be picked up without activating the environment. Something like

/path/to/my_environment/bin/python script_name.py

PS: Conda environments are activated by conda activate env_name in recent versions of Conda (>=4.4)

darthbith
  • 18,484
  • 9
  • 60
  • 76
0

If you need to activate a conda environment from apache the conda command may not work. However, if you know where the the path to your conda environment you may also use

jalazbe: $ source /conda-path-env/bin/activate 

Example:

jalazbe: $ source /home/myuser/myenv/bin/activate 

It should work

jalazbe
  • 1,801
  • 3
  • 19
  • 40