9

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. :-(

3
  • are you trying to activate the environment from within a Python script or as part of a bash script? Commented Aug 22, 2017 at 0:23
  • @James I'm trying to activate the environment with the Python script. Commented Aug 22, 2017 at 5:53
  • 3
    If you are trying to have the script switch to a new environment and then continue execution, that is not possible as each environment is isolated. Each environment uses it's own kernel, so switching to a new environment necessitates starting up a new Python kernel which knows nothing about the previous computation in the script. Commented Aug 22, 2017 at 12:26

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

That would probably be my fallback plan as I would still have to extract the function and the associated variables to a different file. Thanks for idea though.
1

Linux. Was trying to create bash script to run python function from my app with test environment specifications.

#!/bin/bash

conda run -n test python -c 'from otp import Qod; Qod()';

If you have python script not in the same directory you can add sys.path absolute or relative. Planning to upgrade this script bash automation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.