gpt4 book ai didi

python - 在 EC2 Ubuntu 上运行的 Flask 服务器找不到 python 包

转载 作者:行者123 更新时间:2023-12-04 19:25:49 25 4
gpt4 key购买 nike

我正在使用带有 Apache 的 AWS EC2 运行 flask 服务器。问题是 flask 服务器无法识别 python 包的位置。
Python 包位于“/home/ubuntu/.local/lib/python3.8/site-packages/”
下面是/var/www/html/flaskapp/flaskapp.py

    import numpy as np

from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/')
def hello_world():

return jsonify("Hello From Flask")

@app.route('/countme/<input_str>')
def count_me(input_str, methods = ['GET']):
return jsonify(input_str)

if __name__ == '__main__':
app.run()
当我尝试访问我的服务器时,出现此错误
    ModuleNotFoundError: No module named 'numpy'

下面是'flaskapp.wsgi'
    import sys
import site

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

from flaskapp import app as application
下面是'/etc/apache2/sites-enabled/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

WSGIDaemonProcess flaskapp threads=5 python-path=/var/www/html/flaskapp/flaskapp
WSGIScriptAlias / /var/www/html/flaskapp/flaskapp.wsgi

<Directory flaskapp>
WSGIProcessGroup flaskapp
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
谁能让我知道我应该怎么做才能让flaskapp找到包裹在哪里?

最佳答案

您需要执行以下操作:

  • 安装 mod-wsgi对于 python3
  • sudo apt-get install libapache2-mod-wsgi-py3
    这将替换 libapache2-mod-wsgi 并重新启动 apache 服务。
  • 将您的 python 包路径添加到位于 /etc/apache2/sites-enabled/000-default.conf 的脚本中
  • WSGIPythonHome "/usr/local/bin"
    WSGIPythonPath "/home/ubuntu/.local/lib/python3.8/site-packages"

    <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

    WSGIDaemonProcess flaskapp threads=5 python-path=/var/www/html/flaskapp/flaskapp
    WSGIScriptAlias / /var/www/html/flaskapp/flaskapp.wsgi

    <Directory flaskapp>
    WSGIProcessGroup flaskapp
    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>

    关于python - 在 EC2 Ubuntu 上运行的 Flask 服务器找不到 python 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71151718/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com