Packages
To help organize modules and provide a naming hierarchy, Python has the concept of packages.
Packages are just collections of modules, and we normally keep them in a separate directory. We add an empty file with the filename __init.py__ to signal to Python that this is a package. Under the folder Exercises_06, I create a folder called mylib. Under this folder, I create an empty file called __init.py__ marking this as a package.
I add the following line to __init.py__
copyright = "© JOR 2024"
This is now a global variable accessible throughout. There are much more useful things to put into __init.py__ but I’ll leave you to figure that out as your coding skills develop.
Under the folder Exercises_06, I create and test a file called project1.py as shown.
import mylib
print(mylib.copyright)
Under the folder Exercises_06\mylib, I create a file called square.py with the following content. I run it to test.
square_text = "Yo, time to square stuff!"
def square(x):
return x*x
# Uncomment to test
print(square(2))
Under the folder Exercises_06\mylib, I create a file called cube.py with the following content. I run it to test.
cube_text = "Yo, time to cube stuff!"
def cube(x):
return x*x*x
# Uncomment to test
print(cube(2))
Back up to the folder Exercises_06, I create a file called project2.py with the following content.
Comment out the test lines in square.py and cube.py and run project2.py it to test.
import mylib.cube as mycube
import mylib.square as mysquare
print(mycube.cube_text, mycube.cube(3))
print(mysquare.square_text, mysquare.square(3))
There are quite a few ideas to take away from this. I can build packages of reusable code, making all my work modular. To reuse this code in a different project, all I need to do is to copy the entire directory. Obviously, I need heavy commenting and comprehensible documentation to make this really work.
In the examples, I have used functions, but I've also created and used variables from within my packages. I can use the as keyword to reference an imported function and give it any name I like to make it meaningful in my code. Note that I can apply this technique to an entire module also.
For example, the graphing library matplotlib is typically imported as plt, making code more economical and readable. In case you are lost, I have used the tree command to map my directory structure for this exercise so far.

In a large project, I may have several packages which I call, and I need to keep track of all of them.
In a recent project I had network utilities, serial utilities, the decoding of UBlox GPS equipment, the decoding of standard marine instruments, all as separate packages.
Imagine being able to pull years of work into a project in a modular way, without having to worry too much about interdependencies, it all just works!
Last updated