This article will guide you in installing pip, a virtual environment, and activating the virtual environment. The virtual environment is also known as virtualenv for Python2 and vend for Python3.
What is pip?
pip is a tool that allows you to install and manage additional libraries and dependencies that are not distributed as part of the standard library. Package management is so important that pip has been included with the Python installer since versions 3.4 for Python 3 and 2.7. It connects to an online repository of public packages, called the Python Package Index. pip can also be configured to connect to other package repositories, provided that they comply with Python Enhancement Proposal.
What is virtual environment?
A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated Python virtual environments for them. This is one of the most important tools that most Python developers use.
pip can be downloaded and installed using the terminal in Linux by going through the following command:
sudo apt-get install python3-pip python-dev
This installation process first ask to-
a. Allow to use disk space, you have to press yes (y) to continue
b. Download Libraries
c. Unpack File bundles
d. Finish up the Installation
Once successfully completed, run the below command to check the installed version of the pip
Something like the below will be shown by the above command, means your pip installation is complete.
pip 21.1.1 from /<project-path>/env/lib/python3.9/site-packages/pip (python 3.9)
Upgrade the pip installed
Run the below command to upgrade the already installed pip in your system, and once done run the above version check command to see the upgraded version number.
python3 -m pip install --user --upgrade pip
Install virtual environment
If you are using Python 3.3 or above, then venv (virtual environment) is already installed in the system, you are not required to install it again, else run the below command to installed the virtual environment
For Python 3
python3 -m pip install --user virtualenv
For Python 2
pip install virtualenv
Creating a virtual environment
To create a virtual environment, go to your project’s directory and run venv (for Python 3) or
virtualenv (for Python 2).
cd /<path of your working project folder>/ python3 -m venv env OR python -m virtualenv env
The above command will create a folder named env in your project directory, this contains the virtual python installation for the project.
Activating a virtual environment
Run the below command to activate the virtual environment created for the project-
Now if you will run the command which python, it will show the path of the python in your application directory inside env folder.
Leaving/ deactivate virtual environment
If you are switching project or simply wants to deactivate the virtual environment, run the below command