【发布时间】:2017-08-16 10:01:28
【问题描述】:
我有一个在多个项目中使用的共享 python 库,所以结构如下所示:
Project1
main.py <--- (One of the projects that uses the library)
...
sharedlib
__init__.py
ps_lib.py
another.py
现在在每个项目的 main.py 中,我使用以下 hack 使其工作:
import os
import sys
sys.path.insert(0, os.path.abspath('..'))
import sharedlib.ps_lib
...
有没有办法在不使用这个 hack 的情况下做到这一点?或者有没有更好的方法来组织项目结构?
【问题讨论】:
-
为什么不直接“安装”
sharedlib作为包?然后你可以在任何地方导入它。 -
@Rahul 我不喜欢这种方法,因为它不优雅,而且在项目的每个模块中你都会有“from .. import ...”
-
@MSeifert 但是在共享库中的每个小修复之后,我将不得不再次 pip 安装它
-
不一定,您可以安装类似
python setup.py develop的“符号链接”。这样,如果您更改sharedlib,更改将立即生效(或者如果您使用交互式解释器,则在解释器重新启动后)。 -
@MSeifert 这是个好主意!符号链接可以在 Windows 中使用吗?
标签: python python-import directory-structure organization project-structure