从Python中的相对路径导入
我有一个用于存放客户端代码的文件夹、一个用于存放服务器代码的文件夹以及一个用于在它们之间共享的代码的文件夹
Proj/
Client/
Client.py
Server/
Server.py
Common/
__init__.py
Common.py
如何从 Server.py 和 Client.py 导入 Common.py?
I have a folder for my client code, a folder for my server code, and a folder for code that is shared between them
Proj/
Client/
Client.py
Server/
Server.py
Common/
__init__.py
Common.py
How do I import Common.py from Server.py and Client.py?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
2014 年 11 月编辑(3 年后):
Python 2.6 和 3.x 支持适当的相对导入,您可以避免做任何黑客行为。通过这种方法,您知道您正在获得相对导入,而不是绝对导入。 “..”的意思是,转到我上面的目录:
需要注意的是,只有当您从包的外部将Python作为模块运行时,这才有效。例如:
原始的 hacky 方式
你可以将 Common/ 添加到你的 sys.path (python 用来导入东西的路径列表):
os.path.dirname(__file__) 只是给你当前 python 文件所在的目录,然后我们导航到“Common/”目录并导入“Common”模块。
EDIT Nov 2014 (3 years later):
Python 2.6 and 3.x supports proper relative imports, where you can avoid doing anything hacky. With this method, you know you are getting a relative import rather than an absolute import. The '..' means, go to the directory above me:
As a caveat, this will only work if you run your python as a module, from outside of the package. For example:
Original hacky way
You can add Common/ to your sys.path (the list of paths python looks at to import things):
os.path.dirname(__file__)
just gives you the directory that your current python file is in, and then we navigate to 'Common/' the directory and import 'Common' the module.有趣的是,我刚刚遇到了同样的问题,我通过以下方式完成了这项工作:
与 linux 命令
ln
结合,我们可以使事情变得更简单:而且,现在如果你想导入
some_stuff
从文件:Proj/Common/Common.py
到您的文件:Proj/Client/Client.py
,就像这样:并且,相同适用于
Proj/Server
,也适用于setup.py
进程,这里讨论了同样的问题,希望有帮助!
Funny enough, a same problem I just met, and I get this work in following way:
combining with linux command
ln
, we can make thing a lot simper:And, now if you want to import
some_stuff
from file:Proj/Common/Common.py
into your file:Proj/Client/Client.py
, just like this:And, the same applies to
Proj/Server
, Also works forsetup.py
process,a same question discussed here, hope it helps !
进行相对导入是绝对可以的!这是小我所做的:
Doing a relative import is absolulutely OK! Here's what little 'ol me does:
不要进行相对导入。
来自 PEP8:
将所有代码放入一个超级包(即“myapp”)中,并为客户端、服务器和公共代码使用子包。
更新:
“Python 2.6 和 3.x 支持正确的相对导入 (...)”。有关更多详细信息,请参阅 Dave 的回答。
Don't do relative import.
From PEP8:
Put all your code into one super package (i.e. "myapp") and use subpackages for client, server and common code.
Update:
"Python 2.6 and 3.x supports proper relative imports (...)". See Dave's answers for more details.
默认导入方法已经是“相对”的,来自 PYTHONPATH。 PYTHONPATH 默认情况下与原始源文件的文件夹一起指向某些系统库。如果使用 -m 运行模块,当前目录将添加到 PYTHONPATH 中。因此,如果程序的入口点位于 Proj 内部,那么使用 import Common.Common 应该可以在 Server.py 和 Client.py 内部工作。
不要进行相对导入。它不会按照你想要的方式工作。
The default import method is already "relative", from the PYTHONPATH. The PYTHONPATH is by default, to some system libraries along with the folder of the original source file. If you run with -m to run a module, the current directory gets added to the PYTHONPATH. So if the entry point of your program is inside of Proj, then using
import Common.Common
should work inside both Server.py and Client.py.Don't do a relative import. It won't work how you want it to.
我使用的方法与上面提到的Gary Beardsley类似,但有一些小变化。
文件名:Server.py
Approch used by me is similar to Gary Beardsley mentioned above with small change.
Filename: Server.py
创建一个简单的示例
假设我们在当前工作目录中运行 ls -R ,结果如下:
我们运行此命令 $ python3 secondary-karma/import.py
< strong>init.py 是一个空文件,但它应该存在。
现在让我们看看
second-karma/import.py
里面有什么:里面的Second_karma/math/fibonacci.py:
现在最后一个文件是Second_karma/enemy.py:
现在一个简单的答案为什么它不起作用:
python3 path/to/file.py
)。import.py
引用了导入.math
.math
表示“在当前包中查找名为 math 的模块/包”$ python3 secondary-karma/import.py
执行时,我正在执行一个模块,而不是一个包。因此 python 不知道.
在这种情况下意味着什么import.py
属于父包second_karma
,因此您的相对导入将起作用。重要提示:
这些
__init__.py
是必需的,如果您没有它们,则必须先创建它们。github 中的示例
Create a simple example
Assume we run
ls -R
in the current working directory and this is the result:And we run this command
$ python3 second-karma/import.py
init.py is an empty file but it should exists.
Now let's see what is inside the
second-karma/import.py
:And what is inside the
second_karma/math/fibonacci.py
:Now the last file is
second_karma/enemy.py
:Now a simple answer why it was not working:
python3 path/to/file.py
).import.py
makes reference to importing.math
.math
in this context means "go find a module/package in the current package with the name math"$ python3 second-karma/import.py
I am executing a module, not a package. thus python has no idea what.
means in this contextimport.py
is of parent packagesecond_karma
, and thus your relative import will work.Important note:
Those
__init__.py
are necessary and if you have not them you must create them first.An example in github
README.md
for a better comprehension