module, you just need to save all of the code for defining
the class and the attributes and functions as a separate
file with the .py extension. You can import your own
modules by using the same methods shown previously
with standard library modules. By default, Python looks
for a module in the same directory as the Python
program you are importing into. If it doesn’t find the file
there, it looks through your operating system’s path
statements. To print out the paths your OS will search
through, consider this example of importing the sys
module and using the sys.path method:
Click here to view code image
>>> import sys
>>> sys.path
['', '/Users/chrijack/Documents',
'/Library/Frameworks/Python.
framework/Versions/3.8/lib/python38.zip',
'/Library/Frameworks/
Python.framework/Versions/3.8/lib/python3.8',
'/Library/Frameworks/
Python.framework/Versions/3.8/lib/python3.8/lib-
dynload', '/
Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/
site-packages']
Depending on your OS (this output is from a Mac), the
previous code might look different from what you see
here, but it should still show you what Python sees, so it
is useful if you are having trouble importing a module.
If you remove the class from the code shown in Example
4-2 and store it in a separate file named device.py, you
can import the classes from your new module and end up
with the following program, which is a lot more readable
while still operating exactly the same:
Click here to view code image
from device import Router, Switch
rtr1 = Router('iosV', '15.6.7', '10.10.10.1')