导入具有相同名称的python模块

ssc*_*ssc 6 python import module

我有几个python项目,他们都有一个conf包:

/some_folder/project_1/
  conf/
    __init__.py
    some_source_file.py

/another_folder/project_2/
  conf/
    __init__.py
    another_source_file.py
Run Code Online (Sandbox Code Playgroud)

对于每个项目,我在site-packages文件夹中使用以下内容创建了一个.pth文件:

.../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/')
Run Code Online (Sandbox Code Playgroud)

我可以访问以下模块/some_folder/project_1/:

import conf.some_source_file
Run Code Online (Sandbox Code Playgroud)

但不是以下模块/another_folder/project_2/:

import conf.another_source_file
AttributeError: 'module' object has no attribute 'another_source_file'
Run Code Online (Sandbox Code Playgroud)

看起来好像python只搜索conf任何文件夹下面的第一个路径sys.path.有办法解决这个问题吗?

Ign*_*ams 9

不需要.您需要重命名其中一个或将项目目录转换为包并通过它导入.