Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activate a Conda Environment in Python Script

I'm converting a shell script to Python and I'm searching for a way to activate and deactivate a conda environment programmatically in Python. I've looked through the Conda code on Github and haven't been able to find a good solution.

I need the environment to be activated so that I can run multiple statements in it. For example:

source activate my_env
easy_install numpy 
backup_db
initialize_db
source deactivate 

I'm having no luck using subprocess. :-(

like image 204
William Hill Avatar asked Aug 21 '17 23:08

William Hill


People also ask

How can I activate conda environment automatically?

Reload Window from Command Palette, select base:conda as python interpreter then press Ctrl+Shift+` to open a new integrated Terminal, conda environment should be activated automatically in it.

How do you start a conda environment?

To create a new environment for Python development using conda you can use the conda create command. For a list of all commands, take a look at Conda general commands. The command above will create a new Conda environment called “python3-env” and install the most recent version of Python.

How do you activate conda environment in VS code terminal?

Ctrl+Shift+P and search “Terminal”. In the Terminal window, you can see “conda activate <env>” command already executed, and the terminal is under your conda environment.


1 Answers

A round-a-bout way of doing it, but couldn't you have python just call the script directly?

Refer to this question on how to do that

Run a .bat file using python code

like image 117
tisaconundrum Avatar answered Oct 01 '22 09:10

tisaconundrum