【问题标题】:Import Python modules with the same name导入同名的 Python 模块
【发布时间】:2011-07-08 20:52:20
【问题描述】:

我有几个 Python 项目,它们都有一个 conf 包:

/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 文件,其中包含以下内容:

.../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路径。有办法解决吗?

【问题讨论】:

标签: python import module


【解决方案1】:

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

【讨论】:

  • 正确。我实际上一直在这样做,现在我意识到问题似乎不在于 python 本身,而在于第二个项目正在其上下文中运行的 mod_wsgi apache 模块。当然,这是一个完全不同的故事,所以我发布了一个新问题:stackoverflow.com/questions/6631826/…
猜你喜欢
  • 2011-10-01
  • 1970-01-01
  • 2012-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-26
  • 2011-10-29
相关资源
最近更新 更多