如果你决定使用包,你可以在 setup.py 中指定另一个本地包的路径。
你的整体结构是:
|-- Repo1/
| |-- setup.py
| |-- Repo1/
│ | |-- __init__.py
| | |--Code.py
| |-- tests/
| | |--test_Code.py
|
|-- Repo2/
| |-- setup.py
| |-- Repo2/
│ | |-- __init__.py
| | |--testcode.py
repo1/setup.py.
from setuptools import setup, find_packages
import os
# dynamically determine the path to Repo2
local_name = "Repo2"
local_path = os.getcwd().split(os.sep)
local_path = os.sep.join(local_path[0:local_path.index(local_name)])
local_path = os.path.join(local_path, local_name)
setup(
name="Repo1",
version="1.0.0",
description="First Repo",
python_requires=">=3.5.0",
packages = find_packages(),
install_requires=[
'SomePyPIPackage',
f"{local_name} @ file://localhost/{local_path}#egg={local_name}"
]
)
Repo2 中的 setup.py 文件类似,但没有 install_requires localhost 部分。
然后,当您在 Repo1 的顶级目录中时,您可以运行(不要忘记末尾的点):
# would have to reinstall if you make changes to Repo2
pip install .
# or install in editable mode
pip install -e .
那么在 Repo1 中你应该可以使用:
from repo2.testcode import testmodule