The natural world as a whole or a particular geographical area, especially as affected by human activity is referred to as environment. The place where we live can be referred to as home. In the home we have various section (environment) such as kitchen, bath room, dining room, etc. with various particular functions attached to each.
In programming, we are not just working on an environment, but a virtual environment (VE). Virtual according to Cambridge advanced learner’s dictionary describe something that can be done or seen using a computer and therefore without going anywhere to talking to anyone. The virtual environment servers as a tool that helps to keep dependencies required by different project separated by creating isolated Python environment.
Why Do We Need Virtual Environment?
In the home, cooking and bathing doesn’t occur at same place so its synonymous Python dependencies. Imagine a scenario where you are working on two web-based Python projects, let say one of the projects uses Django 1.9 version and the other Django 1.10 version. How do we handle such project? In such a case, virtual environment comes to play, with VE maintaining the dependencies of both projects becomes easy. Or a case where you have Python 3.7 version installed on your machine, but you need version 3.6 for a data science project.
Creating Python Virtual Environment
To create, that’s to install the VE on our machine use these command:
pip install virtualenv
If you are using Python 3, then you should already have the virtual environment module from the standard library.
To test your installation or test if you have VE on your machine:
virtualenv __version
If you’re using Anaconda distribution to install the VE use the command:
conda install virtualenv
To create an environment run the command:
conda create __name myenv
Where myenv is the name of your VE.
To create an environment with a specific Python version run:
conda create -n myenv python=3.6
To create an environment with specific package run:
conda create -n myenv scipy
Where scipy is the name of the package needed.
Alternatively, we can run these:
conda create -n myenv python=3.6 scipy=0.15.0 asteroid label
Creating Virtual Environment With Anaconda Navigator
Anaconda Navigator is a desktop graphical user interface (GUI) included in Anaconda distribution that allows you to launch applications and easy manager conda packages, environments and channels without using command-line commands. Its available for Windows, macOS, and Linux.
To create a VE from the GUI displayed above, move your cursor to the environments. When clicked it offers options (name fields) such as your environment name and location. After filling the required details, you can also choose to select all needed modules for your project and install them by just clicking a button.
Whenever you want to work with the VE, all you need is to just hover your mouse to the environments selection from the GUI and select the particular VE needed.
NOTE: We can have multiple environments in the Anaconda Navigator.
We can alongside install the IDE (Integrated Development Environment) needed for writing the project, such as Jupyter Notebook, Spyder, JupyterLab, etc.
Conclusion
You’ve successfully learned about the virtual environment and the creation using command-line and Anaconda distribution. Thanks to the huge Python community, there are quite a few tools at your disposal to help with these common problems. As you progress as a developer, be sure to take time to learn how to use these tools to your advantage.