BlogsDope image BlogsDope

Install and Uninstall Python Packages Using Pip

Jan. 1, 2021 PYTHON LINUX WINDOWS MAC 4638

Would you prefer going to the wholesaler for a single item or will go to the nearest store? 

I think you will prefer going to the nearest store, the same happens when you want to install a library in Python.

You can get the library in two ways either 

  1.  Go to PyPI for finding the library, download the source code, build and install the setup. 
  2. Or you could use the pip.

Pip is a package installer system that we use to install Python packages or libraries that are not distributed as part of the standard library. Since versions 3.4 for Python3 and 2.7.9 for Python2, pip comes installed with the Python package. Pip is easy to use as it has simple syntax.

Check if pip is installed or not

  • Python2
  • Python3
pip -V or pip --version
pip3 -V or pip3 --version

Output:

pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)

If pip is not working, you should check for the Python installation.

  • Python2
  • Python3
python --version
python3 --version

If you will get the following output, it means Python is installed.

​Python 3.6.5

Installing pip in different operating systems

Linux:

  • Python2
  • Python3
sudo apt install python-pip
sudo apt install python3-pip

In the above output, write -y flag in the command so that it by default takes the answer as yes for any confirmation.

Windows:

  • Download the file get-pip.py and store it in the same directory as Python is installed.
  • Press (windows key + R) and write cmd to open the command prompt
  • In the command prompt, go to the path where your python is installed
  • Run the command  python get-pip.py
Get pip in windows
  • Verify if pip get installed or not  pip -V
Check pip version in Windows

Mac:

  • Download the file get-pip,py and store it in the same directory as python is installed or use the following command in  the terminal

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

  • then run the command python3 get-pip.py
  • Verify if pip get installed or not with  pip --version

Python Comparison/Relational Operator

You could use these operators with pip for versions

All these operators work similarly as used in programming, but here we use them for getting the version of our choice like if you want to download a specific version.  

pip install pandas==1.1.5 

Or want to install the version above  

pip install pandas>1.1.0 

Or maybe less than or equal to  

pip install pandas <=1.1.5 

We have different operators like <= , >= , < , > , != , == . You can use any of these according to your requirement.

Installing modules using pip

  • Python2
  • Python3
pip install <package-name>
pip3 install <package-name>

What if you are working in your system on a project with a friend and now your friend needs to go home and work on the project in his system but, he can't remember what modules you have installed and asked you for the list. 

What will you do? Maybe you could check your history and see all the commands you have run till now or you may do it by easy way using pip freeze, pip freeze will give you an output of all modules you have installed.

  • Python2
  • Python3
pip freeze
pip3 freeze

Output:

pandas==1.1.5

numpy==1.19.4

requests==2.18.4

six==1.11.0

However, this command will make things easy only for you. What about your friend? He still needs to install all the modules one by one. So let's try to make things easy for your friend as well.

So what we can do is we can store the output in a text file so your friend can run a single command to install all the modules at once.

Let's store the output in a text file called requirments.txt.

  • Python2
  • Python3
pip freeze > requirments.txt
pip3 freeze > requirments.txt

pip freeze returns the modules with their version, but it is not necessary to write the version of modules in requirements.txt file

Now your friend only has to run the following command to install all the modules in his system.

  • Python2
  • Python3
pip2 install -r requirements.txt
pip3 install -r requirements.txt

It's Done!!!

Uninstalling Modules using pip

  • Python2
  • Python3
pip uninstall <package-name>
pip3 uninstall <package-name>

Your friend wants to uninstall all the modules from your project. Using the above command, we can only uninstall a single module at a time. So how can your friend proceed? It's easy.

Just need to use the requirments.txt file

  • Python2
  • Python3
pip2 uninstall -r requirements.txt
pip3 uninstall -r requirements.txt

I hope it was fun to understand python-pip.

Alternative for pip

For downloading the python modules we have other ways as well :

  • Pipenv: Pipenv is a tool that merges virtual environment and package manager in a single tool. So if you want to test your project in different versions of a module then you can create different pipenv and install the version in it and test your project.
  • Conda: Conda is another package management system that installs, runs and updates dependencies. It is beneficial because you could easily switch between environments. It's used for data science and machine learning. It works best in the Windows. For working with it, you can download the Anaconda that has many features or IDE for Python like conda, jupyter etc.

Liked the post?
Editor's Picks
0 COMMENT

Please login to view or add comment(s).