Two ways to implement the package mechanism in Python

Source: Internet
Author: User

Article Source: http://www.cnblogs.com/phinecos/archive/2010/05/07/1730027.html

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.

1,__init__.py is a blank file in a way that

demo.py content is as follows:

From Mypackage.classone import Classone
From Mypackage.classtwo import Classtwo

if __name__ = = "__main__":
C1 = Classone ()
C1.printinfo ()
C2 = Classtwo ()
C2.printinfo ()

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!")

2, if the import module is written in __init__.py, then the above example can be done.

The contents of __init__.py are as follows:

From Classone import Classone
From Classtwo import Classtwo

demo.py content is as follows:

Import MyPackage

if __name__ = = "__main__":
C1 = Mypackage.classone ()
C1.printinfo ()
C2 = Mypackage.classtwo ()
C2.printinfo ()

Or demo.py can also be defined as follows:

From mypackage Import *

if __name__ = = "__main__":
C1 = Classone ()
C1.printinfo ()
C2 = Classtwo ()
C2.printinfo ()

Two ways to implement the package mechanism in Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.