Tuesday, March 8, 2022

Accessing packages from any location

The package we built in the previous subsection is accessible only if the program calling the modules is at the same level as the package location. This requirement is not practical for code reusability and code sharing.

Let us discuss a few techniques to make packages available and usable from any program on any location in our system.

Appending sys.path

This is a useful option for setting sys.path dynamically. Note that sys.path is a list of directories on which a Python interpreter searches every time it executes an import statement in a source program. By using this approach, we are appending (adding) paths of directories or folders containing our packages to sys.path.

For the masifutil package, we will build a new program, pkgmain3.py, which is a copy of pkgmain2.py (to be updated later) but is kept outside the folder where our masifutil package is residing. pkgmain3.py can be in any folder other than the mypackages folder. 

When we execute the pkgmain3.py program, it returns an error:

ModuleNotFoundError: No module named 'masifutil'. This is expected as the path of the masifutil package is not added to sys.path. To add the package folder to sys.path, we will update the main program; let's name it pkgmain4.py, with additional statements for appending sys.path, which is shown next:

# pkgmain4.py with sys.path append code

import sys

sys.path.append('/Users/muasif/Google Drive/PythonForGeeks/

source_code/chapter2/mypackages')

import masifutil

def my_main():

""" This is a main function which generates two random\

numbers and then apply calculator functions on them """

x = masifutil.random_2d()

y = masifutil.random_1d()

sum = masifutil.add(x,y)

diff = masifutil.subtract(x,y)

sroot = masifutil.sqrt(x)

log10x = masifutil.log(x)

log2x = masifutil.ln(x)

print("x = {}, y = {}".format(x, y))

print("sum is {}".format(sum))

print("diff is {}".format(diff))

print("square root is {}".format(sroot))

print("log base of 10 is {}".format(log10x))

print("log base of 2 is {}".format(log2x))

""" This is executed only if the special variable '__name__' is

set as main"""

if __name__ == "__main__":

my_main()

After adding the additional lines of appending sys.path, we executed the main script without any error and with the expected console output. This is because our masifutil package is now available on a path where the Python interpreter can load it when we are importing it in our main script.

Alternative to appending sys.path, we can also use the site.addsitedir function from the site module. The only advantage of using this approach is that this function also looks for .pth files within the included folders, which is helpful for adding additional folders such as sub-packages. A snippet of a sample main script (pktpamin5.py) with the addsitedir function is shown next:

# pkgmain5.py

import site

site.addsitedir('/Users/muasif/Google Drive/PythonForGeeks/

source_code/chapter2/mypackages')

import masifutil

#rest of the code is the same as in pkymain4.py

Note that the directories we append or add using this approach are available only during the program execution. To set sys.path permanently (at the session or system level), the approaches that we will discuss in the next blogs are more helpful.

Share:

0 comments:

Post a Comment