HomeBlogProgrammingWhat is Python PIP? Installation, Usage, Upgrade & Uninstall

What is Python PIP? Installation, Usage, Upgrade & Uninstall

Published
19th Jun, 2024
Views
view count loader
Read it in
9 Mins
In this article
    What is Python PIP? Installation, Usage, Upgrade & Uninstall

    Have you ever thought about how Python programmers easily take care of and get libraries installed without a problem, making robust Features mix smoothly into their work projects? That is because PIP in Python is an easy way to install, control, and share Python packages with less work. 

    Developers can easily add outside libraries and tools to their projects just by using a simple instruction. This simple approach not only saves time but also makes it easier for developers to start using many free tools created by the community.

    In this blog, I’ll explain about PIP in Python. We will know its importance and what it can do to make package installation easier.

    Read about Self in Python here!

    What is PIP in Python?

    Pip is a package manager for Python that allows you to install additional libraries and packages that are not part of the standard Python library such as the ones found in the Python Package Index. It is a replacement for an easy install. If your version of Python is 2.7.9 (or greater) or Python 3.4 (or greater), then PIP comes pre-installed with Python, in other cases, you will have to install it separately. Here is an article about sys.argv command line argument in Python for better understanding. 

    PIP is a recursive acronym for “Preferred Installer Program” or PIP Installs Packages. It is a command-line utility that installs, reinstalls, or uninstalls PyPI packages with one simple command: pip. You may be familiar with the term package manager if you have used other languages like Ruby uses Gem, JavaScript uses NPM for package management, and .NET uses NuGet. PIP has become the standard package manager for Python. 

    The Python installer installs PIP automatically, so it is ready for you to use unless you have installed an older version of Python. You can also verify if PIP is available on your Python version by running the command below:

    PIP in Python

    On running the command mentioned above, a similar output should be displayed which will show the PIP version, along with the location and version of Python. If you are using an older version of Python, the PIP version will not be displayed. Then you can install it separately. 

    You can download pip from this link

    How to Install PIP in Python? [Step-by-Step]

    We will see the installation of PIP in Python in the follow OS: 

    1. For Windows
    2. For Mac 
    3. For Linux 
    4. For Raspberry Pi 
    5. For Ubuntu
    6. For Fedora

    1. For Windows

    Follow the instructions to install pip in Python on Windows 7, Windows 8.1, and Windows 10: 

    • Download get-pip.py installer script from this link. For Python 3.2, download from after that, right-click on the link and select Save As and save it to any safe location on your computer. 
    • Open Command Prompt and navigate to the get-pip.py file where you saved it previously. 
    • Run the command: python get-pip.py 

    In case you are a beginner and struggling to make inroads, we recommend taking a python programming course to shorten your learning curve.  

    2. For Mac 

    Modern Mac systems have Python and pip pre-installed but the version of Python tends to be outdated and not the best choice for serious programming in Python. So, it’s highly recommended that you install a more updated version of Python and PIP. 

    If you want to use the pre-installed Python application but don’t have PIP available, you can install PIP with the following commands in Terminal:

    sudo easy_install pip

    If you want to install an updated version of Python, then you can use Homebrew. Installing Python with Homebrew requires a single command:

    brew install python

    Installing Python with Homebrew will give you the latest version which should come packaged with PIP but if PIP is unavailable, you can re-link Python using the following commands in Terminal:

    brew unlink python && brew link python

    3. For Linux 

    If your Linux distribution came with Python pre-installed, using your system’s package manager you will be able to install PIP. This is preferable since pre-installed versions of Python do not work well with the get-pip.py script used on Windows and Mac. Given below are the commands you should run in order to install pip in your system depending on the version of Python you are using:

    Advanced Package Tool (Python 2.x):

    sudo apt-get install python-pip 

    pacman Package Manager (Python 2.x):

    sudo pacman -S python2-pip 

    Yum Package Manager (Python 2.x):

    sudo yum upgrade python-setuptools 
    sudo yum install python-pip python-wheel 

    Dandified Yum (Python 2.x):

    sudo dnf upgrade python-setuptools 
    sudo dnf install python-pip python-wheel 

    Zypper Package Manager (Python 2.x):

    sudo zypper install python-pip python-setuptools python-wheel 

    Advanced Package Tool (Python 3.x): 

    sudo apt-get install python3-pip 

    pacman Package Manager (Python 3.x): 

    sudo pacman -S python-pip 

    Yum Package Manager (Python 3.x): 

    sudo yum install python3 python3-wheel 

    Dandified Yum (Python 3.x): 

    sudo dnf install python3 python3-wheel 

    Zypper Package Manager (Python 3.x): 

    sudo zypper install python3-pip python3-setuptools python3-wheel 

    4. For Raspberry Pi 

    You are most likely running Raspbian if you are a Raspberry Pi user as it is the official operating system designated and provided by the Raspberry Pi Foundation. PIP comes pre-installed on with Raspbian Jessie. It is one of the biggest reasons to upgrade to Raspbian Jessie instead of using Raspbian Wheezy or Raspbian Jessie Lite. If you are using an older version of Raspbian, you can still manually install PIP. Given below are the commands you should run in order to install pip on your system depending on the version of Python you are using: 

    On Python 2.x:

    sudo apt-get install python-pip

    On Python 3.x:

    sudo apt-get install python3-pip

    Raspbian users, working with Python 2.x must use pip while Python 3.x users must use pip3 while running PIP commands.

    5. For Ubuntu

    sudo apt-get install python-pip

    6. For Fedora

    sudo yum install python-pip

    How to Use PIP and PyPI?

    1. PyPI - the Python Package Index 

    After PIP is installed, we need to find a package to install. Packages are usually installed from the repository of software for the Python programming language which is the Python Package Index.

    2. Set environment variable for PIP

    You won’t have to reference the pip install directory again and again if you set an environment variable.

    Set: (default = C:\Python27\Scripts) in your Windows/Linux “PATH” environment variable.

    Getting Started with Python PIP

    Now that we know what PIP is and have successfully installed it on our computer, let's get started on how to use it: 

    Commands in PIP 

    Enter pip in the command terminal and it will show the following output on the screen. 

    Usage:
    pip [options] 

    Commands:

    InstallInstall packages
    DownloadDownload packages
    uninstallUninstall packages
    unzipUnzip individual packages
    bundleCreate pybundles
    helpShow help for commands
    configManage local and global configuration
    freeze
    Output installed packages in required format
    listList installed packages
    wheelBuild wheels from your requirements
    hashCompute hashes of package archives
    completionA helper command used for command completion
    checkVerify installed packages have compatible dependencies
    showShow information about installed packages
    searchSearch PyPI for packages
    zipZip individual packages

    Commonly used commands in pip are install, upgrade or uninstall. 

    General Options

    • -h, --help: Shows help. 
    • --isolated: To run pip in an isolated mode by ignoring environment variables and user configuration. 
    • -v, --verbose: Give more output. Option is additive, and can be used up to 3 times. 
    • -V, --version: Show version and exit. 
    • -q, --quiet: Give less output. Option is additive, and can be used up to 3 times (corresponding to WARNING, ERROR, and CRITICAL logging levels). 
    • --proxy: Specify a proxy in the form [user:passwd@]proxy.server:port. 
    • --trusted-host: Mark this host as trusted, even though it does not have valid or any HTTPS. 
    • --certPath to alternate CA bundle. 
    • --client-cert: Path to SSL client certificate, a single file containing the private key and the certificate in PEM format. 
    • --retries: Maximum number of retries each connection should attempt(5 times by default). 
    • --timeout: Set the socket timeout(15 seconds by default). 
    • --exists-action: Default action when a path already exists: (s)witch,(i)gnore, (w)ipe, (b)ackup, (a)bort). 
    • --cache-dir: Store the cache data in <dir>.
    • --no-cache-dir: Disable the cache. 
    • --disable-pip-version-check: Don't periodically check PyPI to determine whether a new version of pip is available for download. Implied with --no-index. 

    Finding required packages:

    To search any package, i.e. Flask command will be as shown below: pip search Flask 

    The following output will be displayed with all packages and description: 

    • Flask-Cache - Adds cache support to your Flask application
    • Flask-SeaSurf - An update CSRF extension for Flask
    • Flask-Admin - Simple and extensible admin interface framework for Flask
    • Flask-Security - Simple security for Flask apps
    • Flask - A microframework based on Werkzeug, Jinja2 and good intentions 

    How to Install a Python Package Using PIP?

    To install the required package, in our case it is Flask, enter the following command : 

    pip install Flask 

    Pip – Show information 

    To check information about the newly installed packages enter: 

    pip show Flask 
    --- 
    Name: Flask 
    Version: 0.10.1 
    Location: /usr/local/lib/python2.7/dist-packages 
    Requires: Werkzeug, Jinja2, itsdangerous

    How to Uninstall a Package?

    To uninstall any package installed by PIP, enter the command given below. 

    pip uninstall Flask
    Uninstalling Flask:
    ...
    .....
    Proceed (y/n)? 
    Successfully uninstalled Flask 

    That’s all. The PIP application has been uninstalled. 

    How to Upgrade PIP for Python?

    Although PIP application doesn’t receive updates very often, it’s still important to keep the application up to date with the newer versions because there may be important fixes to bugs, compatibility, and security holes. Fortunately, upgrading to the latest versions of PIP is very fast and simple yet quite a few learners often seek help from our experts leading a python programming advanced course on KnowledgeHut.

    A. On Windows 

    python -m pip install -U pip 

    B. On Mac, Linux, or Raspberry Pi 

    pip install -U pip 

    Certain versions of Linux and Raspberry Pi, pip3 needs to be entered instead of pip. 

    Using Requirement Files

    The pip install command always installs the latest published version of a package, but you should install the particular version that suits your code. 

    You would want to create a specification of the dependencies and versions that you have used while developing and running your application, so that there are no surprises when you use the application in production. 

    Requirement files allow you to specify exactly the packages and versions that should be installed on your system. Executing pip help shows that there is a freeze command that displays the installed packages in requirements format. This command can be used to redirect the output to a file to generate a requirements file: 

    pip install Requirements in Python

    The freeze command is used to dump all the packages and their versions to standard output, so as to redirect the output to a file that can be used to install the exact requirements into another system. The general convention is to name this file requirements.txt, but it is completely up to you to name it whatever you want. 

    If you want to replicate the environment in another system, run pip install specifying the requirements file using the -r switch:

    The versions listed in requirements.txt will match those of the packages: 

    $ pip list 

    Package    Version 
    ----------        ---------- 
    certifi2018.11.29 
    chardet3.0.4 
    idna   2.8 
    pip    19.0.1 
    requests     2.21.0 
    setuptools  40.6.2 
    urllib31.24.1 

    You may submit the requirements.txt file to source control and can use it to create the exact environment in other machines. 

    Fine-Tuning Requirements 

    The problem with hardcoding the versions of your packages and their dependencies is that the packages receive frequent updates with bug and security fixes, and you probably want to update to them as soon as they are published. 

    The requirements file format gives you a bit of flexibility to ensure that the packages are up to date by allowing you to enter dependency versions using logical operators, although specifying the base versions of a package. 

    Make the following changes by opening the requirements.txt file in your editor: 

    certifi>=2018.11.29 
    chardet>=3.0.4 
    idna>=2.8 
    requests>=2.21.0 
    urllib3>=1.24.1 

    Change the logical operator to >= to install an exact or greater version that has been published. While you set a new environment using the requirments.txt file, pip searches for the latest version that supports the requirement and installs it. The packages in your requirements file can be updated by running the install command with the --upgrade switch: 

    Fine-Tuning Requirements in Python

    In this case nothing was upgraded because latest versions have already been installed, but if a new version was published for a listed package, then the package would’ve been upgraded. 

    New versions can introduce changes that fix bugs and will make or break your application. In order to fine-tune your requirements, the requirements file syntax supports additional version specifiers. 

    Let us assume that a new version 3.0 of requests is published but it breaks your application as it introduces an incompatible change. In such a case, the requirements file can be modified to prevent 3.0 or higher versions from being installed: 

    certifi>=2018.11.29
    chardet>=3.0.4
    idna>=2.8
    requests>=2.21.0, <3.0
    urllib3>=1.24.1

    Changing the version specifier for the requests package ensures that only the versions which are less than 3.0 get installed. 

    Production vs Development Dependencies

    All packages which are installed during the development of your applications are not going to be application dependencies. During the development process, there are certain packages published to PyPI that are development tools or libraries that can be useful to you. 

    For example, you would require a unit test framework in order to unit test your application. Pytest is a popular framework for unit testing. You would want to install the unit testing framework in your development environment, but not in your production environment because it is not an application dependency. 

    To set up a development environment, you need to create a second requirements file (requirements_file.txt) to list additional tools: 

    # In requirements_file.txt 
    pytest>=4.2.0 

    To do this, you need to install both requirement files using pip: requirements.txt and requirements_file.txt. Pip allows for specifying additional parameters within a single requirements file. The requirements_file.txt can also be modified to install the requirements from the production requirements.txt file: 

    # In requirements_file.txt 
    -r requirements.txt 
    pytest>=4.2.0 

    Notice that the exact same -r switch is being used in order to install the production requirements.txt file. The file format of the requirements file allows you to specify additional arguments right on a requirements file. 

    Alternatives to Python PIP

    Pip is an essential tool for all Pythonistas which is used in developing many applications and projects for package management. This article gives you the basics of Pip for Python, but the Python community is very active in providing great tools and libraries for developers using other applications as well. These include alternatives to pip that try to improve and simplify package management. 

    Here are some package management tools other than pip that are available for Python: 

    • Conda
    • Poetry
    • Pipenv

    Top Cities Where KnowledgeHut Conduct Python Certification Course Online

    Python Course in BangalorePython Course in ChennaiPython Course in Singapore
    Python Course in DelhiPython Course in DubaiPython Course in Indore
    Python Course in PunePython Course in BerlinPython Course in Trivandrum
    Python Course in NashikPython Course in MumbaiPython Certification in Melbourne
    Python Course in HyderabadPython Course in KolkataPython Course in Noida

    Summary

    Keeping track of packages and their different versions is an essential part of making software. Python PIP helps developers keep their projects in good shape by making it easy to update, reduce, and eliminate packages. Knowing how to use different ways helps ensure things work right and stay steady, which is very important for projects with special needs. Once you get a good hold, it will help you to make separate virtual environments for your projects. This separation is a significant change, stopping fights between different project needs and giving each development job a clean start.  

    To gain more knowledge about PIP in Python tips and tricks, check our Python tutorial and get a good hold over coding Python by joining the KnowledgeHut's Python Programming course.

    Frequently Asked Questions (FAQs)

    1What is PIP used for in Python?

    PIP, or "Install Python Packages," is a helper for installing and managing packages in Python. It makes it easier to get, keep track of, and share these packets with others. It is a tool for the command line that makes it easy to install outside libraries, handle package versions and dependencies, and create separate virtual spaces without needing additional tools. PIP links to the Python Package Index (PyPI). This lets developers access a vast collection of community-made packages. It's essential for Python programmers who use it to make their projects better and more efficient. 

    2What is the PIP code in Python?

    The order to use PIP download in Python is easy and keeps a constant way. To put a package, developers use the command `pip install python`. This order makes PIP get and set up the given package from PyPI or other sources you tell it to. More things that can be done include improving packages with 'pip install --upgrade package_name',,' moving them using 'pip uninstall package_name', and handling virtual environments. This simple and clear language makes PIP easy to add to different parts of Python development. 

    3When to use PIP in Python?

    PIP is used in Python when developers need to manage outside stuff, add new packages, or handle package versions for their projects. It is used at the start of a project to set up the development space and later on when making changes when new libraries or updates are needed. PIP is also essential when working with pretend or virtual worlds to separate the things a project needs.

    PIP is a key tool for Python programmers. It helps in setting up projects and during development so that the workflow can be smooth with many kinds of packages from the big world of Python. 

    Profile

    Ramulu Enugurthi

    Blog Author

    Ramulu Enugurthi, a distinguished computer science expert with an M.Tech from IIT Madras, brings over 15 years of software development excellence. Their versatile career spans gaming, fintech, e-commerce, fashion commerce, mobility, and edtech, showcasing adaptability in multifaceted domains. Proficient in building distributed and microservices architectures, Ramulu is renowned for tackling modern tech challenges innovatively. Beyond technical prowess, he is a mentor, sharing invaluable insights with the next generation of developers. Ramulu's journey of growth, innovation, and unwavering commitment to excellence continues to inspire aspiring technologists.

    Share This Article
    Ready to Master the Skills that Drive Your Career?

    Avail your free 1:1 mentorship session.

    Select
    Your Message (Optional)

    Upcoming Programming Batches & Dates

    NameDateFeeKnow more
    Course advisor icon
    Course Advisor
    Whatsapp/Chat icon