What is a package?
A package contains all the files you need for a module. Modules are Python code libraries you can include in your project. The term “package” in this context is being used as a synonym for a distribution (i.e. a bundle of software to be installed), not to refer to the kind of package that you import in your Python source code (i.e. a container of modules).
pip provides various functionalities such as installing and uninstalling packages, the listing of packages, creating a requirements file, etc.
Installing packages using pip
Downloading a package is very easy. Open the command-line interface and tell PIP to download the package you want.
Installing a package
Any package can be installed from the python packaging index by the following command.
pip install SomePackage
This will install the latest version of a module and its dependencies from the Python Packaging Index.
Installing a package of a specific version
similarly, we can also install any packages of a specific version by specifying the version along with the above command. It’s also possible to specify an exact or minimum version directly on the command line.
pip install SomePackage==1.0.4
To install a greater, lesser or intermediate version
we can specify the minimum version to download by,
pip install SomePackage>=1.0.4
This will install the package whose version will be greater than or equal to 1.0.4
We can also specify an intermediate version to download by,
pip install SomeProject>=1,<2 span="">2>
The installed package’s version will be greater than or equal to one and less than two in this case.
Installing a compatible version
To install a version that is compatible with the current version, we can use
pip install SomeProject~=1.4.2
Upgrading the packages
Normally, if a suitable module is already installed, attempting to install it again will have no effect. Upgrading existing modules must be requested explicitly.
Modules can be upgraded by
pip install --upgrade SomePackage
or
pip install -U SomePackage
Uninstalling packages
Uninstalling packages is similar to installing them. we can directly specify the package name and uninstall them like
pip uninstall SomeProject
the example
$ pip uninstall simplejson
Uninstalling simplejson:
/home/me/env/lib/python2.7/site-packages/simplejson
/home/me/env/lib/python2.7/site-packages/simplejson-2.2.1-py2.7.egg-info
Proceed (y/n)? y
Successfully uninstalled simplejson
Installing via Git
It’s quite common to want to pip install
a version of a package that hasn’t been released to PyPI, but is available on its Git repository host, such as GitHub. If the package is pure Python or has a relatively simple build process integrated with setup.py
, it can be installed from source.
pip install git+https://github.com/django/django.git@45dfb3641aa4d9828a7c5448d11aa67c7cbd7966
Installing to the User Site
To install packages that are isolated to the current user, use the --user
flag:
pip install --user SomeProject
For more information see the User Installs section from the pip docs.
Note that the --user
flag has no effect when inside a virtual environment - all installation commands will affect the virtual environment.
Local project installs
pip supports installing local project in both regular mode and editable mode. You can install local projects by specifying the project path to pip:
$ pip install path/to/SomeProject
During regular installation, pip will copy the entire project directory to a temporary location and install from there. The exception is that pip will exclude .tox and .nox directories present in the top level of the project from being copied.
Installing packages from a requirements file
Requirements files give you a way to create an environment: a set of packages that work together. While developing a project we will install the packages one by one. If we want somebody else to run our application on their system, installing the packages one by one is not possible. “requirements” file come in handy here.
So what are requirements files? They are very simple: lists of packages to install.
This is how a requirements file looks like.
we can install all the requirements one by one by running the following command.This will install all the packages one by one like this.
We can also create our own requirements file for the packages that we installed in our application by running this following command.
This creates a requirements.txt file with all the packages that we installed along with their versions.
By default, pip only finds stable versions. To find pre-release and development versions, in addition to stable versions, we can