Social Widget

Responsive Ads Here

Tuesday, November 26, 2019

pip in python - installing, uninstalling and upgrading packages

pip is the package installer for python. Pip is one of the most famous and widely used package management systems to install and manage software packages written in Python and found in the Python Package Index (PyPI).
Most distributions of Python come with pip preinstalled. Python 2.7.9 and later (on the python2 series), and Python 3.4 and later include pip (pip3 for Python 3) by default. Additionally, you’ll need to make sure you have pip available. You can check this by running:
pip --version
If pip is not present, we can install it manually. To install pip, securely download get-pip.py
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
Then run the following:
python get-pip.py

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="">
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 can speak git to do this through its VCS Support. For example, to install Django’s most recent commit at time of writing, we can run:
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.
pip install -r requirements.txt

This will install all the packages one by one like this.




pip in python


Creating requirements file

We can also create our own requirements file for the packages that we installed in our application by running this following command.
pip freeze > requirements.txt
This creates a requirements.txt file with all the packages that we installed along with their versions.

List packages

Use the list command to list all the packages installed on your system:
pip list



pip list
pip list

Find Packages

By default, pip only finds stable versions. To find pre-release and development versions, in addition to stable versions, we can
$ pip install --pre SomePackage
Find more packages at https://pypi.org/.

No comments:

Post a Comment