【问题标题】:Setting up a Flask app in a Amazon EC2 instance, getting ImportError: No module named flask在 Amazon EC2 实例中设置 Flask 应用程序,得到 ImportError: No module named flask
【发布时间】:2017-11-23 11:53:47
【问题描述】:

我在尝试在 Amazon EC2 服务器上部署我的 Flask 应用程序时遇到问题。

我遵循了herehere as well 的教程。

当我运行我的服务器时,在执行sudo apachect1 restart 之后,我在导航到我的 ec2 站点时收到 500 内部错误。检查日志我收到以下错误。

[Tue Jun 20 19:10:48.429468 2017] [wsgi:error] ImportError: No module named flask, referer: http://ec2-<MY-EC2-URL>.amazonaws.com/

尽管如此,通过我用来访问 EC2 实例的 PuTTY,我可以看到 Python 和 flask 已安装...

Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask
>>> flask.__version__
'0.12.2'

我认为/阅读可能是找不到我的 Python 路径的问题。所以我在我的apache配置文件中添加了python-path=/usr/lib/python2.7....

000-default.conf

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    WSGIDaemonProcess leu threads=5 python-path=/usr/lib/python2.7
    WSGIScriptAlias / /var/www/html/leu/app.wsgi

    <Directory leu>
        WSGIProcessGroup leu
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

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

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

仍然没有运气。我见过很多设置虚拟环境的方法,但我还没有这样做。我最初使用 Python 3.5 在 PyCharm 中创建了项目,但后来我返回项目设置并使用 Python 2.7 创建它并添加了所有相应的导入。当我这样做时仍然没有运气。

我应该用 virtualenv 重新启动吗?设置 Apache 时我缺少什么吗?

我能够获得 Apache 的启动页面,说我设置正确。

app.wsgi

import sys
sys.path.insert(0, 'var/www/html/leu')

from app import app as application

有什么帮助吗?

编辑: 还尝试将我上面的 Apache 配置文件中的 Python 路径更改为 /usr/local/lib/python2.7 但仍然没有运气

【问题讨论】:

  • 我认为第一步是确保您可以在本地访问服务器。 localhost:<port> 在 EC2 上。然后在尝试从浏览器访问它时检查该 EC2 实例的传入端口设置是否打开

标签: python apache python-2.7 amazon-web-services amazon-ec2


【解决方案1】:

使用 sudo -H 安装你的模块。例如 sudo -H pp install flask 如果仍然存在,那么您必须更改配置以使用 python 2.7 而不是 python3

【讨论】:

    【解决方案2】:

    我知道我来得太晚了....希望这对正在苦苦挣扎的人有用!

    ServerName your EC2 instance
    

    您必须提及您的 EC2 实例

    【讨论】:

      猜你喜欢
      • 2021-11-11
      • 2017-02-20
      • 2021-07-20
      • 2019-04-03
      • 1970-01-01
      • 2013-06-27
      • 2018-07-20
      • 1970-01-01
      • 2016-06-13
      相关资源
      最近更新 更多