【问题标题】:Importing python modules with mod_wsgi使用 mod_wsgi 导入 python 模块
【发布时间】:2012-11-05 18:50:11
【问题描述】:

我们正试图让 wsgi 应用程序在 mod_wsgi 上运行。在使用wsgiref.simple_server 作为调试环境之前,我们已经运行了它。现在,我们想把它转移到使用 Apache httpd 来集成在同一端口下加载静态文件,并提供一个更适合生产的环境。

我们设置的 httpd.conf 文件如下所示:

<VirtualHost *:80>

    DocumentRoot /var/www/

    ErrorLog /var/www/log/error_log

    LogLevel debug

    WSGIScriptAlias /app /var/www/python/resource/rest.py

    <Directory /var/www/python/>
            Order allow,deny
            Allow from all
    </Directory>

    <Directory />
            Order allow,deny
            Allow from all

            RewriteEngine On
            # Some rewrites go here

    </Directory>

</VirtualHost>

通过此设置,我们可以访问主应用程序文件 rest.py(在 http://myhost/app 下),但是任何对模块的导入(如 from module1 import function,其中 module1 与 rest.py 在同一目录下)都失败了ImportError

我怀疑这与必须设置某种环境变量有关,但我已在主应用程序方法上将 /var/www/python 添加到 sys.path 并且它不起作用。对此的任何帮助或指示将不胜感激。

谢谢!

【问题讨论】:

标签: python apache mod-wsgi


【解决方案1】:

rest.py 是在 /var/www/python 还是 /var/www/python/resource 下?据我所知,将此目录添加到 sys.path 应该可以工作。最好使用字符串常量:

sys.path.append(os.path.dirname(__file__))

即使您决定将来部署到其他位置,这也应该可以工作。

【讨论】:

  • 好的,一定会试一试。有空我会更新。谢谢!
猜你喜欢
  • 2013-10-24
  • 2011-10-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-06
  • 2011-12-20
  • 2019-02-27
  • 2011-12-29
  • 1970-01-01
相关资源
最近更新 更多