First, the module imports with import
cal.py:
# !/usr/bin/python def Add (x, y ): return x+yif__name__'__main__': Print Add (+)
Note: __name__ is a built-in variable and is a file name if the value is called __mail__ directly in the CLI.
Import in new.py:
Import Cal print cal.add (2,3);
II. Packages: Modules organized by directory name
1. Create a folder with the name of the package name
2. Create a __init__.py file under this folder
3. Store script files, compiled extensions, and sub-packages in this folder as needed
4, import in the file with import pack.m1,pack.m2,pack.m3
5, using Pack.m1.xxx to invoke the module in the package method
Sample directory Structure:
util/__init____init__. PYc
Call Example new.py:
Import util.cal print util.cal.add (2,3); 5
or using as to use aliases:
Import util.cal as C print c.add (2,3); 5
Or use the From to shorthand:
from Import Add Print Add (2,3);
Python Learning note five: modules and packages