使用 Python 3.7+ 中的 wfastcgi 以及 Numpy、Pandas 等在 IIS 上部署 Python Flask 应用程序

2024-05-06

使用 wfastcgi 在 IIS 上部署 python 3.7+ Flask 或 Dash 应用程序时,有许多很棒的教程可以让 hello work 程序正常运行,例如

https://medium.com/@bilalbayasut/deploying-python-web-app-flask-in-windows-server-iis-using-fastcgi-6c1873ae0ad8 https://medium.com/@bilalbayasut/deploying-python-web-app-flask-in-windows-server-iis-using-fastcgi-6c1873ae0ad8

这个过程对我来说是崩溃的,对其他用户来说似乎是这样的

无法使用 Flask 服务器在 IIS 上运行 dash 应用程序 https://stackoverflow.com/questions/58980691/cant-get-dash-app-run-on-iis-with-flask-server

如何在IIS服务器上的conda基础环境中部署python Flask应用程序? https://stackoverflow.com/questions/59251820/how-to-deploy-python-flask-application-in-conda-base-environment-on-iis-server/59304736#59304736

那时其他软件包开始加入其中,例如 Numpy、Pandas、Dash、Plotly Express 等。

抛出的错误往往取决于包,但通常类似于一大块测试,看起来像下面的错误开头Error occurred while reading WSGI handler以及诸如 Dll 加载失败或缺少组件之类的问题。

Error occurred while reading WSGI handler: Traceback (most recent call last): File "C:\ProgramData\Anaconda3\envs\dash_search_app\lib\site-packages\wfastcgi.py", line 791, in main env, 
handler = read_wsgi_handler(response.physical_path) File "C:\ProgramData\Anaconda3\envs\dash_search_app\lib\site-packages\wfastcgi.py", line 633,
in read_wsgi_handler handler = get_wsgi_handler(os.getenv("WSGI_HANDLER")) File "C:\ProgramData\Anaconda3\envs\dash_search_app\lib\site-packages\wfastcgi.py", line 616,
in get_wsgi_handler raise ValueError('"%s" could not be imported%s' % (handler_name, last_tb)) ValueError: "dashapp.server" could not be imported: Traceback 
(most recent call last): File "C:\ProgramData\Anaconda3\envs\dash_search_app\lib\site-packages\wfastcgi.py", line 600, 
in get_wsgi_handler handler = __import__(module_name, fromlist=[name_list[0][0]]) File ".\dashapp.py", line 3, in from flaskapp import app File ".\flaskapp\__init__.py", 
line 67, in register_dashapps(app) File ".\flaskapp\__init__.py", line 19, in register_dashapps from flaskapp.dashapp_searchtool.layout import layout File ".\flaskapp\dashapp_searchtool\layout.py", line 30,
 in import pandas as pd File "C:\ProgramData\Anaconda3\envs\dash_search_app\lib\site-packages\pandas\__init__.py", line 17, in "Unable to import required dependencies:\n" + "\n".join(missing_dependencies)
 ImportError: Unable to import required dependencies: numpy: IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE! Importing the numpy c-extensions failed. - 
 Try uninstalling and reinstalling numpy. - If you have already done that, then: 1. Check that you expected to use Python3.7 from "C:\ProgramData\Anaconda3\envs\dash_search_app\python.exe", 
 and that you have no directories in your PATH or PYTHONPATH that can interfere with the Python and numpy version "1.17.3" you're trying to use. 2. If (1) looks fine,
 you can open a new issue at https://github.com/numpy/numpy/issues. Please include details on: - how you installed Python 
 - how you installed numpy - your operating system - whether or not you have multiple versions of Python installed - if you built from source, your compiler versions and 
 ideally a build log - If you're working with a numpy git repository, try `git clean -xdf` (removes all files not under version control) and rebuild numpy. Note: 
 this error has many possible causes, so please don't comment on an existing issue about this - open a new one instead. Original error was: DLL load failed: 
 The specified module could not be found. StdOut: StdErr: C:\ProgramData\Anaconda3\envs\dash_search_app\lib\site-packages\flask_sqlalchemy\__init__.py:794: 
 FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.

最常见的建议是降级到 python 3.6,但这并不是每个项目都可行。

我已经在下面回答了我的解决方案,以防其他人可能遇到同样的问题,因为我几个小时的谷歌搜索并没有让我找到解决方案。


对我来说,这归结为权限问题。我的第一个猜测是环境没有正确注册,但实际上是 IIS 无权访问环境的所有依赖项。

我解决此问题的方法是导航到我的 Anaconda3 文件夹,右键单击并选择属性。在属性窗口中,我导航到安全选项卡并单击编辑

然后我单击“添加”来添加用户并打开以下窗口

在“对象类型”下,我选择了所有选项,在“位置”下,我选择了顶级位置。

在对象名称下我写了 iis apppool 例如,对于名为 app 的 IIS 站点,它将是 iis apppool\app

然后,我单击“检查名称”以确保可以找到该应用程序,然后继续单击“确定”并应用。

添加权限后,IIS 站点可以访问所有必需的 dll 等,并且应用程序可以正常运行

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Python 3.7+ 中的 wfastcgi 以及 Numpy、Pandas 等在 IIS 上部署 Python Flask 应用程序 的相关文章

随机推荐