Two ways to implement the package mechanism in Python

Source: Internet
Author: User

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

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.