从Python中的相对路径导入

时间:2011-09-21 20:09:36

标签: python python-3.x import shared-libraries relative-path

我有一个用于客户端代码的文件夹,一个用于我的服务器代码的文件夹,以及一个用于在它们之间共享的代码的文件夹

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

如何从Server.py和Client.py导入Common.py?

6 个答案:

答案 0 :(得分:117)

2014年11月编辑(3年后):

Python 2.6和3.x支持正确的相对导入,你可以避免做任何hacky。使用此方法,您知道您正在获取相对导入而不是绝对导入。 '..'表示,转到我上面的目录:

from ..Common import Common

作为一个警告,只有从包的外部将python作为模块运行时,这才有效。例如:

python -m Proj

原始的hacky方式

  

此方法在某些情况下仍然常用,您实际上并未“安装”您的软件包。例如,它受Django用户欢迎。

您可以将Common /添加到您的sys.path(python查看的路径列表以导入内容):

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

os.path.dirname(__file__)只是给你当前python文件所在的目录,然后我们导航到'Common /'目录并导入'Common'模块。

答案 1 :(得分:14)

不要进行相对导入。

来自PEP8

  

非常不鼓励进行包裹内进口的相对进口。

将所有代码放入一个超级包(即“myapp”)中,并使用子包作为客户端,服务器和公共代码。

<强>更新 Python 2.6和3.x支持正确的相对导入(...)”。有关详细信息,请参阅Dave's answers

答案 2 :(得分:6)

进行相对导入是绝对可以的!这就是我所做的一切:

#first change the cwd to the script path
scriptPath = os.path.realpath(os.path.dirname(sys.argv[0]))
os.chdir(scriptPath)

#append the relative location you want to import from
sys.path.append("../common")

#import your module stored in '../common'
import common.py

答案 3 :(得分:6)

有趣的是,我遇到了同样的问题,我按照以下方式完成了这项工作:

与linux命令ln结合使用,我们可以让事情变得更简单:

1. cd Proj/Client
2. ln -s ../Common ./

3. cd Proj/Server
4. ln -s ../Common ./

现在,如果您想将some_stuff从文件Proj/Common/Common.py导入到您的文件中:Proj/Client/Client.py,就像这样:

# in Proj/Client/Client.py
from Common.Common import some_stuff

同样适用于Proj/Server,也适用于setup.py进程, a same question discussed here,希望它有所帮助!

答案 4 :(得分:4)

默认导入方法已经是PYTHONPATH中的“relative”。 PYTHONPATH默认情况下是一些系统库以及原始源文件的文件夹。如果使用-m运行以运行模块,则当前目录将添加到PYTHONPATH。因此,如果程序的入口点在Proj中,那么使用import Common.Common应该在Server.py和Client.py内部工作。

不要进行相对导入。它不会如你所愿。

答案 5 :(得分:0)

我使用的方法与上面提到的 Gary Beardsley 类似,只是稍有改动。

文件名:Server.py

import os, sys
script_path = os.path.realpath(os.path.dirname(__name__))
os.chdir(script_path)
sys.path.append("..")
# above mentioned steps will make 1 level up module available for import
# here Client, Server and Common all 3 can be imported.

# below mentioned import will be relative to root project
from Common import Common
from Client import Client
相关问题