Python file structure and import usage

Source: Internet
Author: User

First, a summary chart:

In Pycharm, the current file directory is not typically automatically added to its own sourse_path. If you encounter other modules that cannot be in the import sibling directory,

Right-click Make_directory as-->sources root to add the current working folder to Source_path.

But the summary chart is very uncoordinated, the following to explain in detail:

(1) different modules in the same package refer to each other

After confirming that this folder path is added to SourcePath: "From Module name import class name"

(2) different packages in the same folder refer to each other

There are three ways to do this:

--You can directly refer to each other's module: "From module name import class name"

--If nothing is written in the __init__.py folder of the respective package, the two dots in front of the package name represent the files in the previous level directory

--Write your own references in the __init__.py of the package

You can then refer to the class name directly with the package name:

(3) Cross-references to different packages in different folders

It is just a matter of writing a directory, of course, if you have already referenced your class in the __init___.py file in Wx_video, it is difficult to import the class directly through the package name

Summarize:

1. When you import a package, you actually import the package's __init__.py file, so if you write another import function in init, it is equivalent to helping to import something else at the time of initialization.

2, each package directory will have a __init___.py file, this file must exist, otherwise, Python will treat this directory as a normal directory, rather than a package. It can be an __init__.py empty file, or it can have Python code, because it __init__.py is a module in itself.

Python file structure and import usage

Related Article

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.