Import python basic package and _ init _. py introduction, python _ init _. py

Source: Internet
Author: User

Import python basic package and _ init _. py introduction, python _ init _. py

Call the same level directory:

– src |– mod.py |– test.py

If you import the module mod in the test. py program, you can directly use

Import mod or from mod import *

Call the modules in the subdirectory:

-Src |-mod. py |-lib |-mod2.py |-test. pyfrom lib. mod2 import * or import lib. mod2

Call the files in the upper-level directory

The directory structure is as follows:

– src |– mod.py |– lib | |– mod2.py |– sub | |– test2.pyimport syssys.path.append('C:\\test\\A\\C')import modimport lib.mod2

_ Init _. py

In each package of the python module, There Is A _ init __. the py file (which defines the attributes and methods of the package) is followed by some module files and subdirectories. If the subdirectory also contains _ init __. py, It is the sub-package of this package. When you import a package as a module (for example, importing dom from xml), you actually import its _ init _. py file.

A package is a directory with a special file _ init _. py. The _ init _. py file defines the attributes and methods of the package. In fact, it can not be defined; it can be just an empty file, but it must exist. If _ init _. py does not exist, this directory is just a directory, rather than a package, and cannot be imported or contain other modules and nested packages.

Another important variable in _ init _. py is called _ all __.

If the directory is as follows:

– src |– mod.py |– lib | |– mod2.py | |– mod3.py | |– sub | |– | |– mod3.py

We sometimes make a trick to "import all", that is:

from lib import *

In this case, import the sub-modules and sub-packages registered in the _ init _. py file _ all _ list to the current scope. For example:

# File _ init _. py _ all _ = ["mod2", "mod3", "sub"]

Summary:

To download py files from other paths, add them to sys. path before importing them.

_ Init _. py:

  • The ID of the package. The package cannot be deleted.
  • Define _ all __in the package for Fuzzy import.
  • Compile Python code (it is not recommended to write the python module in _ init _. You can create another module in the package to write it. Make sure that _ init _. py is simple)

Summary

The above section describes how to import the basic python package and _ init __. if you have any questions, please leave a message and the editor will reply to you in time. Thank you very much for your support for the help House website!

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.