Creating Python Virtual Environment with Pip
Tags: python, virtualenv, beginners, programming
Published: 2021-11-20 10:33:54

There are two known Python versions;
1. Python2 that got deprecated in the beginning of 2020.
2. Python3 that is being used currently in many projects.

A requirement is that when installing Python in Windows, ensure that you have checked the "Add to PATH" checkbox otherwise Python and pip commands will not be executed in the Command Prompt.
For instructions on installing Python in Windows, refer to my post [here](https://dev.to/jkariukidev/installing-python3-in-windows10-4d47).

### Python Virtual Environment
By default, Python installs modules or packages system wide which can become an issue if he program(s) in use or being developed or tested require different versions of the same module.  A Python virtual environment is comes into use for the above mentioned challenge and can be defined as a self-contained directory tree that hosts a Python installation and some given packages. For more on definition of it refer to [Python documentation](https://docs.python.org/3/tutorial/venv.html#introduction) 

### Pip
Python versions 2.7.9 / 3.4 and above have pip shipped in their installers therefore one doesn't have to install pip separately. However for earlier versions, reference can be made to the following [link](http://pip-installer.org)
Pip is a package management system for Python that can also be used to install various software written in Python / modules and their respective dependencies.
#### Install pip in Windows
To install pip in Windows, ensure that you click on  'Customize installation' option shown below and then check the checkbox labelled 'install pip'.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6imwp20td4scywp9rzk0.png)
check the checkbox labelled 'pip' as shown below.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/61ua8p2dweux2qsyvca8.png)
### Check pip 
It is always good practice to ensure that pip is installed and working as it should before taking the next steps. To check pip installation in Windows, open Command Prompt (CMD) or Windows Terminal and type the following command and note that the letter 'V' is in upper case.
```
pip -V
```
which should output the version and path to the location of it as shown below.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a0wkcp1yfx0s094tepaz.png)
After confirming the installation, the next step will be to install virtual environment package.
### Installing <em>virtualenv</em> package 
We can now use pip to install virtualenv package and for this we shall install it system-wide using the following command that one can paste inside CMD/terminal.
```
python -m pip install virtualenv
``` 
Ensure the PC is connected to the internet so as to download the packages and their dependencies. The installation appears as shown below.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9tnzwrv6z2shzesydaz0.png)
If you get the warning (shown in amber) that indicates you need to upgrade pip, then run the following command to upgrade pip to the latest version.
```
python -m pip install --upgrade pip
```
The upgrade process in the command window appears as shown below. The pip version has been upgraded from version 21.2.3 to 21.3.1.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cftgweqvcypqrwy9k4ad.png)
### Creating Virtual Environment
Let us create a virtual environment by running the following command in the CMD.
```
python -m virtualenv my_env
```
The above command creates a new python virtual environment named <em>my_env</em> in the current directory. The creation process appears as shown below.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7s4hz1g7l6ixwhhfsozv.png)
The folder that has been created appears as shown below. We won't go into the details of the folder otherwise if interested, refer to the Python pip documentation.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ydt9pgea1d7iwj1418rg.png)
The next step will be to activate the just created virtual environment.
### Activating the Virtual Environment
To use the virtual environment, it has to be activated otherwise if not, one will be using the system-wide pip which is not recommended and may lead to dependency issues. To activate the virtual environment run the following command.
```
my_env\Scripts\activate
```
which enters into an active environment denoted by the name in the parentheses as shown below.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/djufk24uwkjvaa6feigs.png)
To test the environment run the previously run command for checking the pip version, <em>pip -V</em> command which will show the version and location of pip shown below. Note the location is different from the pip that is system-wide.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nlyyp6wam4znmxt8edmo.png)
To check on the installed packages run the following command.
```
pip list
```
which will show all the packages that are installed and these are the default packages.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aseezdn7rqn2rugdgwyd.png)
To show the details of a package for instance, pip package which exists in the current virtual environment, type the following command in the command line.
```
pip show pip
```
which displays some information about the package as shown below.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wk4wfpgde8xjp47krwwt.png)
The last command will be for checking outdated packages which can be done by running the following command.
```
pip list -o 
```
which shows the package name, current and latest version.
### Conclusion
Pip and virtualenv can be used to create and manage isolated environments and python projects alike and are actually being utilized a lot. Therefore it would be of great importance to learn about them by referring to the following:
1. [Pip documentation](https://pip.pypa.io/en/stable/)
2. [Python Virtual Environments and Packages](https://docs.python.org/3/tutorial/venv.html) </br>
To close, one has to deactivate the environment by running the following command.
```
deactivate
```
and this will return to the usual commandline without the environment name inside the parentheses.
Keep posted for future posts on virtual environments and subscribe to my posts as well.