mod_wsgi:导入具有相同名称的python模块 [英] mod_wsgi: import python modules with the same name

查看:219
本文介绍了mod_wsgi:导入具有相同名称的python模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

导入具有相同名称的python模块的后续问题,但由于两者有点不相关,最好提出一个新问题:

This is a follow-up question to import python modules with the same name, but as the two are somewhat unrelated, it's probably better to ask a new question:

我有几个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/')

现在,访问conf模块有效for / some_folder / project_1 /:

Now, accessing the conf module works for /some_folder/project_1/:

import conf.some_source_file

但不适用于/ another_folder / project_2 /:

but not for /another_folder/project_2/:

import conf.another_source_file
AttributeError: 'module' object has no attribute 'another_source_file'

这可能是由于python只搜索sys.path中任何文件夹下面的第一个conf路径。

which is probably due to python only searching the first conf path underneath any folders in sys.path.

因此我将pr​​oject_1和project_2转换为python包并在其中添加了一个符号链接,所以我可以完全合格的方式访问这两个conf包:

I have therefore turned project_1 and project_2 into python packages and added a symlink in either, so I can access both conf packages in a fully qualified fashion:

/some_folder/project_1/
  __init__.py
  project_1 <-- symlink to .
  conf/
    __init__.py
    some_source_file.py

/another_folder/project_2/
  __init__.py
  project_2 <-- symlink to .
  conf/
    __init__.py
    another_source_file.py

In一个普通的python脚本,我现在可以从两个conf包中导入模块:

In an ordinary python script, I can now import modules from both conf packages:

import project_1.conf.some_source_file
import project_2.conf.another_source_file

然而,当我尝试做同样的事情时在使用mod_wsgi Apache模块的WSGI应用程序中,第二个项目的导入失败。具体来说,wsgi'start file'(即在我的虚拟服务器的Apache配置文件中的WSGIScriptAlias语句中引用了wsgi后缀的那个)成功导入/ another_folder / project_2 /中的模块,该模块又导入project_2 / conf / another_source_file .py。

However, when I try to do the same within an WSGI application using the mod_wsgi Apache module, the import fails for the second project. Specifically, the wsgi 'start file' (i.e. the one with a wsgi suffix referenced in the WSGIScriptAlias statement in the Apache config file for my virtual server) successfully imports a module in /another_folder/project_2/ which in turn imports project_2/conf/another_source_file.py.

第二次导入失败,尽管/ another_folder / project_2 /在sys.path中。 WSGI应用程序以守护进程模式运行。

The second import fails, despite the fact that /another_folder/project_2/ is in sys.path. The WSGI application is running in daemon mode.

如何对此进行排序?

推荐答案

FWIW,这是在mod_wsgi邮件列表和内存中处理的,结果是由于存在同名的模块和包而导致错误的一个。

FWIW, this was dealt with on mod_wsgi mailing list and from memory turned out to be caused by presence of both a module and package with same name and was picking up wrong one.

这篇关于mod_wsgi:导入具有相同名称的python模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆