导入同名的python模块 [英] import python modules with the same name
本文介绍了导入同名的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/')
我可以访问/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 只搜索 sys.path
中任何文件夹下的第一个 conf
路径.有没有办法解决这个问题?
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模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文