Want to know how to download and install Python packages?
Learning how to download and install packages in Python is one of the most important stepping stones to becoming a Python developer.
Using packages where possible in your Python projects is a great practice because you do not have to reinvent the wheel (meaning waste your time on coding functionality that has already been coded before) as well as you complete your projects in the fraction of time it takes to build the projects without any packages.
In this short guide, we will show you step-by-step on how to download and install Python packages, no matter which operating system that you’re on — Windows, macOS or Linux-running computer.
Let’s dive right in!
Checking If Python and PIP Is Installed
Checking Python
First and foremost, before we go any further, we will need to make sure that we have Python installed on our computer. You can test this by simply running the following command in your Command Prompt (Windows) or Terminal (macOS/Ubuntu Linux)
python --version
When inputted, you should get back an output that shows as Python 3.6.3. If it doesn’t show and it throws an error like ‘Python’ cannot be found, you will have to install the latest Python 3.x video from python.org.
Please refer to our Python installation guide for step-by-step instructions on how to update Python on Windows, macOS or Linux-based machines.
Checking PIP
If Python is working fine, then the next thing to check would be pip. Pip, as we’ve aforementioned before in our previous post, is the standard package manager for Python. It allows you to install and manage Python packages that are not part of the Python standard library.
It’s a powerful tool to have in your arsenal and can significantly help reduce the development time and complexity that you experience with your Python projects.
To check if PIP is installed correctly, simply enter the following command:
pip --version
You should get back an output that shows the pip version (as shown below)
pip 20.1.1 from c:\users\administrator\appdata\local\programs\python\python38-32\lib\site-packages\pip (python 3.8)
If PIP for some reason is not installed and you get an error when checking, then make sure to first update your Python version to the latest version and make sure that PIP is installed. For Linux, you may have to install PIP separately, and you usually do this with the help of a package manager like Advanced Package Tool (apt). Please refer to our previous post on how to install PIP on Linux using package managers as well as other operating systems.
Once you’ve made sure that both Python and PIP is working on your computer, you can go ahead move onto the second part of this guide which is downloading Python packages.
Steps to Download and Install Python Packages
The easiest and most reliable way to download Python packages and install them is to use PIP. It just involves a single line of command, and you can install the hundreds upon millions of PIP packages that are available from PyPI central repository.
Here’s how you download and install Python packages using PIP:
- First, open up your Command Prompt (Windows) or Terminal (macOS or Ubuntu Linux). If you’re on Windows, we recommend that you run Command Prompt as an administrator by right-clicking on the Command Prompt app after you’ve searched for it from your Start menu, and then selecting Run as administrator.
- Once you have your choice of command line open, cd into the location where your Python script lies. For the purposes of this guide and keeping this simple, our Python script lies in the desktop homepage so we will just cd into Desktop. This may vary if you’re using macOS or Ubuntu Linux, but the steps are pretty much the same, and you just need to cd into the location where your script lies.
- Now, this is where the fun begins! You can type the pip install command (as shown below) and provide the name of the Python package you want to download and install.
pip install package-name
Let’s just say I want to download the popular web scraping package scrapy so that I can scrape Turing for the published posts. To install scrapy, I would just write out like this below:
pip install scrapy
- Once you’ve entered that command, the package will be downloaded and installed. You should see a message when the package is successfully installed as “Successfully installed package-name-version”. In our case, it shows that scrapy was successfully installed at the latest version (at the time of this writing) which is 2.3.5.
That’s pretty much it! You can now repeat the above steps to download and install as many PIP packages as you want for your projects.
What if I want to remove a Python package which I had already installed?
It’s a common situation we find ourselves in where we would install a Python package at first, only to not want it later because it doesn’t meet our uses. If so, then you can uninstall the package using PIP too!
Simply enter the following command into your Command Prompt (Windows) or Terminal (macOS or Ubuntu Linux):
pip uninstall package-name
This will uninstall the Python package that you had installed previously. For example, say I want to uninstall the scrapy package which I had installed from above, I would just need to enter:
pip uninstall scrapy
You will be prompted again to confirm your deletion of the package. Simply type y and then hit the Enter key. The package will be proceed to uninstall.
That’s it!
Helpful Tips
- If you want to download Python packages for Anaconda, then you can download packages using PIP via the Anaconda Prompt. Open the Anaconda Prompt (or Terminal depending on which operating system you’re on) and enter:
pip download package-name options
- If you want to download source code packages in Python from Github, then you can first install Git onto your system, then simply git clone https://github.com/bitcoin/bitcoin.git. You can get the URL via the Code button on the Github project page for the package you’re trying to install.
- Alternatively, if you dislike using the command line to install packages in Python from GitHub, then you can always just Download ZIP of the project and save the project as a file on your desktop and then open your project from there into your IDE of code editor of choice.
There you go. You can now download Python packages and install them for any project you are building. Experiment with as many various Python packages as you can and utilize them to bring the best out of your project without having to reinvent the wheel for most functionality. Learning to download and make use of packages is one of the most crucial and most powerful skills you can have in your arsenal as a Python developer.
Hopefully, you’ve found the above steps useful. If you have any questions feel free to let us know down below and we will try our best to assist. 🙂
And as always, happy coding!