How To Install PIP to Manage Python Packages On Windows?

Photo of author

By Vijay Singh Khatri

When it comes to developing websites and web applications using the Python programming language, you are surely going to need Python packages to make writing code for you easier. One of the best tools to manage your installed Python packages is pip. pip has found its usage in many applications as it enables developers to download and install third-party Python packages in their system with ease.

If you are installing the latest version of Python, you don’t have to install the pip tool separately. Because the newest version of Python comes with pip pre-installed by default. Here, we are going to show you how you can install the pip toolkit separately in your system and manage the Python packages that you have installed in your system.

What is pip?

pip (Preferred Installer Program) is known to be a standard package manager for Python. With the help of pip, a developer can easily install, delete, upgrade the python packages which he wants to use in his project. There are tons of Python libraries and packages, which don’t come by default in Python. So, to get these libraries, you need to download them separately from the Internet and install them in your Python folder. It might sound easy but the fact is that installing newly downloaded packages in Python is complicated. That’s why developers have created pip to make the installation and uninstallation easier for Python developers.

One thing you need to know about pip is that there is no GUI present. All the commands that you will be using in pip will be written on its command-line utility. There are other package managers for different programming languages, such as Ruby uses Gem and Java uses a package manager called npm. Just like these two, pip has become an industry standard as a package manager for Python.

Installing pip on Windows

Microsoft Windows is the most popular operating system right now. To install pip on your Windows PC, you need to follow these steps:

  • Download the tool get-pip installer script from this link.
  • Once downloaded, open the Command Prompt and type in the following command:
    py get-pip.py

Note: When you are running the above command, make sure you are running it in the folder where you have downloaded the get-pip.py.

Things to Check After Installing pip in Your System

Now that you have installed pip in your system, the next thing you might be doing is installing the Python packages using it. But before you install the package, make sure that you can run the Python version from your command line.

Check Your Python Version

Find out the Python version installed on your computer by using the following command: Python -version.

Once you hit the ‘Enter’ key, you will get an output in the following manner Python 3.6.2. If no output comes, it means you don’t have Python installed on your computer.

Make Sure Your Setup Tools and Wheel are Up to Date

There are many advantages of pip. pip is self-sufficient to install the third-party packages of Python. It can also update the setup tools and wheel projects which can be necessary as your project needs resources from source archives. To find if the setup tools and wheel are updated, type in the following command:

python3 -m pip install –upgrade pip setuptools wheel

If Needed, Create a Virtual Environment

With the help of Python’s virtual environment, a developer can install packages in an isolated location. From that location, the packages can be used by a particular application. Let’s take a simple example to explain how a virtual environment can be helpful. Think about having an application that requires you to have version 1 of Pendulum. But the other project of yours requires you to have version 2 of Pendulum. When something like this happens, if you install both the packages in a single location, then your first application will automatically be using version 2 of Pendulum as the first version will be upgraded. Thus, you need to install the packages separately so that the applications can work on the required resources which are specific to their usage.

Right now, two standard tools are used for creating Python virtual environments. The first one is venv which is a default tool present in the latest version of Python. On the other hand, virtualenv is a tool that needs to be downloaded and installed separately.

Write this command to make virtual environment using venv

py -m venv <DIR>

<DIR>\Scripts\activate

Write this command to make a virtual environment using virtualenv

virtualenv <DIR>

<DIR>\Scripts\activate

We are explicitly using source under the Unix shells to make sure that the variables present in the virtual environment are set within the current shell and not in a subprocess. As a result, when a developer is working on Windows, they should not be using source commands. Instead, they should be using the activate command to run the script from the command shell.

When you are running multiple virtual environments, maintaining and managing each of them separately can be quite a tedious task. Thus, you need to use a higher-level tool called Pipenv, which can automatically handle all your separate virtual environments for specific projects and applications which you are developing using Python.

Using pip to Install PyPi

Python Package Index PyPi can be easily downloaded and installed using pip. A user can install the basic PyPi version or use the requirement specifier to install PyPi with a specific set of resources. A requirement specifier is composed of a project name which is followed by an optional version specifier. We have provided below all the different commands which you will be using to install various versions of PyPi using pip.

  • To install the latest version of PyPi: py -m pip install : “XYZProject”
  • To install the specific version of PyPi in your XYZProject: “py -m pip install “SomeProject==1.2”
  • If you want to install the PyPi greater than or equal to one version, then you need to type in the following command: “py -m pip install “XYZProject>=1”
  • In case you are not sure of the compatibility of the PyPi, then you can use the following command to make pip download and install the compatible version on its own: “py -m pip install “XYZProject~=1.3.4”

Difference Between Source Distributions and Wheels

pip can use either Source Distributions (sdist) or Wheel to install the required Python packages. But in case both of these are present in your computer, then pip will prefer the usage of Wheels. In addition to this, you can easily override the default choice of pip using the no-binary option.

Wheels are a set of pre-built distribution formats which helps a user to have a faster installation in comparison to the (sdist). Moreover, if you are working with projects that contain compiled extensions, then using Wheels is much more preferable to using sdist.

On the other hand, if pip is unable to locate Wheels in your system to install, it will automatically create a wheel locally and then cache it so that it can be used in any future installation. This will eradicate the need to rebuild the source distribution in the future when you are installing a new package.

Upgrading Python Packages Using pip

If you want to upgrade the Python package installed in the specific project, then you need to type in the following command: “py -m pip install –upgrade SomeProject”

In case you want to install a Python package that is isolated to the specific user on your computer, then you need to use the –user flag with the above-written command.

Installing Requirements Files Using pip

One of the primary uses of pip is installing the list of items that are present in the “Requirements files”. If we look at the need of the requirement file, we will see it’s nothing but a set of packages, libraries, and resources that need to be installed by pip. One thing you need to know is that pip does not follow the order when installing the packages that are listed in the requirement files. To run the installation from the requirement file, you need to type in the following command:

py -m pip install -r requirements.txt

Benefits of Using Requirement File for Installing Multiple Packages Using pip

  • First, requirement files are used to hold up the results of the pip freeze so that pip can perform repeatable installations. When this scenario takes place, your file needs to have a pinned version of every package that was installed during the session in which pip freeze was run.
  • When you are not sure of the dependencies which pip is going to install with the package, then you need to use requirement files to force pip to use the proper dependencies during the installation. You can place the required dependencies in your file along with your other requirements and run the file on pip.
  • Furthermore, if there are sub-dependencies that need to be installed, you can write them in the requirement file, and they will be installed by pip. With the use of pip, you can even make pip install the earlier versions of the package and dependencies. Suppose if a ProjectX in your requirement files demands the use of DependencyB, but the latest version (v2.3) of the Dependency has some bug. Then you can force pip to install the older version of the same Dependency using the following command:

ProjectX

DependencyB<2.3

  • Lastly, with the use of requirement files, a user can quickly override the dependencies which are present in the version control and stored in a local patch. For example, your project requires you to work with some dependency from PyPi, but that Dependency has some bug in it, and you don’t have time to get the updated version of the Dependency which comes with the fix.
  • In that case, you could make the copy or clone of the source file, make the fix by yourself once you place it in the VCS by including the tag which relates to the Dependency. You can refer to that Dependency in your requirement file by using that same tag so that pip can install the fix dependency from the recruitment files.

Note: A developer must know the difference between the constraint files and the requirement files. The constraint files are those that are used to control the version of the requirements. They don’t have to deal with the installation and uninstallation of the required packages. Also, the syntax of constraint files is a subpart of requirement files in which several types of syntaxes are not allowed to perform their functions.

Most Beneficial Packages of Python

Given below are the most downloaded Python packages on the Internet; you might have downloaded them too. Or, if you are starting a new project, then these Python packages can help you in creating the application in a much shorter code length.

1. NumPy

With the NumPy installed in your Python project, you can easily use all the basic mathematical operations without installing any special Python packages. On the other hand, if you are going to use some of the complex mathematical operations, then again the NumPy package will be making your coding life much easier. Likewise, with NumPy, you get tools that can build multi-dimensional arrays and can easily calculate the data which is stored in these arrays.

2. Pendulum

If you have just started using Python as your programming language, you know that you can manage the date and time of your application using the DateTime module. Well, there is no doubt that the DateTime module is a great way to do most of the basic time and date management tasks. But if you are looking to work with complex codes which involve dates and times, then Pendulum is the package that will allow users to manage different time zones automatically.

3. Urllib3

There are tons of standard Python libraries which are missing from the basic version of Python. Thus, you need Urllib3 to install the missing packages. This Python package consists of thread safety, connection pooling, support for gzip and deflate encoding, along with the proxy backing for HTTP and SOCKS. A developer needs to know that Urllib3, despite having a similar name to Urllib2, Urllib3 isn’t the successor of the same.

4. Six

Six is a Python package that is compatible with Python 2 and 3. As a result, it works perfectly fine with the codebases which work on Python 2 and 3. With the use of the Six packages, you can support the syntax of both Python 2 and 3 in a single statement. To make it easy for you to understand its working, let’s take an example of six. print().

In Python 3, printing can be done by using the following syntax print () function. While if you are working with Python 2, you can use print for the output without even using the parentheses. But when you have installed Six in Python, you can use six. print () for both Python 2 and 3, and the code will work flawlessly.

Conclusion

These were some of the methods which can help you install the various versions of pip to manage your Python packages in Windows. You can install pip even on your macOS and Linux systems, and it will work exactly in the same way as it works on Windows. In addition to this, pip is one of the essential tools which every developer needs to have in their systems when they are developing a project using Python as their primary programming language. We hope this article helped you and solved the problems which you were facing while installing the pip on your Windows computer. If there are any questions or queries regarding the same, feel free to drop us a comment below, and we will provide you with the appropriate solution. We wish you errorless code writing!

Leave a Comment