I have two pieces of code that require different versions of python and versions of packages. I have two conda environments that allow each piece of code to work separately. It seems impossible to create an environment that will support both of them. Is there a way to switch conda environment during the run (in python code), so that I could execute one part using one environment and the second part using the second environment in the same script? The form and format of the result of the first part is definitely supported by the second part, so I don't see a reason why it can't work.
Asked
Active
Viewed 1,064 times
1
-
Does this answer your question? [How do you activate an Anaconda environment within a Python Script?](https://stackoverflow.com/questions/36275308/how-do-you-activate-an-anaconda-environment-within-a-python-script) – Israel-abebe Jan 11 '20 at 13:33
2 Answers
2
I have had success using conda within a shell script that calls the python process. e.g. something along these lines
conda activate my_env_1
python some_process.py
conda deactivate
conda activate my_env_2
python some_process_2.py
conda deactivate
You will have to enable your shell to use conda. See this Python - Activate conda env through shell script

roshambo
- 197
- 1
- 10
0
If you want to change the environments in the same code at the same time, unfortunately as far as I know that will be painful, or just impossible with one word. In order to fix this issue, just add the libraries that your other environment has into the one that you use mainly. You can achieve this by opening either your cmd (commnand line prompt), or just anaconda prompt: activate yourenvironment name pip/pip3 install modulename

Vusal Ismayilov
- 111
- 2
- 9
-
Thank you for your reply. The problem is that one piece of code requires torchvision 0.2.0 (not higher), and the second piece of code requires 0.3.0 (not lower). I need to use the two pieces in the same code. – yliats Jan 11 '20 at 16:43
-
oh, you really gotta take care of it. did you use . Glad that you found it out, good luck! – Vusal Ismayilov Jan 11 '20 at 17:12
-
Just a note, you say "or just impossible with one word". Please notice that 'painful' is one word as well – Shaq Jul 25 '23 at 11:37