230

I've installed Anaconda and created two extra environments: py3k (which holds Python 3.3) and py34 (which holds Python 3.4). Besides those, I have a default environment named 'root' which the Anaconda installer created by default and which holds Python 2.7. This last one is the default, whenever I launch 'ipython' from the terminal it gives me version 2.7. In order to work with Python 3.4, I need to issue the commands (in the shell)

source activate py34
ipython

which change the default environment to Python 3.4. This works fine, but it's annoying since most of the time I work on Python 3.4, instead of Python 2.7 (which I hold for teaching purposes, it's a rather long story). Anyway, I'll like to know how to change the default environment to Python 3.4, bearing in mind that I don't want to reinstall everything from scratch.

AMC
  • 2,642
  • 7
  • 13
  • 35
user2734434
  • 2,661
  • 2
  • 14
  • 16
  • 7
    have you considered using `source activate py34` in your `.bashrc`? – cel Feb 10 '15 at 16:54
  • 1
    Thanks for your answer. I haven't tried it because I think it is not possible to call source activate from a bash script, as the source command needs to be run in the same shell, not in a subshell. I've tried putting the two lines above into a bash script, and that I can say does not work. – user2734434 Feb 10 '15 at 16:58
  • 4
    I have `CONDA_ROOT="/Users/bla/miniconda"` and `source ${CONDA_ROOT}/bin/activate ${CONDA_ROOT}/envs/empty &> /dev/null` in my `.bashrc` and it seems to work fine. – cel Feb 10 '15 at 17:04
  • 1
    I think `.bashrc` is not run as a script, but sourced by the new shell, therefore you cannot check it by running a bash script. – cel Feb 10 '15 at 17:07
  • 3
    @user2734434 the bashrc file is sourced at the beginning of the session, so if you put `source activate` in it, it will affect the shell environment. – asmeurer Feb 11 '15 at 17:40
  • I like the response from @cel -- I would combine it with running 'conda config --set changeps1 False' so you don't see the name of your config is the prompt. The downside is that you won't ever see the configuration details in your prompt. – MrMas Nov 23 '16 at 20:24

18 Answers18

110

If you just want to temporarily change to another environment, use

source activate environment-name

ETA: This may be deprecated. I believe the current correct command is:

source conda activate environment-name

(you can create environment-name with conda create)


To change permanently, there is no method except creating a startup script that runs the above code.


Typically it's best to just create new environments. However, if you really want to change the Python version in the default environment, you can do so as follows:

First, make sure you have the latest version of conda by running

conda update conda

Then run

conda install python=3.5

This will attempt to update all your packages in your root environment to Python 3 versions. If it is not possible (e.g., because some package is not built for Python 3.5), it will give you an error message indicating which package(s) caused the issue.

If you installed packages with pip, you'll have to reinstall them.

Andreus
  • 2,437
  • 14
  • 22
asmeurer
  • 86,894
  • 26
  • 169
  • 240
  • So if I want to update my root environment from python 3.4 to 3.5, I can do `conda install anaconda python=3.5`? I'm on Windows and I also have some pip and other packages in there as well. When you said "that last item will not work in Windows" were you referring to the `--clone` option, or your 4th bullet? – flutefreak7 Nov 03 '15 at 20:02
  • 1
    @flutefreak It actually will work on Windows now (run `conda update conda` first to make sure you have the latest version). I'll update the answer. – asmeurer Nov 03 '15 at 22:20
  • Yeah the `conda update conda` became necessary to resolve another issue I documented [here](https://github.com/ContinuumIO/menuinst/issues/16). I actually did `conda update conda python=3.5` which updated conda and python, then `conda update anaconda` picked up the Python 3.5 change and updated anaconda accordingly. Not sure if order matters, but that's what worked for me. – flutefreak7 Nov 03 '15 at 22:23
  • 181
    This answer doesn't seem to connect to the question, which is about changing the default environment. – Bob Dec 22 '15 at 08:34
  • @Bob if you don't pass `-n` and don't have an environment activated, conda will work against the default (root) environment. – asmeurer Dec 22 '15 at 16:26
  • 20
    @asmeurer But that isn't what's been asked. He doesn't want to update packages in any env. He wants to make py34 the default environment, to be active without intervention, so py34 is active before he sends the "source activate" command. – Bob Dec 22 '15 at 22:34
  • @Bob, this will do that. The default "environment" is what is active without any `activate` commands. The command `conda update --all python=3.5` ensures that all packages that are installed will be "updated" to the Python 3.5 versions (and more generally, that the package dependencies in the environment are fully consistent). – asmeurer Dec 26 '15 at 06:20
  • 21
    I have to agree with @Bob here. The packages in the root environment will be updated to 3.5, and this may have solved the original poster's specific problem. But when you open a prompt you will still be in the root environment, so you have not specified another default environment. Since everyone who wants to achieve this and googles it ends up at this question, it would be better if the original questions was also answered. Perhaps changing the default env to something other than root is not possible? – arjan Mar 16 '16 at 16:44
  • @arjan also take a look at the other answer (by Jev). That may be closer to what you are looking for. – asmeurer Mar 16 '16 at 20:50
  • 4
    This doesn't work for me. My default is python 3.4, using the flag `python=3.5` didn't upgrade python from 3.4 to 3.5...Any suggestions? – WillZ Jan 18 '17 at 21:38
84

Overview
Some people have multiple Conda environments with different versions of Python for compatibility reasons. In this case, you should activate the desired default environment in the shell initialization file (e.g., .bashrc, .zshrc). With this method, you can preserve the versions of Python you use in your environments.

The following assumes environment_name is the name of your environment

Mac / Linux:
Edit your bash profile so that the last line is conda activate environment_name. In Mac OSX this is ~/.bash_profile, in other environments this may be ~/.bashrc

Example:
Here's how I did it on Mac OSX

  1. Open Terminal and type:

    nano ~/.bash_profile

  2. Go to end of file and type the following, where "p3.5" is my environment:

    conda activate p3.5

  3. Exit File. Start a new terminal window.

  4. Type the following to see what environment is active

    conda info -e

The result shows that I'm using my p3.5 environment by default.

For Windows:
Create a command file (.cmd) with activate environment_name and follow these instructions to have it execute whenever you open a command prompt

  1. Create a batch file command, e.g. "my_conda.cmd", put it in the Application Data folder.
  2. Configure it to be started automatically whenever you open cmd. This setting is in Registry:
    key: HKCU\SOFTWARE\Microsoft\Command Processor
    value: AutoRun
    type: REG_EXPAND_SZ
    data: "%AppData%\my_conda.cmd"

from this answer: https://superuser.com/a/302553/143794

merv
  • 67,214
  • 13
  • 180
  • 245
FistOfFury
  • 6,735
  • 7
  • 49
  • 57
  • 1
    Thank you very much. I found that with my version of Mac I am unable to get rid of Python 2 -- so followed the recommended action to set up two separate environments, but it was very annoying to have to remember to activate the environment on every new terminal window. This works! – Livvy Jeffs Sep 12 '17 at 14:51
  • This leads to a broken pipe and frozen command prompt when I do this under Windows 10. – aaronsnoswell Apr 10 '18 at 14:17
  • Anyone on Windows 10 have better luck with this? – Wassadamo Nov 22 '18 at 22:10
  • 4
    This should be the accepted answer (at least for unix based Conda) ! – DrGorilla.eth Oct 25 '19 at 12:02
  • 2
    Users should also set `conda config --set auto_activate_base false`, to skip activating **base** during the Conda initialization code. – merv Aug 02 '22 at 17:06
53

Under Linux there is an easier way to set the default environment by modifying ~/.bashrc or ~/.bash_profile At the end you'll find something like

# added by Anaconda 2.1.0 installer
export PATH="~/anaconda/bin:$PATH"

Replace it with

# set python3 as default
export PATH="~/anaconda/envs/python3/bin:$PATH"

and thats all there is to it.

MartyMacGyver
  • 9,483
  • 11
  • 47
  • 67
Jev
  • 1,163
  • 10
  • 13
  • 4
    And what is the question? – Dieter Meemken Dec 19 '15 at 22:53
  • I tried this solution, but it still shows anaconda2 on my path, any ideas? – William Ross Nov 14 '16 at 11:16
  • 18
    This doesn't set the default environment. It appears to circumvent conda and fakes it out to think that "python3" is the default. "conda info -e" still shows "root" as the current environment. I am still looking for a way to tell conda which is the default. – MrMas Nov 23 '16 at 19:47
  • 10
    This doesn't set the default environment from conda's perspective. It appears to circumvent conda and fakes it out to think that "python3" is the default. "conda info -e" still shows "root" as the current environment. To fix this, you need to also set CONDA_PREFIX and CONDA_DEFAULT_ENV. Even then, the answers is still a hack; why not just run 'source activate ' since this is already provided with conda as the "built-in" way to accomplish this. – MrMas Nov 23 '16 at 20:28
  • Is there a `.bashrc` or `.bash_profile` on Windows 10? I found one under cygwin while following this https://jattenberg.github.io/PDS-Fall-2013/assets/install/AnacondaPythonInstallationGuide.pdf. But I'm not sure it's what I should change to get the OP's desired functionality. – Wassadamo Nov 22 '18 at 22:12
33

For windows Anaconda comes with Anaconda Prompt which is a shortcut to cmd and can be used run conda commands without adding anaconda in PATH variable. Find the location of it, copy and rename the copy (say myenv_prompt). Right click myenv_prompt and select properties in the context menu.

enter image description here

The Target form of Properties window should already be filled with text, something like %windir%\system32\cmd.exe "/K" C:\Users\xxx\AppData\Local\Continuum\Miniconda3\Scripts\activate.bat C:\Users\xxx\AppData\Local\Continuum\Miniconda3\ There are three parts of this command 1)start ...\cmd.exe 2)run ...\acitvate.bat with environment 3)...\Miniconda3\

Change 3rd part to path of the environment (say myenv) you want as default i.e. fill the Target form something like %windir%\system32\cmd.exe "/K" C:\Users\xxx\AppData\Local\Continuum\Miniconda3\Scripts\activate.bat C:\Users\xxx\AppData\Local\Continuum\Miniconda3\envs\myenv

Now myenv_prompt will act as shortcut to start cmd with myenv as the default environment for python. This shortcut you can keep in start menu or pinned in taskbar.

One advantage of this method is that you can create a few shortcuts each having different environment as default environment. Also you can set the default folder by filling Start in form of the Properties window

Hope this helps

PS:It is not required to find Anaconda Prompt and can be done by changing target of any shortcut. But you will require to know path of cmd.exe and activate.bat

Kushdesh
  • 1,118
  • 10
  • 16
  • 7
    This should be the top answer – James McCormac Jul 02 '19 at 10:47
  • I use this method, but my first part is a little different:`C:\Windows\System32\cmd.exe /c start ""` Not sure how I arrived at that solution, but here's a post I made regarding it's [syntax](https://superuser.com/questions/1335745/) – xtian Aug 03 '19 at 15:11
13

Just activate your py34 environment when you load your terminal/shell.

If you use Bash, put the line:

conda activate py34

in your .bash_profile (or .bashrc):

$ echo 'conda activate py34' >> ~/.bash_profile

Every time you run a new terminal, conda environment py34 will be loaded.

Brandt
  • 5,058
  • 3
  • 28
  • 46
10

The correct answer (as of Dec 2018) is... you can't. Upgrading conda install python=3.6 may work, but it might not if you have packages that are necessary, but cannot be uninstalled.

Anaconda uses a default environment named base and you cannot create a new (e.g. python 3.6) environment with the same name. This is intentional. If you want your base Anaconda to be python 3.6, the right way to do this is to install Anaconda for python 3.6. As a package manager, the goal of Anaconda is to make different environments encapsulated, hence why you must source activate into them and why you can't just quietly switch the base package at will as this could lead to many issues on production systems.

cgnorthcutt
  • 3,890
  • 34
  • 41
  • 1
    What if my base environment is broken and I want to replace it with one that works? – endolith May 19 '20 at 03:44
  • 1
    @endolith _What if my base environment is broken and I want to replace it with one that works?_ Reinstall Conda. – AMC Jun 10 '20 at 00:58
  • 1
    @AMC I've reinstalled so many times. I want a way to recover a good state without redoing everything – endolith Jun 10 '20 at 14:47
  • 2
    @endolith You’ve reinstalled multiple times to fix the same exact issue? How did it end up broken? We should discuss this elsewhere, I created a [chat room](https://chat.stackoverflow.com/rooms/215679/room-for-amc-and-endolith). – AMC Jun 10 '20 at 14:48
10

If you want Anaconda Navigator to default to Virtual Env you created, go to file > Preference and select default conda env in drop down lint: enter image description here

If you want Anaconda command automatically opens to virtual env without having to type activate envName, do this:

Right click on conda shortcut > go to properties and change the Target to something like this:

%windir%\System32\cmd.exe "/K" C:\Anaconda\Scripts\activate.bat C:\Anaconda\envs\p37

Optionally you can set your default working dir as well, like I did in snapshop below: enter image description here

gl

Sean
  • 680
  • 7
  • 10
  • Does this set a default for anything other than Anaconda Prompt? E.g., if you start up PowerShell, will it also default to this new environment? – merv Jul 25 '22 at 17:11
  • The Anaconda Navigator GUI preference did nothing for me. But your suggestion to add the path to the environment as a parameter to activate.bat did the trick! Thank you!! ` C:\Anaconda\Scripts\activate.bat C:\Anaconda\envs\p37` – Michael Currie Mar 26 '23 at 13:40
7

Change permanent

conda install python={version}

Change Temporarily

View your environments

run conda info --envs on your terminal window or an Anconda Prompt

If It doesn't show environment that you want to install

run conda create -n py36 python=3.6 anaconda for python 3.6 change version as your prefer

Activating an environment (use Anaconda prompt)

run activate envnme envnme you can find by this commandconda info --envs as a example when you run conda info --envs it show

base * C:\Users\DulangaHeshan\Anaconda3 py36 C:\Users\DulangaHeshan\Anaconda3\envs\py36

then run activate py36

to check run python --version

In Windows, it is good practice to deactivate one environment before activating another. https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html?highlight=deactivate%20environment

Dulanga Heshan
  • 1,335
  • 1
  • 19
  • 36
  • 15
    I think the OP wants a solution that changes the base environment to py36 so they don't have to run `activate py36` every time on startup. – Wassadamo Nov 22 '18 at 22:09
5

activate.py is hardcoded to emit conda activate base\n into your shell profile when you evaluate the shell hook produced by conda shell.zsh hook.

you can suppress this hardcoded "auto-activate base" via:

conda config --set auto_activate_base false

then, in ~/.zshrc, ~/.bashrc or wherever you source your shell profile from, you can append the following (after the conda shell hook) to explicitly activate the environment of your choosing:

conda activate py34
Birchlabs
  • 7,437
  • 5
  • 35
  • 54
3

On Windows, create a batch file with the following line in it:

start cmd /k "C:\Anaconda3\Scripts\activate.bat C:\Anaconda3 & activate env"

The first path contained in quotes is the path to the activate.bat file in the Anaconda installation. The path on your system might be different. The name following the activate command of course should be your desired environment name.

Then run the batch file when you need to open an Anaconda prompt.

3

Here is the solution I found for autoactivating my preferred environment on a Windows 10 system:

  • Open anaconda prompt & use 'conda env list' to find the location of the environment you wish to use.

    enter image description here

  • Go to the start menu, right-click 'Anaconda Prompt' and go to file location. enter image description here

  • Create a copy of this shortcut file

  • Open its properties & change the target to the location of your preferred environment.

    enter image description here

Now every time you open anaconda prompt through this shortcut it will automatically load your chosen environment.

BOT_bkcd
  • 321
  • 3
  • 12
2

I wasn't satisfied with any of the answers presented here, since activating an environment takes a few seconds on my platform (for whatever reason)

I modified my path variable so that the environment I want as default has priority over the actual default.

In my case I used the following commands to accomplish that for the environment "py35":

setx PATH "%userprofile%\Anaconda3\envs\py35\;%PATH%"
setx PATH "%userprofile%\Anaconda3\envs\py35\Scripts;%PATH%"

to find out where your environment is stored, activate it and enter where python. I'm not sure yet if this approach has any downsides. Since it also changes the default path of the conda executable. If that should be the case, please comment.

dominik andreas
  • 155
  • 1
  • 7
2

For Jupyter and Windows users, you can change the Target path in your Jupyter Notebook (anaconda3) shortcut from C:\Users\<YourUserName>\anaconda3 to C:\Users\<YourUserName>\anaconda3\envs\<YourEnvironmentName>

you could do the same thing for the Anaconda Prompt..etc.

After changing the path you can check your active environment by opening a terminal in Jupyter and run conda info --envs.

enter image description here

Hamza
  • 171
  • 1
  • 10
1

I got this when installing a library using anaconda. My version went from Python 3.* to 2.7 and a lot of my stuff stopped working. The best solution I found was to first see the most recent version available:

conda search python

Then update to the version you want:

conda install python=3.*.*

Source: http://chris35wills.github.io/conda_python_version/

Other helpful commands:

conda info
python --version
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Brad123
  • 877
  • 10
  • 10
  • _My version went from Python 3.* to 2.7 and a lot of my stuff stopped working._ Unless I'm missing something, this doesn't appear to be related to the question and what is described in the OP. – AMC May 18 '20 at 02:43
0

Create a shortcut of anaconda prompt onto desktop or taskbar, and then in the properties of that shortcut make sure u modify the last path in "Target:" to the path of ur environment:

C:\Users\BenBouali\Anaconda3\ WILL CHANGE INTO C:\Users\BenBouali\Anaconda3\envs\tensorflow-gpu

preview

and this way u can use that shortcut to open a certain environment when clicking it, you can add it to ur path too and now you'll be able to run it from windows run box by just typing in the name of the shortcut.

Kream
  • 131
  • 1
  • 3
  • 12
0

Tried both source activate default_3_9 and source conda activate default_3_9

but worked conda activate default_3_9

0

I'm trying to update Anaconda in order to use Python 3.10.4 and then Spyder 5.3.2. Actually, I wanted to set the Python interpreter used by Pycharm inside the Spyder console but it required the newest Spyder version. I didn't try all the possible solutions (it's pending for me to use the window batch and modifying path solutions given here) but:

  1. Since I couldn't update the Anaconda base due to the well-known error on the "Solving environment". Then Python and Spyder remain the same.

  2. Creating a new env allows to get the last Python and then his newest Spyder version but it doesn't actualize the Anaconda shortcuts and even the Anaconda navigator if you set it to this new env still has some inconsistencies like keeping the older Spyder version in his menu.

  3. Besides, on point 2, changing the shortcuts target path doesn't work for me.

  4. Finally, I create a new shortcut of the Spyder file from the Scripts folder inside the environment directory ( C:\Users<userName>>\Anaconda3\envs<EnvName>\Scripts )

I couldn't use the default Anaconda shortcuts but I have what I wanted and quick access.

Jony
  • 51
  • 9
0

Open the Anaconda prompt shortcut and look at the target :

%windir%\System32\cmd.exe "/K" C:\Users\MyUser\Anaconda3\Scripts\activate.bat C:\Users\MyUser\Anaconda3

You can actually add a command to be executed at the end while the prompt is starting. For your need, you can add & conda activate my_environment.
This is what it should look like :

%windir%\System32\cmd.exe "/K" C:\Users\MyUser\Anaconda3\Scripts\activate.bat C:\Users\MyUser\Anaconda3 & conda activate my_environment