So, you want Python to do something very functional but do not feel like writing heaps of code to achieve that functionality? Then look no further, this guide will teach you exactly how to do that by utilising Python Packages. Python Packages are collections of handwritten scripts that will increase the capabilities of the programming language exponentially! Python Packages can be found online with the official distribution repository being the Python Package Index (PyPI). This is an excellent repository of packages for the programming language with over a quarter of a million unique packages available to download. Each and every one will improve the potential of Python, all you need to do is install the right one for you.
It is important to have the same Python version to the particular package. Also having pip, setup tools and wheel projects installed and up-to-date will allow the easiest installation method. Pip is the standard package-management system used to install and manage software packages written in Python. Wheel projects are a pre-built distribution format that provides faster installation. You can also create a Python virtual environment so that packages with different version requirements can all be run effectively. This issue is called version conflicts. For this guide a Windows computer will be used, contents for this tutorial is seen below.
- Good Places to find Python Packages
- Requirements to Install Packages
- Test for Python Version from Command Prompt
- Test for Pip from Command Prompt
- Ensure Pip, Setup tools and Wheel Projects are Up-To-Date
- Installing Packages using Pip and PyPI
- Access and Code with Your New Installed Packages!
- Uninstall Packages
- Guide for Mac or Linux Users
- The Next Step
- Download The Code
A great place to start your journey would be Python Rundown and Main Windows. This will also tell you where to download Python and access online Python emulators. Python is an excellent language to utilise with Raspberry Pi Boards such as Raspberry Pi 4 Model B 8GB. These are computing devices that can easily fit on the palm of your hand. Within this tutorial when there is text/symbols that would be written as code it will be denoted in this tutorial with line symbols, | |. This notation will stay consistent throughout the whole page, please don't type the line symbols into your code as it will cause a syntax error.
As always if you have any questions, queries or things to add please let us know your thoughts!
The Python Package Index (PyPI) is an excellent repository of packages for the Python programming language. It is the official site and is open to all Python developers to consume and distribute their packages. The website is also a great way to discover new packages and easily install them using the package manager installed by default to Python called Pip.
Another good place to find Python Packages is the Anaconda repository. Anaconda is a free and open-source distribution of Python programming language with built-in modules different than default Python. It is slanted more towards data-science, large scale data processing and machine learning applications. Anaconda also uses a different package manager called Conda which will be explored in another tutorial.
Now before we dive into the requirements there is an important fact to remember and realise which will save a lot of time. The Command Prompt and the Python Command-Line Shell look really similar. Command Prompt is one of the command-line interface programs used to execute commands in Windows operating systems. On the other hand, there is the Python Command-Line Shell which can also be referred to as the Python Shell. It allows a user to directly interact with the Python programming language. The guide Python Rundown and Main Windows goes into depth about this interface. Now even though they both look very similar it is important to realise that they are very different and to use the correct one for the particular task.
Now let me be very clear, for all this tutorial the Command Prompt and only the Command Prompt will be used for installing Python Packages. To access the Command Prompt on Windows computers press the Windows key on your keyboard then type Command Prompt to the search bar. Clicking on the application icon will open this interface. The picture below demonstrates this occurring within Windows 10.
Before installing packages make sure you have Python on your computer and that the expected/most recent version is available in your Command Prompt. You can check this by typing directly into the Command Prompt the below.
By pressing the enter key you will execute this line and should receive an output like | Python 3.8.3 |. In the image below you can see this has happened. Now if your running into problems make sure you are running this from the Command Prompt and not the Python Command-Line Shell. If you run this line in the Python Command-Line Shell you will get a Traceback Error. If you do not have Python installed this line will also return an error. Downloading this programming language can be done from Python Official Website.
If you installed Python from the official site then pip will already be installed. Pip is a tool that installs packages directly into Python utilising the Command Prompt and a single typed line. It is good practice is to make sure pip is available on your system. You can check this by typing directly into the Command Prompt the below.
Then by pressing the enter key. If pip is running and installed then it should return an output like | pip 20.2.1 from ... |. As you can see in the below image this is what has happened. If pip isn’t already installed then this will cause an error to appear. To install it first try to load it from the standard library by typing the following into the Command Prompt and then pressing the enter key.
python -m ensurepip --default-pip
If errors are still coming up double check you are executing from the Command Prompt and not the Python Command-Line Shell. If you were to run the above line in the Python Command-Line Shell you will get a Traceback Error. And if that still does not let you run pip go to the Python Pip Bootstrap Page. Copy and paste everything on that page into the Command Prompt. Then press enter to run the many lines of code. It will take some time using this method (~10 minutes) but pip will be fully installed. This method will also install the setup tools and wheels if not already installed. Interesting facts, pip is a recursive acronym that stands for 'Pip Installs Packages'. A recursive acronym is an acronym that refers to itself coined by Douglas Hofstadter and only came about in the 1979 . Recursive acronyms have a long history of use in early computer science.
Pip is good enough by itself to install the majority of Python Packages however up-to-date copies of the setuptools and wheel projects are useful to ensure you can also install from older source archives. To update both of these type into the Command Prompt the following below.
python -m pip install --upgrade pip setuptools wheel
Once you press enter the Command Prompt will start download the newest versions of the setup tools and wheel projects. Pip will also be updated using this method. Below is an image of the Command Prompt where you can see all this occurring.
Now all the setting up parts are over! Let us crack on in and download some exciting Python Packages. The most common usage of pip is to install from the Python Package Index using a requirement specifier. Generally speaking, a requirement specifier is composed of a project name followed by an optional version specifier. For examples to install the latest version of a package called “Package” you would type directly into the Command Prompt the below.
pip install Package
Then as soon as you press enter on your keyboard the package would be accessible to utilise in code via the | import | keyword. More on this keyword can be found in the tutorial Import, From and As Keywords in Python.
Below is an image of Pybrot attempting to be installed using the line | pip install pybrot | typed and entered directly into the Command Prompt. Pybrot is a Python Package that you can use to create images of the Mandelbrot set. The Mandelbrot set is an interesting way of seeing how complex functions behave under repeated iterations. As this package was already in the system and up to date it does not re-install itself and instead states that | Requirement already satisfied |. The location of Pybrot comes from the Python Package Index found in this location.
Below is an image of the NumPy package is being installed for the first time into Python using the line | pip install numpy | typed and entered directly into the Command Prompt. This package brings great mathematical prowess to the programming language enabling it to do calculations such as Fourier transforms. A Fourier transform decomposes a function into its constituent frequencies, such as the expression of a musical chord in terms of the volumes and frequencies of its constituent notes. The Numpy package comes from the Python Package Index found in this location. A really easy install and it took less than a minute.
Below is another Python Package called matplotlib installed using the Command Prompt and the text | pip install matplotlib |. Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. It comes from the Python Package Index found in this location. Another easy install taking less than a minute. This is the advantages of installing using the pip method as it makes installing the packages a breeze.
Now if you are wondering where these packages end up after they are downloaded, they end up in the site-packages folder deep inside the Python folder. The images below show exactly where the above Pybrot installation has placed the files.
Below is an image exhibiting a quick script to demonstrate the new functionality that the above packages have brought to Python. The Python Packages downloaded before were PyBrot, NumPy and Matplotlib. These three packages in combination with Python means you can produce intricate Mandelbrot images from just three lines of code. So easy! In the image below the top left shows the Python Programming Window importing the Mandelbrot functionality. For this | import | keyword to be successful both NumPy and Matplotlib need to be installed as both of these packages are imported internally by the scripts within Pybrot. In the image below the bottom left shows the Python IDLE Shell acting as the calling environment. To learn more about calling environments check out the guide Making Your Own Python Functions. In the image below the right side represent the output when the | m.show() | function is typed into the calling environment. The output is an image of the Mandelbrot set! The Mandelbrot set is an interesting way of seeing how complex functions behave under repeated iterations and without external packages would take a lot of lines of code to create.
You now have all that new functionality ready and raring to go! No need to type out heaps of lines of code. Just check out below a small section of just one of the Python files that is inside the Pybrot package. You do not need to ever re-write this! The Python Package Index has an excellent browse projects section making it an amazing place solely to search through. There is so much variety, depth and choice available made from real people happily sharing their script creations and code masterpieces.
To uninstall a no longer desired Python Package simply type the below directly into the command prompt.
pip uninstall package name
Then press enter. This will then give an overview of exactly what files it will remove and then ask a Boolean yes or no question as to whether you want those files to be uninstalled. This Boolean question will look like | proceed (y/n)? |. If you then type | y | and press enter pip will uninstall the package for you. The image below demonstrates a Python Package called Pillow being uninstalled. Pillow is a free and open-source additional library which adds support to Python for opening, manipulating, and saving many different image file formats. As you can see below it was successfully uninstalled using this process.
If your using Linux take a look at this guide when installing pip, setuptools and wheel projects. If your installing pip on a macOS take a look at this guide. These guides should clear up any questions or issues around installing packages to Python when using these operating systems.
Now let us get some practical codes on the table and show off some of the coding muscles we have been building throughout this Python journey! No better way to start flexing that the guide Swell Scripts with the Turtle Library. Join us next time on our next step in this Python Odyssey!
Below you can find the code for the above example. You will be able to run the code on your own computing device and modify the code at your leisure. Open up the script with Python IDLE Shell and run it using the Python Programming Window.