简体   繁体   English

mod_python和子包导入问题:ImportError:未命名模块

[英]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']"

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.

相关问题 Freeswitch mod_python没有名为freeswitch的模块 - Freeswitch mod_python No module named freeswitch mod_python没有名为_apache的模块 - mod_python no module named _apache 在mod_python下执行模块时ImportError - ImportError when executing module under mod_python 导入Python包-“ ImportError:未命名模块...” - Importing Python package - “ImportError: No module named…” Django mod_python日志记录问题 - Django mod_python logging issues “ ImportError:没有名为xhaus的模块” Python模块从ansible导入错误 - “ImportError: No module named xhaus” Python module importing error from ansible Django Python mod_wsgi:ImportError:没有名为“ django”的模块 - Django Python mod_wsgi: ImportError: No module named 'django' Python-导入paypalrestsdk导致ImportError:没有名为_winreg的模块 - Python - Importing paypalrestsdk causes ImportError: No module named _winreg 导入Pocketsphinx无法在python 3.2(Windows)中运行。 “ ImportError:没有名为pocketsphinx的模块” - Importing pocketsphinx not working python 3.2 (Windows). “ImportError: No module named pocketsphinx” 跨包导入模块时,Python“ ImportError:未命名模块” - Python 'ImportError: No module named' when importing modules across packages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM