Installing Python modules

We have several techniques to install a Python module or package:

  • We can write setup.py and use the distribution utilities module, distutils, to install the package into Python's lib/site-packages directory. This is described in detail in the Python Packaging Authority documentation. See https://www.pypa.io/en/latest/ for information. Building software for other people to install can be complex, and will often require sophisticated test cases using the tox tool to build environments, run tests, and create distribution kits. See https://tox.readthedocs.io/en/latest/ for more information.
  • We can set the PYTHONPATH environment variable to include our packages and modules. We can set this temporarily in a shell, or we can set it more permanently by editing our ~/.bash_profile or the system's /etc/profile. We'll take a look at this later in this section.
  • The current working directory is a package as well. It's always first on the sys.path list. When working on a simple one-module Python application, this is very handy. 

Setting the environment variable can be done transiently or persistently. We can set it in an interactive session with a command, as follows:

export PYTHONPATH=~/my_app-v1.2/src

This sets PYTHONPATH to include the named directory when searching for a module. The module is effectively installed through this simple change to the environment. Nothing is written to Python's lib/site-packages.

This is a transient setting that may be lost when we end the terminal session. The alternative is to update our ~/.bash_profile to include a more permanent change to the environment. We simply append that export line to .bash_profile so that the package is used every time we log in.

For web server applications, the Apache, NGINX, or uWSGI configuration may need to be updated to include access to the necessary Python modules. There are two approaches to creating web servers:

  • Install Everything. Use Python package installation to create the entire web server. This will involve using a local package index for customized application components. More work is done in the integration phase of continuous integration/continuous deployment (CI/CD).
  • Install Open Source Only. Use Python package installation for open source components. Use Git checkout and PYTHONPATH for the customized application components. More work is done in the deployment phase of CI/CD.

Either approach will work nicely. While the install everything approach has the superficial advantage of simplicity, it leads to creating installable versions of all of our customized application software. For software that is proprietary, and will not be released as open source, this seems like needless integration work to create an installable version.

Installing only open source components leads to a more complex application deployment. There will be a conda (or pip) installation of open source components, in addition to git checkout of proprietary components. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset