[英]mod_python and subpackages importing issues: ImportError: No module named
I'm exploring mod_python and I'm having trouble with the package importing. 我正在探索mod_python,而软件包导入遇到了麻烦。
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: 和一个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>
in the index.py file I've something like this: 在index.py文件中,我有以下内容:
from package.package import myobject
....
....
When I load index.py from Apache, I get a 500 Internal Server Error as follows: 从Apache加载index.py时,出现500内部服务器错误,如下所示:
ImportError: No module named package.package
What am I doing wrong? 我究竟做错了什么?
Cheers, Ivan 干杯,伊万
Firstly, if you're just beginning with Python web deployment you should not be using mod_python. 首先,如果您刚开始进行Python Web部署, 则不应该使用mod_python。 It is now officially a dead project and is deprecated.
现在,它正式是一个死项目,不建议使用。 Use mod_wsgi instead.
请改用mod_wsgi。
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. 代码的实际问题是您没有将根目录放在Python路径上,因此mod_python不知道在哪里可以找到它。 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.
DocumentRoot用于静态文档,而不用于代码-实际上,您不应将其设置为基本目录,因为这是不安全的,并且可能导致Python代码的内容在网络上公开,这不是您想要的。
Instead, use the PythonPath directive: 而是使用PythonPath指令:
PythonPath "['/path/to/my base dir']"
确保您的PYTHONPATH是正确的: http : //www.modpython.org/live/mod_python-3.2.8/doc-html/dir-other-pp.html
In mod_python 3.3, the structure of Python code files for mod_python.publisher is not a package. 在mod_python 3.3中,mod_python.publisher的Python代码文件的结构不是包。 Ensure you read:
确保您阅读:
http://www.modpython.org/live/current/doc-html/pyapi-apmeth.html http://www.modpython.org/live/current/doc-html/pyapi-apmeth.html
Specifically, the documentation about import_module() as it explains how code importing works. 具体来说,有关import_module()的文档解释了代码导入的工作方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.