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)