mod_python在虚拟环境下无法找到web.py模块
我有一个小型 web.py
Python 应用程序,我想使用 mod_python
在 Apache 下提供服务。 web.py
框架以及相关应用程序所依赖的其他第三方 Python 模块安装在虚拟环境中。虚拟环境在内部创建: /home/ayaz/Sandbox/Scrapper/
我用来设置此应用程序的 Apache 虚拟主机配置的相关片段如下:
<Location "/api">
PythonPath "['/home/ayaz/Sandbox/Scrapper/lib/python2.5/site-packages/', '/home/ayaz/project/'] + sys.path"
#PythonHandler wsgiref.modpython_gateway::handler
PythonHandler modpython_gateway::handler
SetHandler python-program
PythonOption wsgi.application device_api::main
PythonOption device_api /api/
</Location>
在浏览器上然而,当我尝试访问 /api
URL 时,我从服务器收到 500
,日志中显示错误,指出 web
模块无法找到从 device_api.py
文件(这是我的应用程序)导入的;换句话说,我看到一个 ImportError
。我不知道为什么它找不到 web
模块。
从 Python 解释器能够找到 device_api.py
的事实来看,我知道 PythonPath
指令正在工作,至少部分来自该指令中定义的路径的文件。但是,它无法找到在同一指令中也定义了路径的其余模块。
对此的任何帮助将不胜感激。
谢谢!
更新 #1
Ned 的回复让我着迷 通过
site-packages
目录 对于我拥有的虚拟环境。 虽然权限看起来不错 在我的目录里面,我意识到mod_python/Apache
无法 读取鸡蛋
。自从我安装了 虚拟内的所有包 使用easy_install
的环境,它们都是形式 鸡蛋。所以,举例来说,如果我搬家了 目录 /home/ayaz/Sandbox/Scrapper/lib/python2.5/site-packages/web.py-0.34-py2.5.egg/web/ 进入 /home/ayaz/Sandbox/Scrapper/lib/python2.5/site-packages/ (基本上是从鸡蛋中取出 文件/目录),mod_python
已停止 抱怨缺少web
模块(当然,它然后启动 抱怨其余的失踪 第三方模块)。我已将 Apache 配置为作为我的运行 用户和组,即
ayaz
,而我 检查了权限/home/ayaz/.python-eggs
目录 都很好。然后我使用了这个 使用鸡蛋 mod_python 被欺骗的解释 Django 部署文档 页。但这没有帮助。 最终,我硬着头皮,为了 每个第三方模块
mod_python
抱怨它不能 发现,我移动了实际目录 从该模块的鸡蛋内部 从鸡蛋外面到里面site-packages
目录 虚拟环境。这使得mod_python
找到模块。现在,我真的不知道为什么会这样 正在发生,为什么
mod_python
没有发生 能够读懂鸡蛋。
I have a small web.py
Python application that I would like to serve under Apache using mod_python
. The web.py
framework, as well as other third-party Python modules the application in question relies upon, are installed in a virtual environment. The virtual environment is created inside: /home/ayaz/Sandbox/Scrapper/
The relevant snippet from the virtual host configuration for Apache that I am using in order to set up this application is this:
<Location "/api">
PythonPath "['/home/ayaz/Sandbox/Scrapper/lib/python2.5/site-packages/', '/home/ayaz/project/'] + sys.path"
#PythonHandler wsgiref.modpython_gateway::handler
PythonHandler modpython_gateway::handler
SetHandler python-program
PythonOption wsgi.application device_api::main
PythonOption device_api /api/
</Location>
On the browser when I try to access the /api
URL however, I get a 500
from the server with the error in the logs saying that the web
module imported from within the device_api.py
file (which is my application) could not be found; in other words, I see an ImportError
. I am not sure why it is unable to find the web
module.
I know that the PythonPath
directive is working, at least partially, judging from the fact that the Python interpreter is able to find the device_api.py
file from the path defined in that directive. But, it is unable to find the rest of the modules for which the path is also defined in the same directive.
Any help with this will be deeply appreciated.
Thanks!
UPDATE #1
Ned's reply had me looking
through thesite-packages
directory
for the virtual environment I have.
While the permissions looked fine
inside the directory to me, I realized
thatmod_python/Apache
was not able
to read theeggs
. Since I installed
all the packages inside the virtual
environment usingeasy_install
, they are all in the forms
of eggs. So, for example, if I moved
the directory/home/ayaz/Sandbox/Scrapper/lib/python2.5/site-packages/web.py-0.34-py2.5.egg/web/
into/home/ayaz/Sandbox/Scrapper/lib/python2.5/site-packages/
(essentially taking it out of the egg
file/directory),mod_python
stopped
complaining about the missingweb
module (of course, it then started
complaining about the missing rest of
the third-party modules).I have Apache configured to run as my
user and group, that isayaz
, and I
checked that the permissions on the/home/ayaz/.python-eggs
directory
were fine.I then used this Using eggs with
mod_python tricked explained on
the Django deployment documentation
page. But it didn't help.
Eventually, I bit the bullet, and for
each third-party module thatmod_python
complained it couldn't
find, I moved the actual directory
from inside the egg for that module
outside of the egg and into thesite-packages
directory for the
virtual environment. This mademod_python
find the modules.Now, I am really not sure why this
is happening, and whymod_python
isn't
able to read through the eggs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
会不会是文件或目录权限问题?验证是否可以通过
Apache
和mod_python< 的用户名访问
virtualenv
site-packages
中的所有文件和目录/code> 下运行。Could it be a file or directory permission problem? Verify that the all of the files and directories in your
virtualenv
site-packages
are accessible from the user name thatApache
andmod_python
are running under.为了回答这个问题,我将复制问题中的更新段落并将它们作为答案发布(因为它们似乎在某种程度上解释了我如何设法摆脱问题)。
So as to answer this question, I am going to copy the update paragraphs from my question and post them as the answer (as they seem to explain to some extent how I manage to get rid of the problem).