Python Learning note 7 Add rules for header files (reprint)

Source: Internet
Author: User

Reprinted from: https://www.cnblogs.com/taurusfy/p/7605787.html

***************************************************************************************************

The following is the text:

A Python project in which one file needs to refer to a class in another file, several problems encountered, summarized as follows:

    • Situation one: Under the same directory

Directory structure:

   |--a.py   |--b.py   |--main.py

  

In main.py, you need to refer to the classes in a.py and b.py:

From a imoport *from b imoport *

  

    • Scenario Two: The main path is lowered with the class in the subfolder

Directory structure:

   |--a       |---a.py   |--b       |---b.py   |--main.py

  

In main.py, you need to refer to the classes in a.py and b.py, and A, B, to folders. But the direct quote would be an error .

From A.A imoport *from b.b Imoport *

  

The workaround is to add the __init__.py file to the A/b folder

Directory structure:

   |--a       |---__init__.py       |---a.py   |--b       |---__init__.py       |---b.py   |--main.py

  

    • Scenario Three: calls between different folders

Directory structure:

   |--a       |---__init__.py       |---a.py   |--b       |---__init__.py       |---b.py   |--main.py

  

The class in B is called in A, or the class in B is called in a

In the b.py

Import syssys.path.append (' e:\\testnow\\project\\a ') import a

  

The path format for Linux and window is not the same, this method is suitable for all cross-folder invocation situations, but note the path format.

    • Duplicate call problem for the same module that occurs when a file is called

Directory structure:

|--a    |---__init__.py    |---a.py|--b    |---__init__.py    |---b.py|--main.py

  

For example, the a.py called the

From time Import * from datetime import datetime

  

And the main.py import a:

From A.A Import *

  

Then you can use Time (), DateTime ()-related functions directly in main.py, no need to import any more.

If you import time in main.py and call the related function time.time () or Time.sleep (1), an error will occur.

Even if the call is repeated, the consistency of the call is guaranteed.

Python Learning note 7 Add rules for header files (reprint)

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.