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.
In this section, we will 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...