Third Party Modules

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Python has a large library of built-in modules (batteries included).

There are even more third party modules. Check them in the Python Package Index or PyPi. Or just do a Google search for a specific topic.

How to handle third-party dependencies is an ever-evolving topic with Python. This section merely covers the basics to help you wrap your brain around how it works.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/AdvancedTopicsGroup(["`Advanced Topics`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/ObjectOrientedProgrammingGroup -.-> python/constructor("`Constructor`") python/AdvancedTopicsGroup -.-> python/regular_expressions("`Regular Expressions`") python/PythonStandardLibraryGroup -.-> python/os_system("`Operating System and System`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/BasicConceptsGroup -.-> python/python_shell("`Python Shell`") subgraph Lab Skills python/importing_modules -.-> lab-132739{{"`Third Party Modules`"}} python/standard_libraries -.-> lab-132739{{"`Third Party Modules`"}} python/constructor -.-> lab-132739{{"`Third Party Modules`"}} python/regular_expressions -.-> lab-132739{{"`Third Party Modules`"}} python/os_system -.-> lab-132739{{"`Third Party Modules`"}} python/numerical_computing -.-> lab-132739{{"`Third Party Modules`"}} python/python_shell -.-> lab-132739{{"`Third Party Modules`"}} end

sys.path is a directory that contains the list of all directories checked by the import statement. Look at it:

>>> import sys
>>> sys.path
... look at the result ...
>>>

If you import something and it's not located in one of those directories, you will get an ImportError exception.

Standard Library Modules

Modules from Python's standard library usually come from a location such as `/usr/local/lib/python3.6'. You can find out for certain by trying a short test:

>>> import re
>>> re
<module 're' from '/usr/local/lib/python3.6/re.py'>
>>>

Simply looking at a module in the REPL is a good debugging tip to know about. It will show you the location of the file.

Third-party Modules

Third party modules are usually located in a dedicated site-packages directory. You'll see it if you perform the same steps as above:

>>> import numpy
>>> numpy
<module 'numpy' from '/usr/local/lib/python3.6/site-packages/numpy/__init__.py'>
>>>

Again, looking at a module is a good debugging tip if you're trying to figure out why something related to import isn't working as expected.

Installing Modules

The most common technique for installing a third-party module is to use pip. For example:

$ python3 -m pip install packagename

This command will download the package and install it in the site-packages directory.

Problems

  • You may be using an installation of Python that you don't directly control.
    • A corporate approved installation
    • You're using the Python version that comes with the OS.
  • You might not have permission to install global packages in the computer.
  • There might be other dependencies.

Virtual Environments

A common solution to package installation issues is to create a so-called "virtual environment" for yourself. Naturally, there is no "one way" to do this--in fact, there are several competing tools and techniques. However, if you are using a standard Python installation, you can try typing this:

$ sudo apt install python3-venv
$ python -m venv mypython
bash %

After a few moments of waiting, you will have a new directory mypython that's your own little Python install. Within that directory you'll find a bin/ directory (Unix) or a Scripts/ directory (Windows). If you run the activate script found there, it will "activate" this version of Python, making it the default python command for the shell. For example:

$ source mypython/bin/activate
(mypython) bash %

From here, you can now start installing Python packages for yourself. For example:

(mypython) $ python -m pip install pandas
...

For the purposes of experimenting and trying out different packages, a virtual environment will usually work fine. If, on the other hand, you're creating an application and it has specific package dependencies, that is a slightly different problem.

Handling Third-Party Dependencies in Your Application

If you have written an application and it has specific third-party dependencies, one challenge concerns the creation and preservation of the environment that includes your code and the dependencies. Sadly, this has been an area of great confusion and frequent change over Python's lifetime. It continues to evolve even now.

Rather than provide information that's bound to be out of date soon, I refer you to the Python Packaging User Guide.

Exercise 9.4 : Creating a Virtual Environment

See if you can recreate the steps of making a virtual environment and installing pandas into it as shown above.

Summary

Congratulations! You have completed the Third Party Modules lab. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like