2

How can I change Anaconda python environment on Windows globally? This question is NOT asking how can I create or activate environments, which already answered in multiple other questions as listed below. What this question is asking is how can I globally or permanently switch python environments so that the default environment becomes the activated environment until another environment is activated again.

I know I can use the command conda activate my_env_name in Command Prompt (cmd). However, this only changes the python environment for that specific instance of CMD window that is open. This does NOT change the python environment globally, thus any other application that would be running a python code (ex: Sublime text editor), will still use the default python environment.

Currently I have two environments on my system as listed below. Base is the default Python 2.7 environment which was created when I installed Anaconda2. I created a new environment called py3, which is Python 3.7.

C:\Users\MyUsername>conda env list
# conda environments:
#
base                  *  C:\Users\MyUsername\Anaconda2
py3                      C:\Users\MyUsername\Anaconda2\envs\py3

I have two Python scripts which I would like to run with the proper environment. One script uses Python 2 and the other uses Python 3. I use Sublime as my text editor and I build and run my python scripts directly from the text editor. The issue I am having that just by changing the environment in CMD using conda activate py3, the text editor still uses the default base environment to run the python script.

What is the purpose of managing multiple Python environment by using conda activate my_env_name, when it ONLY changes the environment for that specific CMD/Terminal instance and NOT globally? I am sure there much a reason, but maybe I am not able to think more abstractly since my use for Python has mostly been for scripting and executing in a text editor.

Temporary solution

A temporary solution I have found is editing the Path variable for User variables to include my path to my alternate py3 environment. I was able to manage the Path variable by going to Advanced system settings >> Environment Variables >> User variables. Thus, my Path variable contains path for both base and py3. If I want to switch the default environment to py3, I have to change the order of py3 path to before base path.

Although this temporary solution works, I am always afraid editing PATH variables. I think users, on Windows at least, shoulnd't have to worry about manually changing or updating PATH variables. Why is this needed? I originally expected that when I used the command conda activate py3 in Power Shell or CMD, it would automatically change the Path variable to point Python path to the desired alternate environment.

Zythyr
  • 1,142
  • 4
  • 20
  • 33

1 Answers1

1

The reason is simple, some applications or projects have certain dependencies that may not be compatible with one environment E.g. say a project requires python 2.7 you obviously cannot run this application on python 3 environment.

Now, to answer your question of why conda changes environment for only specific CMD, well honestly let me answer by asking why should conda change the default environment everytime when you use the conda activate your-env command? You could just switch to the directory where your python files are and run the python yourfilename.py

if you just want to change the default interpreter in sublime, you could follow this link here.

Souldiv
  • 145
  • 1
  • 5