14
Creating Python Virtual Environment with Pip
There are two known Python versions;
- Python2 that got deprecated in the beginning of 2020.
- 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.
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
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
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.
To install pip in Windows, ensure that you click on 'Customize installation' option shown below and then check the checkbox labelled 'install pip'.
check the checkbox labelled 'pip' as shown below.
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.
After confirming the installation, the next step will be to install virtual environment 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.
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.
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 my_env in the current directory. The creation process appears as shown below.
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.
The next step will be to activate the just created 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.
To test the environment run the previously run command for checking the pip version, pip -V command which will show the version and location of pip shown below. Note the location is different from the pip that is system-wide.
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.
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.
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.
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:
- Pip documentation
- Python Virtual Environments and Packages 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.
14