![](https://crypto4nerd.com/wp-content/uploads/2023/07/1l3o_9ZsD2nPcm9JHuJZqUQ-1024x512.png)
Before starting any project, you want to make sure to use a virtual environment to not break your Python installation.
What is a virtual environment?
In short, a virtual environment is a directory with a fresh installation of Python that runs by itself, allowing you to install dependencies locally on that environment, rather than in your whole system.
Why use it?
As you start working on multiple projects, you might want to use different dependency versions for newer projects, and you most likely won’t want to upgrade/downgrade the dependencies for each new project you start, as doing so could cause your older projects to break. With a virtual environment, you start with a fresh install of the Python version of your choice, allowing you to install whatever you may please.
Sounds useful. So how do I create one?
Start by installing Anaconda. Anaconda is a package manager for Python just like pip, meaning it strives to simplify your package installation by making it easy to find and integrate packages to a project without any conflicts. On top of that, it allows you to seamlessly create virtual environments.
Once you have Anaconda installed, you can open the Anaconda Prompt and type the following:
conda create -n name_of_env python=X.YZ
This will create a new folder that will hold your environment. It will also install Python with your specified version. If you don’t specify the Python version, the environment will come clean with no Python installation. I recommend always creating an environment with Python pre-installed to avoid breaking your installation, as if you run ‘pip install’ or ‘conda install’ on an empty environment, it might install libraries on your base environment instead.
Cool, so can I start working on my project now?
NO! You need to activate the environment first, which can easily be done by typing:
conda activate name_of_env
This will now change your active environment to the one you specified. You will also noticed that the prompt changes from
(base) C:Usersgdutra>
to
(name_of_env) C:Usersgdutra>
Similarly, to deactivate an environment, simply run:
conda deactivate
And to delete an environment with all its dependencies, run:
conda env remove -n name_of_env
Now you can start working on your project 🙂
Note that, whenever you open a new Anaconda Prompt, you start at the base environment. This environment should always stay clean, DO NOT INSTALL ANYTHING ON THE BASE ENVIRONMENT. This will save you from headaches in the future. If you do end up installing libraries on the base environment, you can revert the changes by doing the following:
On your base environment, type:
conda list --revisions
This will list all revisions of the environment. A new revision is created whenever new changes are introduced in the environment.
After seeing what revision you want to roll back to, type:
conda install --revision X
And conda will revert the environment to that revision. This also works for any environment other than the base.
Anything else I should know?
There are some useful tips that I learned when managing environments:
- Cleaning cache: As you start creating multiple envs, cache starts to build up in your installation. I recommend running
conda clean --all
every time you delete an environment. This will remove any files not being used by an environment, and it is very safe, so no need to worry about breaking your installation.
- Mamba: Mamba is a tool to speed up the management of conda environments. You might experience very slow times in conda as you try to install new libraries on your env. Mamba attempts to fix that.
It can be installed by following this documentation: https://mamba.readthedocs.io/en/latest/user_guide/mamba.html
While I haven’t used it much, many users had positive feedback for this tool, so it’s worth taking a look.