Thursday, June 20, 2019

PIP - Python's Package Management System

Python has an easy to use and very robust package management system called pip. In order to take advantage of another programmer's work you have to import their source code into your program. The Python package management system offers a true advantage over compiled language such as C++ and even other interpreted languages like Ruby.

Pip is included with Python 3 but if you don't have it installed the instructions can be found here:
Pip Installation

Its dead simple to include an external package using pip:

$ pip install mortgage

In some operating systems you may need to install and use pip3.

$ pip3 install mortgage

For instance pip3 installation on a Debian system:

$ sudo apt-get update
$ sudo apt-get install python3-pip
$ pip3 --version

https://linuxize.com/post/how-to-install-pip-on-debian-9/After you have installed the package you can include it in your own python code using the import statement. After the package is installed you use it as follows:


from mortgage import Loan
loan = Loan(principal=30000, interest=.925, term=6)
loan.summarize

If you run the above program in python you will get a very nice summary of a loan with the given parameters.

Because of the overwhelming popularity of python for both commercial and open source projects, there are a ton of projects written in python that you can take advantage of. This is one of the biggest selling points for me when deciding on a good programming language for a project. If there is a package available and I don't have to write one or extend and existing package then I can save a-lot of time and effort.

The Dirty Way To Include External Python Code

Many compiled languages and even interpreted languages don't have a package management system or they are so difficult to use that it takes as much time to include someone else's source code as it would to re-write it yourself.

C and C++ programs and even Java and C# programs for that matter are littered with copy and paste code. If a programmer can't figure out how to reference a library or include code the "correct" way they will use whatever means necessary to get the program working.

Copy and paste code is a huge burden for both commercial and open source projects. Source code always carries with it a maintenance cost.

How to Find Packages

You can find packages easily online by navigating to The Python Package Index. When you find the package you are looking for you can use the pip install <<package> just like we did the the Mortgage example above.

Another good way to find a package is to search for open source projects on SourceForge or GitHub. Most python projects have instructions on how to install the package using pip. Many commercial vendors such as Microsoft and Amazon have open source packages to support their applications.

If you prefer the command line then you can use pip search <<package>>  . It will also tell you if the package is already installed, what version is installed and if there is a later version available.

You can see from the below command that we have several other packages to choose from. Packages that have a version below 1.0.0, py-mortgage 0.1.0 for example, are generally less mature. Sometimes they can be useful but if you are doing serious work then I would stick with 1.0 and above.


$ pip search Mortgage
mortgage (1.0.3)             - Mortgage Calculator
  INSTALLED: 1.0.2
  LATEST:    1.0.3
py-mortgage (0.1.0)          - A package to handle mortgage calculations
pymortgage (0.1.4)           - Python Mortgage utilities
py-mortgagekit (1.0.3b1)     - Python library for mortgage calculations.
Mortgages-and-Loans (0.1.2)  - Simple Python Scripts for Complex Mortgages
pandicake (0.3.3)            - Utilities for mortgage backed securities cash flow analytics

Pip has a-lot of functionality and can get complicated if you get into advanced features. Most of the time you just need a simple pip install to get your python program to work. However, if you are working on a larger project or in a strict programming shop then you may need to dig deeper into how pip works.

The official user guide for pip can be found here:
PIP - User Guide

No comments:

Post a Comment

nonlocal keyword

The nonlocal keyword is used to allow an inner function to access variables defined in an outer function. Without using the nonlocal keywo...