Learning Management Platform for Written Tutorial Series.

Python 3 PyPI - Take advantage of the community packages

Python 3 PyPI - Take advantage of the community packages

Python Package Index (PyPI)

One of the things that make python great is that it has a great community of developers who have developed packages that perform specific tasks and most importantly are willing to share them.

This is were Python Package Index a.k.a PyPI comes in. It is a repository of python packages. A package is simply a library that performs a specific task, for example. You might have a large dataset that you would like to analyze and print a trends graph. You can search Python Package Index for packages that do that for you and install them as opposed to you write the code from scratch

Topics to be covered

We will cover the following topics in this lesson

  • What are third-party packages
  • Pip Installs Packages (PIP)
  • Installing packages using pip
  • Python english spellchecker application

What are third-party packages

Python is a great language that comes with a lot of function out of the box. That is good but the functionality is kind of general purpose.

Third-party packages are libraries which add more functionality to Python without requiring the programmer to write a lot code. For example, let's say you want to create a web application using python. You can use Django as a third-party package and build your application on it. You can also download utility packages such as spell checkers to analyze text and printing out incorrect words or do more advanced staff such as artificial intelligence using packages like TensorFlow.

Third-party packages for python can be found on Python Package Index

Pip Installs Packages (PIP)

Pip Installs Packages also known as pip is the standard python package management system for python that can be used to install packages that are hosted on PyPI.

Pip is run from the command line using lower case letters like so

pip install package-name

If you need need to install a particular version of a package then you can use the following syntax

pip{x} install package-name

Where {x} is the version number.

For example, if you want to install a package for python 3, then you can do it like so

pip3 install package-name

Installing packages using pip

We will now install a package using pip. For this section, you will need an active internet connection as the packages are downloaded over the internet.

Run the following command to install a spell checker package.

pip3 install pyspellchecker

HERE,

  • The above code tells pip to install the package pyspellchecker

You should be able to see results like the following in the terminal

Collecting pyspellchecker
  Downloading https://files.pythonhosted.org/packages/49/77/78b5a7ccb9079dff2d32b88762b1d53f29fad1f09b61ee883d3bae841020/pyspellchecker-0.5.0-py3-none-any.whl (1.9MB)
    100% |████████████████████████████████| 1.9MB 704kB/s 
Installing collected packages: pyspellchecker
Successfully installed pyspellchecker-0.5.0
You are using pip version 19.0.3, however version 19.2.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

Python English spellchecker application

In this section, we will look define a list that contains words with some that will be misspelled on purpose then we will use the package that we downloaded using pip to find alternative spellings and the most likely spelling.

from spellchecker import SpellChecker #imports our third-party package that we installed using pip

spell = SpellChecker()

word_list = ['Jakc','was','ak','work','on','satuday']

misspelled = spell.unknown(word_list)

for word in misspelled:
    correct_word = spell.correction(word)
    print(f'The word "{word}" has been mispelled. The correct spelling is {correct_word}')
    print(f'Other likely words are {spell.candidates(word)}')

HERE,

  • from spellchecker import SpellChecker imports the third-party package spellchecker that we installed using pip
  • spell = SpellChecker() creates an instance of the SpellChecker class
  • word_list = ['Jakc','was','at','work','on','satuday'] defines a list of words with Jakc and satuday misspelled on purpose.
  • misspelled = spell.unknown(word_list) extracts the misspelled words from the list
  • for word in misspelled: loops through the list of misspelled words and using the correction method passing in the misspelled word as the argument to get the most likely correct spelling. We are also printing out most likely correct words.

Summary

PyPI is a central repository for python packages that we can install using pip and use them in our applications. Third-party packages allow us to take advantage of open libraries to perform tasks that would otherwise taken us a lot of time to write ourselves.

What next?

If you enjoyed this lesson then show us your appreciation by creating a free accounts on our site. As always we appreciate your comments down below.


...