When you execute the Import module, the interpreter searches for the module1.py file based on the following search path.
1) Current working directory
2) Directories in the Pythonpath
3) Python installation directory (/usr/local/lib/python)
In fact, the module search is searched in the list of directories saved in the global variable Sys.path.
Sys.path is initialized to include when the interpreter starts executing:
1) Current working directory
2) Directories in the Pythonpath
3) Python installation directory (/usr/local/lib/python)
The package is a collection of modules, and there should be a __init__.py file under the root directory of each. When the interpreter finds this file in the directory, he thinks it's a package, not an ordinary directory.
Let's illustrate this by following an example
Assume that the project structure is as follows:
demo.py
MyPackage
---classone.py
---classtwo.py
---__init__.py
Now we implement the package mechanism in two ways, the main difference is whether to write the module import statement in __init__.py.
---The above content is reproduced in "Cave Court scattered People"
1,__init__.py is a blank file in a way that
demo.py content is as follows:
classone.py content is as follows:
Class Classone:
def __init__ (self):
Self.name = "Class One"
def printinfo (self):
Print ("I am class one!")
classtwo.py content is as follows:
Class Classtwo:
def __init__ (self):
Self.name = "Class"
def printinfo (self):
Print ("I am class two!")
Execution Result:
The 2.__init__.py file is written to the import module, the above example can be done,
__init__.py File Contents:
From Classone import Classone
From Classtwo import Classtwo
demo.py file:
You can also do this:
Before just imitate others to write the example, the common module to add __init__.py files can be imported, and did not notice the details, this document looked up the Python package article did the exercise, understand a little O (∩_∩) o~
Two ways to implement the package mechanism in Python