Appendix A. Conda Environments

In Chapter 2, I was introducing Conda environments by explaining that (base) at the beginning of a command line stands for the currently active Conda environment with the name base. Anaconda requires you to always work in a properly activated environment even if you are only ever using the default base environment. The activation process is done automatically for the base environment when you start the Anaconda prompt on Windows or the Terminal on macOS. You can be sure that you are in a properly activated base environment if you see (base) at the beginning of the command line. Working inside a Conda environment guarantees you to properly separate the different dependencies for your different projects. If you want to try out a newer version of a package like e.g. pandas without changing your base environment, you can do so in a separate Conda environment. Let’s see how you can create one!

Create a new Conda environment

To create a new Conda environment, run the following command:

(base) $ conda create -n myenv python=3.8

This instructs Conda to create a new environment with the name myenv that uses Python 3.8. When hitting Enter, Conda will show you what it is going to install into the new environment and asks you in the end:

Proceed ([y]/n)?

Hit Enter to confirm or n if you want to cancel. Once the installation is done, activate your new environment like this:

(base) $ conda activate myenv
(myenv) $

As you can see, the environment name changed from base to myenv. You can now use conda or pip to install packages into this new environment without impacting any of the other environments. That way, each of your project gets exactly the packages it needs in the correct version. To deactivate the environment again, type: conda deactivate and you are back in the base environment. You do not have to deactivate an environment before activating another one, Conda is doing this automatically for you.

When you install Anaconda, by default you will always get the base environment activated when you open a Terminal on macOS or the Anaconda Prompt on Windows. How you can change that behavior is shown next.

Disable Auto Activation

Some users don’t like that the base environment is activated automatically upon firing up a new command line. You can change this behavior so that you need to run conda activate base manually before being able to use Python.

Windows

On Windows, you will need to use the regular Command Prompt instead of the Anaconda Prompt. Open the Command Prompt as described in Chapter 2. The following steps will enable the conda command in a normal Command Prompt. Make sure to replace the path in the first line with the path to where you have Anaconda installed.

$ cd C:UsersusernameAnaconda3condabin
$ conda init cmd.exe

Now you can activate the base environment in a normal Command Prompt like this:

$ conda activate base
(base) $
macOS

On macOS, simply run the following command in your Terminal to disable auto activation:

$ conda config --set auto_activate_base false

If you ever want to revert, run the same command again with true instead of false. Changes will come into effect after restarting the Terminal. Going forward, you will need to activate the base directory like this before you can use the python command again:

$ conda activate base
(base) $

Conclusion

Conda environments allow you to create separate Python worlds with different versions of Python and third-party packages. This allows you to easily run separate projects with different dependencies. As some users don’t like if the command line automatically activates the base environment, this appendix has additionally shown you how you can switch this behavior off.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.179.186