Installing Python Packages

One of the things which makes Python awesome is the quality and quantity of open source packages available to extend its power. This means the with the right package and just a few lines of code, ridiculously complicated and impressive tasks can performed, such as as sentiment analysis of text, powerful 3D graphical image manipulation, advanced computational processing and much more besides.

None of this is much use though unless you know how to install Python packages!

Python Built-In Modules

Be aware that not all extended functionality requires installing packages. Several modules come shipped with a standard Python installation. These include

  • math for various mathematical functionality
  • os for interacting with the operating system
  • random for working with random numbers
  • time for working with time-related processes
  • collections for access to some fancy data structures

You can go ahead and explore these without any further installation steps with your standard installation, if you haven’t done so already. All you need to do is import them at the top of your Python files using import math, for example.

Here’s a simple example of using a built-in Python module:

import math

print(math.pi)
print(math.e)

This uses the math module to display high-accuracy versions of the mathematical constants pi and e.

Top tip: Do not name your programs with the same name as an existing module. E.g. math.py. This creates a bug that can be surprisingly hard to track down until you’ve made the mistake once, after which you probably won’t do so again!

Some awesome Python packages

Beyond the built-in modules in Python, there are some amazing packages which add all kinds of functionality, according to your needs. For example:

  • matplotlib for powerful graphing capabilities
  • pygame for fun and easy game development
  • numpy – for powerful mathematical processing beyond what math offers
  • pandas – an industry standard for data processing and analysis
  • nltk for natural language processing

This is just a taster. For a more comprehensive list, check out the Python Package Index, here.

Using pip to install Python packages

Let’s say you want to install some awesome package like pandas.

These days there is rarely a reason not to use the standard package manager for Python: pip, to install your project’s dependencies. It is extremely easy to use, assuming you know how to work with a command line interface on your machine. On Windows, this is usually cmd or powershell. On Mac/Linux it may well be terminal/bash. Once you have this open (see Running python scripts from a command line for more info), you can type:

  • pip install pandas on Windows

and

  • pip3 install pandas on Mac/Linux

Ta da – this is all you should need to do to be able to import new package into you Python program and make use of the goodness they provide.

Please note: Python virtual environments are beyond the scope of this article, but you may want to consider their use. If using a virtual environment, you would want to use pip from within that environment so that your packages are not installed to your main Python installation.

Check out these beginner Python books on Amazon

As an Amazon Associate I earn from qualifying purchases.

Using PyCharm to Install Packages in Python

If you use PyCharm or another Python IDE that supports package installation, you may prefer to install via the IDE’s GUI. This applies to both virtual and non-virtual environments.

In PyCharm, the process is very simple. Just go to settings->python interpreter and select the packages you want to install. You could also still use pip by opening a terminal from within the IDE. Which method you should choose is merely a matter of preference.

Installing Python packages with PyCharm


So that’s it really. It’s very simple to install Python packages, assuming you know how to use a command line on your system or have an IDE that allows you to do it via a GUI. If you don’t yet know how to do this, it is a very useful skill to learn which is not that difficult, so maybe this is a good opportunity to extend your computing knowledge just a little. The rewards of working with all the amazing Python packages available are well worth it!

Happy computing!

Sharing is caring!

Leave a Reply

Your email address will not be published. Required fields are marked *