Create a module.

Put importer.py in the directory that holds all your other Python scripts. For the time being, put date.py there too.

date.py
importer.py

Import the module date.py into a Python script.

Run the above file importer.py, which imports the module date. The output comes from lines 15–36 of importer.py. No output comes from lines 98–104 of date.py because the variable __name__ (two underscores in front, two in back) in line 97 of date.py contains the string "date".

The current directory is /Users/myname/python
The import statement searches the following directories.
1 /Users/myname/python
2 /Users/myname/Documents
3 /Library/Frameworks/Python.framework/Versions/3.8/lib/python38.zip
4 /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8
5 /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload
6 /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages

The following module has been loaded:
<module 'date' from '/Users/myname/python/date.py'>
date.__file__ = /Users/myname/python/date.py

d = 12/31/2019
type(d) = <class 'date.Date'>

Run the module date.py by itself.

Run the file date.py by itself, without importer.py. You can run date.py from IDLE, or in two different ways from the Terminal command line. With the -m option, you can omit the .py filename extension, just like in an import statement. (In the Microsoft Windows command prompt, say py.exe instead of python3.)

python3 date.py
python3 -m date

pydoc3 --help
pydoc3 topics
pydoc3 /Users/myname/python/date.py
pydoc3 ~/python/date.py

pydoc3 -b
Server ready at http://localhost:50240/
Server commands: [b]rowser, [q]uit
server>

The output now comes from lines 98–104 of date.py, because the variable __name__ in line 97 of date.py now contains the string "__main__", making the if in line 97 true.

Today is 11/07/2019.

We saw another example of a module with a small demonstration section in tkinter Flag.

Import the module date.py interactively.

In the Python 3.8.0 Shell window of IDLE, say

>>> import date

>>> help(date)
Help on module date:

NAME
    date - date.py

DESCRIPTION
    This module is named date.py.  You can import it by saying
    import date
    at the top of your Python script.

CLASSES
    builtins.object
        Date

    class Date(builtins.object)
     |  Class Date demonstrates class and instance attributes, class and instance methods.
     |  It is a simple date class, containing year, month, and day integers.
     |
     |  Methods defined here:
     |
     |  __init__(self, month, day, year)
     |      Initialize self.  See help(type(self)) for accurate signature.
     |
     |  __str__(self)
     |      Return a string that looks like the contents of myself.
     |
     |  dayOfYear(self)
     |      Return my day of the year: a number in the range 1 to 365 inclusive.
     |
     |  getDay(self)
     |      Return the number of my day (1 to the length of my month, inclusive).
     |
     |  getMonth(self)
     |      Return the number of my month (1 to 12 inclusive).
     |
     |  getYear(self)
     |      Return my year.
     |
     |  monthsInYear()
     |      Return the number of months in a year.  This function is selfless.
     |
     |  nextDay(self)
     |      Move myself one day into the future.
     |
     |  nextDays(self, n)
     |      Move myself n days into the future.
     |
     |  ----------------------------------------------------------------------
     |  Data descriptors defined here:
     |
     |  __dict__
     |      dictionary for instance variables (if defined)
     |
     |  __weakref__
     |      list of weak references to the object (if defined)
     |
     |  ----------------------------------------------------------------------
     |  Data and other attributes defined here:
     |
     |  lengths = [None, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

FILE
    /Users/myname/python/date.py

>>>

You can do the same thing in a Terminal command line. (In the Microsoft Windows command prompt, say py.exe instead of python3.) In help, press return or q.

python3
Python 3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> import date

>>> help(date)
Help on module date:
etc.

>>> help(date.Date)
Help on class Date in module date:
etc.

>>> control-d

Thing to try

  1. importer.py uses only one thing from the module date, namely the class Date. (Actually, there are no other things in this very simple module.) To import only that one thing, and to get on a first-name basis with that one thing, change line 10 of importer.py from
        import date             #Import everything in the module date.py.
    
    to
        from date import Date   #Import only the class Date from the module date.py.
    
    and change line 27 from
        d = date.Date(12, 31, 2019)  #We used to have to refer to Date by its full name.
    
    to
        d = Date(12, 31, 2019)       #We are now on a first-name basis with class Date.
    
    The last line of output remains
    type(d) = <class 'date.Date'>
    
  2. Is the name of the class Date too long to type? If so, change line 10 of importer.py to
        from date import Date as D
    
    and change line 27 to
        d = D(12, 31, 2019)
    
    The last line of output remains
    type(d) = <class 'date.Date'>
    
  3. How can you add a directory to the ones listed in sys.path? A Python script run from the command line (macOS Terminal, Linux shell, Microsoft Windows command prompt), but not from IDLE, will have the contents of the environment variable PYTHONPATH prefixed to its sys.path. A Python script run from IDLE does receive a PYTHONPATH environment variable, but it is not added to the script’s sys.path.
    import os
    
    if "PYTHONPATH" in os.environ:                  #os.environ is a dictionary.
        pythonpath = os.environ["PYTHONPATH"]
        directories = pythonpath.split(os.pathsep)  #os.pathsep is a one-character string.
                                                    #directories is a list of strings.
    
        for i, directory in enumerate(directories, start = 1):
            print(i, directory)
    
    Run from the macOS Terminal, or from my Fedora Linux, PYTHONPATH was empty. Run from macOS IDLE, PYTHONPATH was
    1 /Applications/Python 3.8/IDLE.app/Contents/Resources