15 October 2019

Create a Virtual Environment With Anaconda

Background:

Sooner than latter we will have to setup a Virtual Environment in Python. And this blog post is an endeavour to provide a method to create a virtual environment using Conda Package Manager for the Anaconda installation. Pls. note that I am running Anaconda on Windows 10 64 bit.


Why Create a Virtual Environment ?

Sometimes we may be running different projects on our computer with each project using its own version of different packages. As an example, I have a project that uses quantopian Zipline which needs Python 3.5 but the present version of anaconda download is using Python 3.7. So trying to run this project in Python 3.7 will not work. Another project was developed on Python 3.6 and Selenium 3.4.3 which supports Firefox browser automation and this will require its own environment. If I try to run the code of the first project in the environment of the second project and vice-versa it will not run.
Essentially , the virtual environment keep the dependencies in a separate Sandbox where we can run them without any conflicts. To give a rough analogy, they are analogous to a Docker Container in DevOps.


Using Conda Manager:

Venv is the default from Python 3.3 onwards. And Anaconda is a python distribution, with installation and package management tools. Since I am using Anaconda, I prefer to stick to the official Anaconda Package Manager called Conda. This translates to lesser chances of any breakage in the anaconda system. Tip: If you are also using Anaconda, stick to conda wherever and whenever  possible for installation. As an example, if I wanted to install the Technical Indicator Library, Ta-Lib , I can either do a pip install or I can search the anaconda cloud for Ta-Lib . It is always better to go with the latter as it can be installed with Conda.

Now lets Create the Conda Virtual Environment with:
conda create -n testenv
where testenv is the name of the new environment.

In the event that we want to setup the environment for running quantopian's zipline which requires Python 3.5, then we can type:
conda create --name testenv python=3.5 -y
The additional ‘-y’ flag essentially informs the command line to say ‘yes’ to all of the prompts that follow. While this is optional, it does save me a little it of a hassle. The -n can also be expanded and replaced with --name


The environment is then stored in the envs folder in your Anaconda directory. After you have created the environment, you can activate it by typing:
conda activate testenv


To Deactivate this environment, we can type:
conda deactivate

To remove this virtual environment completely, type in:
conda remove --testenv --all

By default, the active environment, the one you are currently using is shown in parentheses () or brackets [] at the beginning of your command prompt:
(testenv) $

If you do not see this, run:
conda info --envs

The above will also give a list of all of your environments.

For more detailed information, check out the official documentation on Managing environments

No comments:

Post a Comment