Python comes with several built-in modules, but the Python community has more to offer. It’s the modules that makes python so powerful!

Third party modules add so much more functionality to Python. So it’s time to learn how to install these modules so that we can use those in our programs.

The simplest way is to use pip

pip install <module_name>

If you have used npm, then you can think of it as npm of Python.

Side note: The difference is that with npm, npm install by default installs packages locally to a project, whereas pip install by default installs globally.

To install modules locally, you need to create and activate what is called a virtual environment, so pip install installs to the folder where that virtual environment is located, instead of globally (which may require administrator privileges).

Last time, in import-statements wiki we used requests module as an example. As it is a third party module we have to install it separately after installing python.

Installing it would be as simple as pip install requests . You can even pass various arguments along with it. The one that you’ll come across more often is –upgrade. You can upgrade a python module by :

pip install <module_name> --upgrade

For example, to upgrade the requests module to its latest version would be as simple as pip install requests –upgrade.

Before using pip, you will need to install it (it’s quite simple). You can install it from here

Just click on the link. And save the file asget-pip.py Please don’t forget the .py extension. And run it.

An alternative to using pip would be to try easy_install.

Using easy_install is also simple. The syntax is:

easy_install <module_name>

However, pip is more popular than using easy_install.

Note: On some systems where both Python 2 & Python 3 are installed, pip and pip3 will do different things. pip installs the Python 2 version of the package, and pip3 will install the Python 3 version of the package.

For more information on the difference between Python 2 & 3, see this guide. You can check the pip version by doing pip –version and/or pip3 –version:

pip3 --version
pip 18.0 from /usr/local/lib/python3.5/dist-packages/pip (python 3.5)

We can also create a txt file containing a list of modules which should be installed using pip. For example, we could create the file requirements.txt and its content:

Kivy-Garden==0.1.4
macholib==1.5.1
idna==2.6
geoip2nation==0.1.2
docutils>=0.14
Cython

In this file we could also set a version for the installation. After this, by invoking pip with:

 pip install -r <FILE CONTAINING MODULES>
 
          OR IN OUR CASE
 
 pip install -r requirements.txt
 

it should install all the modules listed on the file.

Install a package

Linux / MacOS

$ python -m pip install sampleproject
[…] Successfully installed sampleproject

Windows

C:> py -m pip install sampleproject
[…] Successfully installed sampleproject

By default, pip will fetch packages from Python Package Index, a repository of software for the Python programming language where anyone can upload packages.

Install a package from GitHub

See VCS Support for more information about this syntax.

Linux / MacOS:

$ python -m pip install git+https://github.com/pypa/[email protected]
[…] Successfully installed sampleproject

Windows:

C:> py -m pip install git+https://github.com/pypa/[email protected]
[…] Successfully installed sampleproject

Install a package from a distribution file

pip can install directly from distribution files as well. They come in 2 forms:

Linux / MacOS:

$ python -m pip install sampleproject-1.0.tar.gz
[…] Successfully installed sampleproject
$ python -m pip install sampleproject-1.0-py3-none-any.whl
[…] Successfully installed sampleproject

Windows:

C:> py -m pip install sampleproject-1.0.tar.gz
[…] Successfully installed sampleproject
C:> py -m pip install sampleproject-1.0-py3-none-any.whl
[…] Successfully installed sampleproject

Install multiple packages using a requirements file

Many Python projects use requirements.txt files, to specify the list of packages that need to be installed for the project to run. To install the packages listed in that file, you can run:

Linux / MacOS:

$ python -m pip install -r requirements.txt
[…] Successfully installed sampleproject

Windows:

C:> py -m pip install -r requirements.txt
[…] Successfully installed sampleproject

Upgrade a package

Linux / MacOS:

$ python -m pip install –upgrade sampleproject
[…] Successfully installed sampleproject

Windows:

C:> py -m pip install –upgrade sampleproject
[…] Successfully installed sampleproject

Uninstall a package

Linux / MacOS:

$ python -m pip uninstall sampleproject
Uninstalling sampleproject:
[…] Proceed (Y/n)? y
Successfully uninstalled sampleproject

Windows:

C:> py -m pip uninstall sampleproject
Uninstalling sampleproject:
[…] Proceed (Y/n)? y
Successfully uninstalled sampleproject


Source