mod_wsgi: import python modules with the same name
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:
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
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/')
Now, accessing the conf module works for /some_folder/project_1/:
import conf.some_source_file
but not for /another_folder/project_2/:
import conf.another_source_file
AttributeError: 'module' object has no attribute 'another_source_file'
which is probably due to python only searching the first conf path underneath any folders 开发者_运维知识库in sys.path.
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 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
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.
The second import fails, despite the fact that /another_folder/project_2/ is in sys.path. The WSGI application is running in daemon mode.
How can I get this sorted ?
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.
精彩评论