avatarBetter Everything

Summary

The article discusses the utility of Python packages, how to install them using pip, and the benefits of using virtual environments for package management.

Abstract

Python's extensive library of packages is a key factor in its popularity among programmers. These packages, once installed, can be leveraged in Python scripts to perform a wide range of tasks, from web scraping and data visualization to data manipulation. The article provides a simple guide to installing packages using pip, the Python package installer, and emphasizes the importance of virtual environments to manage dependencies for different projects. It also touches on troubleshooting common issues with pip installation and encourages readers to support the author and other writers by joining Medium for full access to articles.

Opinions

  • The author believes that the ability to easily install and use software developed by others greatly enhances a programmer's capabilities.
  • Popular packages like Requests, Matplotlib, and Pandas are highlighted for their specific use cases, suggesting the author's endorsement of their utility.
  • The author suggests that data visualization with Matplotlib is not only useful for data analysis but also for presenting results, indicating its versatility in data science.
  • The use of virtual environments is recommended as a best practice to prevent conflicts between different package versions, reflecting the author's experience with package management challenges.
  • By inviting readers to join Medium, the author expresses a desire for community support and engagement with their content.

The power of Python packages and how to install them using pip

Python is a very popular programming language. An aspect of Python that makes it so popular is the wide range of available packages. Packages are pieces of software that can be installed on a computer and imported in scripts. Once a package is installed and imported a programmer can make use of all its contents.

A Python package contains pieces of software that you can use after installing. Image by catalyststuff on Freepik

Being able to easily install and use pieces of software developed by others opens up an enormous range of possibilities for a programmer. To illustrate what I am talking about, here are some examples of popular packages and what you can do with them.

  • Requests — The requests package makes webscraping possible. Webscraping is gathering data from websites. You can use this package to load the HTML file of a webpage into Python. The requests package also enables programmers to communicate with APIs. An API is a system to send and receive data.
  • Matplotlib — The matplotlib package can be used to make graphs. For data scientists data visualizations can be useful both for eploring data as presenting results.
  • Pandas — The pandas package offers dataframes. Dataframes can be used to work with data stored in a table format.

Installing Python packages

If by now you are excited to try some packages, you might ask yourself: How can I install packages? Well, here I have a simple tutorial for you.

Step 1 Open the application ‘Command prompt’ on your Windows computer. Or the application ‘Terminal’ on your Apple computer.

Step 2 In the opened application, type the following command and hit enter. In our example we will install the pandas package and so we use the package-name: pandas.

pip install pandas

And with those 2 steps you are done installing Python.

Importing Python packages and using them

To use the contents of a Python package you first have to import it in your script. This can be done with an import statement. Import statements are typically at the top of a script. This is an example of importing the requests package.

import requests

Among the contents of the requests package is the get function. If you want to use a something from a package you have to specify it comes from the package. This way Python knows where to find it. You specify it by writing the package name + a dot (.) + the name of the function.

This is how you call the get function from the requests package:

requests.get(url)

Troubleshooting problems with pip install

  • Are you connected to the Internet? Since you are downloading software from an online repository you will need an Internet connection to use pip install.
  • Do you have pip installed? pip is a package installer for Python. It gets automatically installed if you use Python version 3.4 or higher. That is if you use the default settings in the Python installer.

To learn more about the installation and usage of pip visit PyPI’s website. PyPI stands for Python Package Index, it is where all the packages are stored.

Tip for working with packages

To prevent problems with using different packages and package versions you can install the packages for different projects in isolated environments.

Such an isolated environment is called a virtual environment. They are easy to make and use. You can read more about virtual environments in this short article:

Thank you for reading!

You can get full access to all my posts by joining Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium:

Python
Software Development
Pip
Programming
Data Science
Recommended from ReadMedium