Home Artificial Intelligence All you might want to find out about Python virtual environments (using Anaconda)

All you might want to find out about Python virtual environments (using Anaconda)

3
All you might want to find out about Python virtual environments (using Anaconda)

Before starting any project, you would like to ensure that to make use of a virtual environment to not break your Python installation.

What’s a virtual environment?

Briefly, a virtual environment is a directory with a fresh installation of Python that runs by itself, allowing you to put in dependencies locally on that environment, fairly than in your whole system.

Why use it?

As you begin working on multiple projects, it is advisable to use different dependency versions for newer projects, and also you most probably won’t need to upgrade/downgrade the dependencies for every latest project you begin, as doing so could cause your older projects to interrupt. With a virtual environment, you begin with a fresh install of the Python version of your selection, allowing you to put in whatever you might please.

Sounds useful. So how do I create one?

Start by installing Anaconda. Anaconda is a package manager for Python similar to pip, meaning it strives to simplify your package installation by making it easy to seek out and integrate packages to a project with none conflicts. On top of that, it means that you can seamlessly create virtual environments.

Once you’ve got Anaconda installed, you possibly can open the Anaconda Prompt and sort the next:

conda create -n name_of_env python=X.YZ

This can create a latest folder that can hold your environment. It’ll also install Python together with your specified version. In case you don’t specify the Python version, the environment will come clean with no Python installation. I like to recommend at all times creating an environment with Python pre-installed to avoid breaking your installation, as should you run ‘pip install’ or ‘conda install’ on an empty environment, it’d install libraries in your base environment as an alternative.

Cool, so can I start working on my project now?

NO! You might want to activate the environment first, which might easily be done by typing:

conda activate name_of_env

This can now change your energetic environment to the one you specified. You may even 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 possibly can start working in your project 🙂

Note that, every time you open a latest Anaconda Prompt, you begin at the bottom environment. This environment should at all times stay clean, This can prevent from headaches in the longer term. In case you do find yourself installing libraries on the bottom environment, you possibly can revert the changes by doing the next:

In your base environment, type:

conda list --revisions

This can list all revisions of the environment. A latest revision is created every time latest changes are introduced within the environment.

After seeing what revision you would like to roll back to, type:

conda install --revision X

And conda will revert the environment to that revision. This also works for any environment apart from the bottom.

Anything I should know?

There are some useful suggestions that I learned when managing environments:

  • As you begin creating multiple envs, cache starts to accumulate in your installation. I like to recommend running
conda clean --all

each time you delete an environment. This can remove any files not getting used by an environment, and it is extremely protected, so no must worry about breaking your installation.

  • Mamba is a tool to hurry up the management of conda environments. You may experience very slow times in conda as you are trying to put in latest libraries in your env. Mamba attempts to repair that.
    It may 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 price looking.

3 COMMENTS

  1. … [Trackback]

    […] There you will find 3895 more Info to that Topic: bardai.ai/artificial-intelligence/all-you-might-want-to-find-out-about-python-virtual-environments-using-anaconda/ […]

LEAVE A REPLY

Please enter your comment!
Please enter your name here