Resources
52084144
Http://www.runoob.com/python/python-modules.html
Python Basic Concept Python module
The Python module, which is a python file that ends with a. Py, contains Python object definitions and Python statements.
Modules allow you to logically organize your Python code snippets.
Assigning the relevant code to a module will make your code better and easier to understand.
Modules can define functions, classes, and variables, and modules can also contain executable code.
Python Package
A package is a hierarchical file directory structure that defines a Python application environment consisting of a module and a sub-package, and a sub-package under a sub-package.
In a nutshell, a package is a folder, but a __init__.py file must exist under the folder, and the contents of the file can be empty. __init__.py is used to identify the current folder as a package.
Scene app import sibling directory file
If you need to introduce a file in a sibling directory, you can invoke it in the form of an import module.
Considering the two Python files in the same directory, test.py needs to call the functions in support.py, the directory structure is as follows:
|--test.py|--support.py
The code in support.py is as follows:
def print_func (PAR): Print " ", par return
The code for the test.py call is as follows:
# !/usr/bin/python # -*-coding:utf-8-*- # Import Module Import # The functions contained in the module can now be called Support.print_func ("runoob")
To import a sub-catalog file
If you need to introduce a file under a subdirectory, you can call it in the form of an import package that encapsulates a subdirectory into a package.
Consider a runoob1.py, runoob2.py, __init__.py file under the package_runoob directory, test.py the code for the Test call package, with the following directory structure:
test.pypackage_runoob|--__init__.py|--runoob1.py|--runoob2.py
__init__.py can be an empty file.
The test.py calling code is as follows:
# !/usr/bin/python # -*-coding:utf-8-*- # Import Phone Package from Import Runoob1 from Import runoob2 runoob1 () runoob2 ()
You can also use:
# !/usr/bin/python # -*-coding:utf-8-*- # Import Phone Package Import Package_runoob.runoob1 Import package_runoob.runoob2 package_runoob.runoob1.runoob1 () package_runoob.runoob2.runoob2 ()
Python imports custom modules and packages