skip to Main Content

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?

2

Answers


  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)

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search