导入同名模块
Importing modules with same name
基本上,我有两个 python 项目,一个位于 myapp/screening
下,另一个 myapp/server
。我目前正在开发 server
模块并希望使用 myapp.screening
.
从 screening
导入函数
我的文件夹结构如下图:
myapp/
screening/
screening-env/
myapp/
__init__.py
screening/
__init__.py
screening_task.py
submodule1/
# __init__.py and ub module files
submodule2/
# __init__.py and sub module files
submodule3/
# __init__.py and sub module files
tests/
# __init__.py and test scripts
setup.py
server/
server-env/
myapp/
__init__.py
server/
__init__.py
server_task.py
tests/
__init__.py
server_test.py
我按照这个 构建了我的项目。
我的setup.py
基本如下:
from setuptools import setup
setup(
name='myapp-screening',
version='0.1.0',
packages=[
'myapp.screening',
'myapp.screening.submodule1',
'myapp.screening.submodule2',
'myapp.screening.submodule3'
],
)
我激活了 server-env
并通过导航到 myapp/screening/
(与 setup.py
相同的目录)和 运行 python setup.py develop
.[= 安装了筛选项目35=]
最后,server_test.py
和server_task
都如下图:
from myapp.screening.screening_test import foo
foo()
当我 运行 python -m myapp.server.server_task
或 python -m test.server_test
我得到:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'myapp.screening'
如果我运行宁python -m myapp.server.server_task
这个错误是有道理的,因为本地myapp
存在并且可能覆盖安装的myapp
包含 screening
个模块。
有没有办法使用 from myapp.screening.screening_task import foo
从 screening
导入内容?!
所以,经过更多的研究,我发现 this similar (in a way) question that leads to import python modules with the same name and How do I create a namespace package in Python?。
"importing modules with same name" 的答案没有用,因为它说要重命名一个模块或将项目目录变成一个包。
另外一个问题正是我想要的。它基本上讨论了 pkgutil
,您可以使用它 'append' 模块到给定的命名空间。
在某些情况下,我理解并反对这种技术(例如 this) but since it's presented 有时你想要分离的结构,这样即使你不想要所有东西,你也不会把所有东西都修补在一起
基本上,我有两个 python 项目,一个位于 myapp/screening
下,另一个 myapp/server
。我目前正在开发 server
模块并希望使用 myapp.screening
.
screening
导入函数
我的文件夹结构如下图:
myapp/
screening/
screening-env/
myapp/
__init__.py
screening/
__init__.py
screening_task.py
submodule1/
# __init__.py and ub module files
submodule2/
# __init__.py and sub module files
submodule3/
# __init__.py and sub module files
tests/
# __init__.py and test scripts
setup.py
server/
server-env/
myapp/
__init__.py
server/
__init__.py
server_task.py
tests/
__init__.py
server_test.py
我按照这个
我的setup.py
基本如下:
from setuptools import setup
setup(
name='myapp-screening',
version='0.1.0',
packages=[
'myapp.screening',
'myapp.screening.submodule1',
'myapp.screening.submodule2',
'myapp.screening.submodule3'
],
)
我激活了 server-env
并通过导航到 myapp/screening/
(与 setup.py
相同的目录)和 运行 python setup.py develop
.[= 安装了筛选项目35=]
最后,server_test.py
和server_task
都如下图:
from myapp.screening.screening_test import foo
foo()
当我 运行 python -m myapp.server.server_task
或 python -m test.server_test
我得到:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'myapp.screening'
如果我运行宁python -m myapp.server.server_task
这个错误是有道理的,因为本地myapp
存在并且可能覆盖安装的myapp
包含 screening
个模块。
有没有办法使用 from myapp.screening.screening_task import foo
从 screening
导入内容?!
所以,经过更多的研究,我发现 this similar (in a way) question that leads to import python modules with the same name and How do I create a namespace package in Python?。
"importing modules with same name" 的答案没有用,因为它说要重命名一个模块或将项目目录变成一个包。
另外一个问题正是我想要的。它基本上讨论了 pkgutil
,您可以使用它 'append' 模块到给定的命名空间。
在某些情况下,我理解并反对这种技术(例如 this) but since it's presented