First, the module imports with import
cal.py:
#!/usr/bin/pythondef 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 Calprint 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/├──cal.py├──cal.pyc├──__init__.py└──__init__.pyc
Call Example new.py:
Import Util.calprint util.cal.add (2,3);//5
or using as to use aliases:
Import util.cal as Cprint c.add (2,3);//5
Or use the From to shorthand:
From util.cal import addprint Add (2,3);
This article is from the "ganshizhe.bokee.com" blog, make sure to keep this source http://328538.blog.51cto.com/318538/1610614
Import of Python modules and packages