Install uninstall and manage Python packages with pip

Introduction

The power of Python is the very large amount of libraries that are available online. These libraries can be downloaded to your development environment as packages. Libraries installed on your system system are gradually accumulating and their management could be problematic if there were no adequate tools.

The package management in Python can be really an easy thing if you use the right tools and Pip is one of them.

Pip and PyPI – the Python Package Index

Tutti i pacchetti più utilizzati dalla comunità di sviluppatori in Python vengono raccolti da anni in un repository unico e mondiale chiamato In questo repository, tutti i pacchetti vengono ordinati, documentati e versionati con un opportuno controllo per le dipendenze.

All the packages used by the community of developers in Python are collected for years in a single repository called PyPI (Python Package Index). In this repository, all packages are sorted, documented and versioned with a suitable control for all dependencies.

PIP is an essential tool that allows you to download, update (synchronize virtually) all of the repository packages you need, taking care to check the proper dependencies and the compatibility among versions

Pip installation

Generally in most distributions of Python, pip should already be included. In fact, for all versions of Python 2> 2.7.9 and Python 3> 3.4, pip is already installed. Same thing if you have created a virtual environment with virtualenv or pyenv.

Installation is simple

On Ubuntu and Debian

$ sudo apt-get install python-pip

On Fedora

$ sudo yum install python-pip

On Mac

$ sudo easy_install pip

But if it were not so, you can install it by downloading a script in Python specially created and then run it.

wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py

Using PIP – Commands

Pips, in addition to being very useful, it is also very simple to use. From the command line, Just write pip followed by an option (command) which is nothing but the task that we want him to do.

Installing a package

For example, to install a package, simply write

pip install nomepacchetto

Pip will connect with the repository, then perform a search to find the package you requested. It will check if there is a version compatible with your version of Python and with the operating system you are using.

Then it will ask you for confirmation if you want to install with classic choice Yes or No. Then if confirm the installation will begin to install the package and any other packages which are required for its operation.

Uninstall a package

Removing a package is even easier

pip uninstall nomepacchetto

List installed packages

To get the list of installed packages on your system

pip freeze

or

pip list

Search for a particular package in the repository PyPI

Finally if you want to know both the existence and the information of a given packet within the PyPI repository without having to install it, you write the following command:

pip show nomepacchetto

update PIP

However Python is a language that is experiencing great excitement and then every month there are always a number of new releases, including PIP. So it is important to check if PIP itself is upgraded to the latest version

To update PIP simply write

pip install --upgrade pip

and the system will begin to write the following commands

You are using pip version 6.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
C:\Users\Fabio\Anaconda\lib\site-packages\pip\_vendor\requests\packages\urllib3\util\ssl_.py:79: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
 InsecurePlatformWarning
Collecting pip
 Downloading pip-9.0.1-py2.py3-none-any.whl (1.3MB)
 100% |################################| 1.3MB 264kB/s
Installing collected packages: pip
 Found existing installation: pip 6.1.1
 DEPRECATION: Uninstalling a distutils installed project (pip) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project.
 Uninstalling pip-6.1.1:
 Successfully uninstalled pip-6.1.1
 Failed to write executable - trying to use .deleteme logic
Successfully installed pip-9.0.1

Once you have updated PIP, you’ll know which packages have newer versions than those currently installed on your system

pip list --outdated --format=columns

You will get a table with the name of a package, the current version installed on your system and the latest version in the repository

Package Version Latest Type
---------------------------- ------- ----------- -----
argcomplete 0.8.1 1.7.0 wheel
astropy 0.4.2 1.3 sdist
atom 0.3.9 0.3.10 sdist
backports.ssl-match-hostname 3.4.0.2 3.5.0.1 sdist
beautifulsoup4 4.3.2 4.5.3 wheel

Update all packages or packages with a single command

it is strange but true, there is no simple and direct command to update all packages that we have installed on our system. So?

Well there is a fairly complex line with a pipe of different commands that allows you to do this:

pip freeze --local | grep -v '^\e' | cut -d = -f 1 | xargs -n1 pip install -U

and the system will begin to update a package after the other. This may take several minutes.[:]



Leave a Reply