本文介绍了导入具有相同名称的python模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有几个python项目,他们都有一个conf包:
I have several python projects and they all have a conf package:
/some_folder/project_1/
conf/
__init__.py
some_source_file.py
/another_folder/project_2/
conf/
__init__.py
another_source_file.py
对于每个项目,我在site-packages文件夹中使用以下内容创建了一个.pth文件:
For each project, I have created a .pth file in the site-packages folder with this contents:
.../site-packages/project_1.pth:
import sys; sys.path.append('/some_folder/project_1/')
.../site-packages/project_2.pth:
import sys; sys.path.append('/another_folder/project_2/')
我可以访问<$中的模块c $ c> / some_folder / project_1 / :
import conf.some_source_file
但不是 / another_folder / project_2 /
中的模块:
import conf.another_source_file
AttributeError: 'module' object has no attribute 'another_source_file'
看起来python只搜索 conf 路径code> sys.path中。有没有办法解决这个问题?
It looks as if python only searches the first conf
path underneath any folders in sys.path
. Is there a way to fix that ?
推荐答案
没有。您需要重命名其中一个或将项目目录转换为包并通过它导入。
No. You will need to either rename one of them or turn the project directory into a package and import via that.
这篇关于导入具有相同名称的python模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!