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). While introducing pip, we need to clarify what is a package. A package contains all the files you need for a module which are Python code libraries you can include in your project.
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.
The installation of Python itself should be fairly straight-forward.
Check if PIP is Installed
First of all, you need to open the command window in Windows. You can go to the search bar and type cmd.
python
Before you go any further, make sure you have Python and that the expected version is available from your command line. You can check this by running:
python –version
If you don’t have python downloaded in your computer, download and execute the latest Python from python.org. You can grap the installation package from there.
While either 32-bit (x86) or 64-bit (x86-64) versions should work just fine, I tend to gravitate to 32-bit installs as I have encountered other libraries/modules in the past that only offered 32-bit versions. I have no idea if those modules that pushed me to 32-bit in the past still do not support 64-bit, but I’m a creature of habit.
Verify a successful installation by opening a command prompt window and navigating to your Python installation directory (default is C:\Python35). Type python in the cmd line to launch the Python interpreter.
Microsoft Windows [Version 6.2.9200] (c) 2012 Microsoft Corporation. All rights reserved. C:\Users\Username>python Python 2.7.8 (default, Sept 30 2020, 16:03:49) [MSC v.1500 32 bit (Intel)] on win 32 Type "help", "copyright", "credits" or "license" for more information. >>>
If you are done, we can go a step further to download pip for python.
Pip install
Additionally, you’ll need to make sure you have pip available. You can check this by running:
pip --version
You should already have pip if you have downloaded from the source like we have done. If you are on Linux and installed using your OS package manager, you may have to install pip separately.
If pip isn’t already installed, then first try to bootstrap it from the standard library as:
python -m ensurepip --default-pip
If that still doesn’t allow you to run pip:
- Securely Download get-pip.py
- Run python get-pip.py.
This will install or upgrade pip. Additionally, it will install setuptools and wheel if they’re not installed already.
Ensure pip, setuptools, and wheel are up to date
While pip alone is sufficient to install from pre-built binary archives, up to date copies of the setuptools and wheel projects are useful to ensure you can also install from source archives:
python -m pip install --upgrade pip setuptools wheel
Download 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 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.
Open the command-line interface and tell PIP to download the package you want.
Navigate your command line (cmd) to the location of Python’s script directory, and type the following:
Download a package named "numpy". Just type pip install numpy into command-line. C:\Users\Your Name\AppData\Local\Programs\Python\Python36-32\Scripts>pip install numpy
Using a Package
Once the package is installed, it is ready to use.
Import a package into your project. As an example,
Import and use “numpycase”:
import numpy as np np.zeros(5) array([ 0., 0., 0., 0., 0.]) >>> np.zeros((5,), dtype=int) array([0, 0, 0, 0, 0])
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
The installed package’s version will be greater than or equal to one and less than two in this case.
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