Lab 9 (Part - I): Building a Python Package


This lab has two parts: part I is here.

  1. Creating a library (package) from the code written in the previous labs
  2. Using the library (package) to solve a set of problems covering the entire space of numerical methods learnt thus far. Use this Link for the Problem Set


Building a Python package

Creating a Python package is a three-step process.

  1. Decide a name for your package

    I am calling it cbnm2023; you can give it a name of your choice. In the explanation later, I will keep referring to the package as cbnm2023.

  2. Create an appropriate directory structure

    Create a new directory with your chosen package name. You can create a directory in Linux as mkdir <package name>

  3. Create subdirectories for each related set of functions

    Move into the newly created directory. Create subdirectories for each major class of functions we have coded so far. In Linux, we can create all the subdirectories at once as

    • mkdir utils roots syseq linreg interp integrate
    • Copy your Python source files (.py files) into appropriate directories. For example, you can copy the files: gauss_eliminate.py, LUD.py, jacobi.py into the syseq directory.
    • Copy all the Python files (.py) files into their correct subdirectories.
  4. Create __init__.py files

    In each directory, starting from the package directory (in my example, cbnm2023, create an empty file called __init__.py in each newly created directory. In Linux, you can do this easily by going to each directory and typing the command cp   /dev/null   __init__.py

  5. Now, you are almost done but for trying your package!

    Move into the directory above cbnm2023 (your package name directory) and start Python either in the terminal, IDE or in Jupyter Notebook.

    Try the code in the cells below: if it works, you are done! Of course, don't forget to replace the names with your directory and file names. The output may also be different - it will match your code output, not mine!

In [1]:
import numpy as np
# cbnm2023.utils refers to the directory name containg all the
# IEEE floating point operations.
# cbnm2023.roots is the name of the directory containing all the
# root-finding programs. My programs are in the file:
# root_finder.py
# We import it as in Line 10 below. See how we use the names
# of the Python files without the .py extension
from cbnm2023.utils import IEEEUtils as iu
from cbnm2023.roots import root_finder as rf
from cbnm2023.syseq import eqn_gauss_eliminate as eg
from cbnm2023.syseq import LUD

np.set_printoptions(precision=2, suppress=True)
In [2]:
def f1(x) :
    return x ** 5 - 100.

ans = rf.root_bisection(f1, -10., 10., eps=1e-8)
print(ans[0], '(Verification: ', ans[0]**5, ')')
Found Solution after  38  iterations.
2.5118864315300016 (Verification:  100.00000000406497 )

Congratulations on your first package!


Making your package accessible system-wide

Now that you built and verified that your package is working, let us do the one more step: make the package available for use from any directory (folder) on the system.

Before doing anything, make the following changes to the python code written if you have used your own functions in any other functions. I am sure that you did! Very likely, you used your gauss_eliminate() function for interpolation! Change the way you used the gauss_eliminate() by doing the following.

  1. Keep only one copy of gauss_eliminate (as well as every other!) function. It should be in the directory syseq. If there are other copies in other files, remove them. Similarly, ensure that there are no duplicate copies of any function.
  2. In each file where you are using your own functions, import your own library functions as shown in the cell below.
Do this in every file where you used your own functions. Only then, go to the steps below.

In [3]:
##### I have my package code in the top directory cbnm2023. The
##### gauss_eliminate code is in a sub-directory syseq containing
##### the Python file eqn_gauss_eliminate.py. Modify the import
##### statement according to your naming of files and directories.

import numpy as np
import cbnm2023.syseq.eqn_gauss_eliminate as eg

There are three more steps to make your package public. First, move into the directory (folder) above the top directory (folder) of the package, the one you created in Step 2 above. For example, if my package is in the folder cbnm2023 and this is itself in the directory CBNMCLASS. Therefore, I now move into the directory CBNMCLASS and do the following in that directory.

  1. Create the file setup.py (shown in the cell below). Of course, replace the name and other details to match your case.
  2. Run the command
    python setup.py build
    If you are using a Windows system, open a command terminal, change into the correct folder and then type the above command.

    After running the above command, make sure there is a directory called build in your current directory.

  3. Run the command
    python setup.py install
    If everything goes well, it would have installed the package now for system-wide use.
You can test it by going to any directory and running the code in Cells 1 and 2 above.

In [ ]:
from setuptools import setup, find_packages

VERSION = '1.0' 
DESCRIPTION = 'Computer Based Numerical Methods(CBNM 2023) '
LONG_DESCRIPTION = 'Commonly used numerical methods for scientific applications'

# Setting up
setup(
    # the name must match the folder name 'verysimplemodule'
    name='cbnm2023', 
    version=1.0,
    author='Chakravarthy Bhagvati',
    author_email="chakravarthybhagvati@uohyd.ac.in",
    description='Computer Based Numerical Methods (CBNM 2023)',
    long_description='CBNM 2023 provides numerical methods for scientific applications',
    packages=find_packages(),
    install_requires=[], # add any additional packages that 
    # needs to be installed along with your package.
    
    keywords=['python', 'numerical methods'],
    classifiers= [
        "Development Status :: 1 - Alpha",
        "Intended Audience :: UG Students",
        "Programming Language :: Python :: 3",
        "Operating System :: Ubuntu :: Linux",
    ]
)