This tutorial is about installing python package using pip and Virtual Environment. We have venv for Python 3 and virtualenv for Python 2. Both pip and Virtual Environment are tools for managing python packages. Installing python package using pip and Virtual Environment is easy. All you need is to follow along this tutorial.
1. Installing Pip
Windows
Python installer for Windows already contains pip so you should be able to access it by simply.
py -m pip --version
You can upgrade pip to latest version by using command :
py -m pip install --upgrade pip
You can check pip version by using command :
py -m pip --version
Linux / macOS
These distributions already contains package called python-pip. You can upgrade pip to latest version by using command :
python3 -m pip install --user --upgrade pip
You can check pip version by using command :
python3 -m pip --version
2. Installing Virtual Environment
Virtual Environment is the tool used to install python packages for different projects. It minimizes the risk of version conflict for your project.
Windows
py -m pip install --user virtualenv
Linux / macOS
python3 -m pip install --user virtualenv
3. Creating Virtual Environment
Windows
py -m venv env
Linux / macOS
python3 -m venv env
env is the location for the virtual environment. Here in this case the virtual environment is created at the folder called env.
4. Activating Virtual Environment
Before you install your python package firstly you need to activate virtual environment so that all the installs and updates is only reflected in the active virtual environment but not globally.
Windows
.\env\Scripts\activate
Linux / macOS
source env/bin/activate
To leave the active virtual environment you can use the following command :
deactivate
also view : Introduction to Git and GitHub for beginner
5. Installing packages using pip
- Package’s latest version :
pip install <package name>
for example
pip install Django
- Specific version of the package :
pip install <package name>==<version>
for example
pip install Django == 3.0.8
- Latest release of specific version :
pip install package_name>=version,<version
for example
pip install Django>=2.0.0,<3.0.0
- To upgrade package :
pip install --upgrade <package name>
for example
pip install --upgrade Django
- You can use following command to list all the installed packages along with the version :
pip freeze
it will have the output similar to :
google-auth==1.1.1
idna==2.6
pyasn1==0.3.6
pyasn1-modules==0.1.4
requests==2.18.4
- Using requirements file :
you can save all the packages and versions in a file (requirements.txt) and use the file to install all the listed packages with single command. It will help while migrating the project to new system of making backup of the project.
To create requirements.txt you can use following command :
pip freeze > requirements.txt
To install packages from requirements.txt you can use following command :
pip install -r requirements.txt
You can find, install and publish python packages through PyPI website.