keywords: [Python]从相对路径导入(Importing from a relative path)

Case 1

假设python工程结构如下:

Proj/
    Client/
        Client.py
    Server/
        Server.py
    Common/
        __init__.py
        Common.py

要在Client.py中import Common目录。代码如下:

import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'Common'))
import Common

其中os.path.dirname(__file__)表示当前python脚本的所在目录。

参考:
Importing from a relative path in Python
https://stackoverflow.com/questions/7505988/importing-from-a-relative-path-in-python

Case 2

file structure:

main.py
folder/
    file.py

In file.py I have a class imlpemented (Klasa), how to import file.py in main.py?
method 1:

from folder.file import Klasa

method 2:

from folder import file
k = file.Klasa()

method 3:

import folder.file as myModule
k = myModule.Klasa()

Origin:
Importing class from another file
https://stackoverflow.com/a/41276151/1645289


我行过许多地方的桥,看过许多次数的云,喝过许多种类的酒,却只爱过一个正当最好年龄的人。----沈从文《湘行散记》