Python is great as a first programming language because it is concise and easy to read. The syntax rules of Python are easy to understand and allow you to do your work in minimal lines of code.
In Python you can use English keywords instead of more punctuation, unlike other programming languages. Many top programmers around the world will refer you to Python as the language to learn if you are looking to understand programming and pursue a career in the world of computing.
This is because of several reasons, among which a major reason is python virtual environments.
Why would you need a Python Virtual Environment?
A new python installation installs basic packages along with its executable files. These packages are what make python one of the most used languages in the world. Programmers often say, if there is a solution you can’t seem to figure out how to code, there is a high probability a python package (or library) already exists for it.
However, the packages you need may vary depending on the solution you are coding, and like all other languages there tend to be compatibility issues between packages. Or, the package you need might only work for a specific version of python. That’s where python virtual environments come in handy.
A python virtual environment is technically only a directory tree. However, the directory contains python executables files and other files which indicate that it is a virtual environment. This directory in a way acts as an independent installation of python. You can create a python virtual environment that has only the base packages, and the ones you specifically need.
Benefits of a Python Virtual Environment and when you can/should use it
A python virtual environment has multiple benefits. It isolates a project from your base installation, and other environments as well. This helps when you are working on multiple projects simultaneously which each need different combinations of packages/libraries. By creating a virtual environment for each project, you know which environment has which packages installed and helps reduce conflicts.
With the vast number of python packages available, you are bound to come across some that have a dependency on the python version. Python virtual environments help solve this issue as well. Even if your base installation is of a higher, or lower python version than the one you need, you have the ability to create a virtual environment with a specific version of python.
How to make a Python Virtual Environment
There are two different ways to make a python virtual environment:
- using the Anaconda distribution, or
- using the virtualenv python package.
In this article, we will be discussing how to make a Python Virtual Environment using the Anaconda distribution.
Building a Python Virtual Environment with Anaconda
First, you must download and install the Anaconda individual edition if you don’t have it already. Next, you must be able to run the “conda” command in your terminal. If you are unable to run the conda command, please refer to this solution.
For creating a new environment with the same python version as your base installation and all default packages open your command prompt in windows or terminal if you are using a LINUX system such as MAC or Ubuntu and type in the command:
$ conda create --name tutorialenv
When conda asks you to proceed, type Y. This will have created a virtual environment with the name “tutorialenv”.
If you require an environment that used a specific version of python you can use the following command:
$ conda create --name tutorialenv python=3.5
You can type any old, or current version of python to be installed.
To create an environment with a specific package:
$ conda create --name tutorialenv pyserial
To create an environment with a specific version of python and a specific package:
$ conda create --name tutorialenv python=3.5 pyserial
You can also create an environment by installing multiple packages at the time of creation as well in the following manner:
$ conda create --name tutorialenv python=3.5 pyserial opencv-python
If you need an environment with a specific version of package installed, you can easily do that as well in the following manner:
$ conda create --name tutorialenv opencv-python=18.104.22.168
How to use a Python Virtual Environment
Using a python virtual environment is as simple as it is to create one. All you need to do is open your terminal or an anaconda prompt (if you are familiar with that) and type in the command
$ conda activate environmentname
Or in the case of our examples above:
$ conda activate tutorialenv
You will know the environment is activated when you see the name of your environment in brackets before the directory path on the terminal like (tutorialenv).
However, if you see (base) written it means you are still in your base installation of python and not in your virtual environment.
If you found this article helpful, then check out more of our tech help!
AwsmTips Team consists of some of many awesome experts who want to learn and share new things with people on the internet.