当 运行 来自 apache uwsgi 时,无法在 python 中导入我自己的 .py 模块

Can't import my own .py modules in python when run from apache uwsgi

我想创建自己的 API,所以我选择了 apache2、uwsgi、flask 和 python。 我遵循了这些说明 here

我更改了mod规则:

我更改了命令:

此时我正常继续引导并使系统正常工作。 但是当我尝试添加自己的文件时出现错误

Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/3.5.2 配置

阿帕奇 error.log:

[Wed Jun 27 15:12:53.838360 2018] [wsgi:error] [pid 24654:tid 140042661635840] [remote 192.168.1.6:38961] mod_wsgi (pid=24654): Target WSGI script '/var/www/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module.
[Wed Jun 27 15:12:53.838397 2018] [wsgi:error] [pid 24654:tid 140042661635840] [remote 192.168.1.6:38961] mod_wsgi (pid=24654): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'.
[Wed Jun 27 15:12:53.838553 2018] [wsgi:error] [pid 24654:tid 140042661635840] [remote 192.168.1.6:38961] Traceback (most recent call last):
[Wed Jun 27 15:12:53.838572 2018] [wsgi:error] [pid 24654:tid 140042661635840] [remote 192.168.1.6:38961]   File "/var/www/FlaskApp/flaskapp.wsgi", line 11, in <module>
[Wed Jun 27 15:12:53.838574 2018] [wsgi:error] [pid 24654:tid 140042661635840] [remote 192.168.1.6:38961]     from FlaskApp import app as application
[Wed Jun 27 15:12:53.838577 2018] [wsgi:error] [pid 24654:tid 140042661635840] [remote 192.168.1.6:38961]   File "/var/www/FlaskApp/FlaskApp/__init__.py", line 3, in <module>
[Wed Jun 27 15:12:53.838579 2018] [wsgi:error] [pid 24654:tid 140042661635840] [remote 192.168.1.6:38961]     import utils
[Wed Jun 27 15:12:53.838587 2018] [wsgi:error] [pid 24654:tid 140042661635840] [remote 192.168.1.6:38961] ImportError: No module named 'utils'

这是 apache 配置文件:

<VirtualHost *:80>
ProxyPreserveHost On
ServerAdmin webmaster@localhost

WSGIScriptAlias /api /var/www/FlaskApp/flaskapp.wsgi
<Directory /var/www/FlaskApp/FlaskApp/>
    Order allow,deny
    Allow from all
</Directory>

ProxyPass /api !
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/

ErrorDocument 404 /index.html

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

当系统工作时,我确实检查了 __init__:py 文件在哪里看到它自己

路径:/var/www/FlaskApp/FlaskApp

当 运行 在 python 或虚拟环境

中的 apache 之外时,这一切都有效

我也尝试通过 pip 安装 modules 但它们没有用

尝试在 wsgi 文件中的导入之前添加以下行:

import sys
sys.path.append("/path/to/directory/containing/utils")