mod_python和子包导入问题:ImportError:没有名为模块
我正在探索 mod_python,但在导入包时遇到问题。
我有一个这样的结构:
my base dir
|
+- __init__.py
+- index.py
+- package (directory)
|
+- __init__.py
+- package.py (file)
和一个像这样的 Apache 虚拟主机:
<VirtualHost *:80>
ServerAdmin root at localhost
ServerName myname
DocumentRoot /path/to/my base dir
<Location />
DirectoryIndex index.html index.py
Options Indexes MultiViews FollowSymLinks
AddHandler mod_python .py
PythonHandler mod_python.publisher
</Location>
</VirtualHost>
在 index.py 文件中,我有这样的内容:
from package.package import myobject
....
....
当我从 Apache 加载 index.py 时,我收到 500 内部服务器错误 如下:
ImportError: No module named package.package
我做错了什么?
干杯, 伊万
I'm exploring mod_python and I'm having trouble with the package importing.
I've a structure like this:
my base dir
|
+- __init__.py
+- index.py
+- package (directory)
|
+- __init__.py
+- package.py (file)
and an Apache Virtual Host like this:
<VirtualHost *:80>
ServerAdmin root at localhost
ServerName myname
DocumentRoot /path/to/my base dir
<Location />
DirectoryIndex index.html index.py
Options Indexes MultiViews FollowSymLinks
AddHandler mod_python .py
PythonHandler mod_python.publisher
</Location>
</VirtualHost>
in the index.py file I've something like this:
from package.package import myobject
....
....
When I load index.py from Apache, I get a 500 Internal Server Error as
follows:
ImportError: No module named package.package
What am I doing wrong?
Cheers,
Ivan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,如果您刚刚开始使用 Python Web 部署,那么您不应该使用 mod_python。它现在正式成为一个死项目并且已被弃用。请改用 mod_wsgi。
您的代码的实际问题是您没有将根目录放在 Python 路径上,因此 mod_python 不知道在哪里可以找到它。 DocumentRoot 用于静态文档,而不是代码 - 事实上,您不应该将其设置为您的基本目录,因为这是不安全的,并且可能导致 Python 代码的内容在网络上公开,这不是你想要的。
相反,使用 PythonPath 指令:
Firstly, if you're just beginning with Python web deployment you should not be using mod_python. It is now officially a dead project and is deprecated. Use mod_wsgi instead.
The actual issue with your code is that you haven't put your root directory on the Python path, so mod_python doesn't know where to find it. DocumentRoot is used for static documents, not code - in fact you shouldn't set it to your base dir, as that is insecure and may lead to the contents of your Python code being exposed over the web, which is not what you want.
Instead, use the PythonPath directive:
确保您的 PYTHONPATH 正确:
http://www.modpython。 org/live/mod_python-3.2.8/doc-html/dir-other-pp.html
Make sure your PYTHONPATH is correct:
http://www.modpython.org/live/mod_python-3.2.8/doc-html/dir-other-pp.html
在 mod_python 3.3 中,mod_python.publisher 的 Python 代码文件结构不是包。确保您阅读:
http://www.modpython.org/ live/current/doc-html/pyapi-apmeth.html
具体来说,有关 import_module() 的文档解释了代码导入的工作原理。
In mod_python 3.3, the structure of Python code files for mod_python.publisher is not a package. Ensure you read:
http://www.modpython.org/live/current/doc-html/pyapi-apmeth.html
Specifically, the documentation about import_module() as it explains how code importing works.