如何 'pip install packages' 在 Azure WebJob 中解决包兼容性问题
How to 'pip install packages' inside Azure WebJob to resolve package compatibility issues
我正在使用 Google 地图 API 和 Azure SQL 存储在 Azure Web 应用程序中部署 WebJob。
我遵循的是创建 WebJob 目录并将我的 'site-packages' 文件夹复制到 WebJob 根文件夹中的典型方法。然后我还在 'site-packages' 中添加我的代码文件夹,并在根目录中创建一个 run.py 文件,如下所示:
import sys, os
sys.path.append(os.path.join(os.getcwd(), "site-packages"))
import aero2.AzureRoutine as aero2
aero2.run()
现在代码可以在 Azure 中正确运行。但是我在一些命令后看到了警告,这会减慢我的代码速度。
我尝试将 'pyopenSSL' 和 'requests' 模块复制到我的站点包文件夹中,但错误仍然存在。
但是,代码在我的本地机器上运行完美。
如何找到与 Azure 上的 python 运行 兼容的 'pyopenSSL' 或 'requests'?
或者
我如何修改我的代码,以便它在 Azure 上 pip 安装 python 运行 的相关包?
或者更重要的是,
我该如何解决这个错误?
根据警告消息中引用的文档 https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning:
Certain Python platforms (specifically, versions of Python earlier than 2.7.9) have restrictions in their ssl module that limit the configuration that urllib3 can apply. In particular, this can cause HTTPS requests that would succeed on more featureful platforms to fail, and can cause certain security features to be unavailable.
因此修复此警告的最简单方法是升级 python 版本的 Azure Web 应用程序。登录 Azure 管理器门户,在 Application settings 列中将 python version 更改为 3.4:
当我在 webjob 任务中测试使用 requests
模块来请求 "https://" url,并且自从将 python 版本升级到 3.4 后,不再有警告.
@萨阿德,
如果你的 webjob 在 Azure Web App 上运行良好,但是你得到了 inscuritywaring,我建议你可以尝试通过这个配置(https://urllib3.readthedocs.org/en/latest/security.html#disabling-warnings)禁用警告信息。
同时,requests lib和高版本有一些不同,推荐大家参考这个文档:
http://fossies.org/diffs/requests/2.5.3_vs_2.6.0/requests/packages/urllib3/util/ssl_.py-diff.html
Azure web app 使用的是 Python 2.7.8 版本,低于 2.7.9。所以你可以下载 2.5.3 版本的请求库
我遵循了 this article 并且 'pip installed' 我的脚本使用了 pymongo 库。不确定它是否适合您,但步骤如下:
- 确保在 requirements.txt
中包含库名称和版本
- 使用 Git 部署网络应用程序。该目录应至少包括
requirements.txt
(只在虚拟环境中安装requirements.txt里面的东西,与D:\home\site\wwwroot\env\Lib\site-packages中的Web App共享)
- 将此代码块添加到您要在 WebJob zip 文件中使用的 Python 代码。
import sys
sitepackage = "D:\home\site\wwwroot\env\Lib\site-packages"
sys.path.append(sitepackage)
我正在使用 Google 地图 API 和 Azure SQL 存储在 Azure Web 应用程序中部署 WebJob。
我遵循的是创建 WebJob 目录并将我的 'site-packages' 文件夹复制到 WebJob 根文件夹中的典型方法。然后我还在 'site-packages' 中添加我的代码文件夹,并在根目录中创建一个 run.py 文件,如下所示:
import sys, os
sys.path.append(os.path.join(os.getcwd(), "site-packages"))
import aero2.AzureRoutine as aero2
aero2.run()
现在代码可以在 Azure 中正确运行。但是我在一些命令后看到了警告,这会减慢我的代码速度。
但是,代码在我的本地机器上运行完美。
如何找到与 Azure 上的 python 运行 兼容的 'pyopenSSL' 或 'requests'?
或者
我如何修改我的代码,以便它在 Azure 上 pip 安装 python 运行 的相关包?
或者更重要的是,
我该如何解决这个错误?
根据警告消息中引用的文档 https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning:
Certain Python platforms (specifically, versions of Python earlier than 2.7.9) have restrictions in their ssl module that limit the configuration that urllib3 can apply. In particular, this can cause HTTPS requests that would succeed on more featureful platforms to fail, and can cause certain security features to be unavailable.
因此修复此警告的最简单方法是升级 python 版本的 Azure Web 应用程序。登录 Azure 管理器门户,在 Application settings 列中将 python version 更改为 3.4:
当我在 webjob 任务中测试使用 requests
模块来请求 "https://" url,并且自从将 python 版本升级到 3.4 后,不再有警告.
@萨阿德, 如果你的 webjob 在 Azure Web App 上运行良好,但是你得到了 inscuritywaring,我建议你可以尝试通过这个配置(https://urllib3.readthedocs.org/en/latest/security.html#disabling-warnings)禁用警告信息。 同时,requests lib和高版本有一些不同,推荐大家参考这个文档: http://fossies.org/diffs/requests/2.5.3_vs_2.6.0/requests/packages/urllib3/util/ssl_.py-diff.html Azure web app 使用的是 Python 2.7.8 版本,低于 2.7.9。所以你可以下载 2.5.3 版本的请求库
我遵循了 this article 并且 'pip installed' 我的脚本使用了 pymongo 库。不确定它是否适合您,但步骤如下:
- 确保在 requirements.txt 中包含库名称和版本
- 使用 Git 部署网络应用程序。该目录应至少包括
requirements.txt
(只在虚拟环境中安装requirements.txt里面的东西,与D:\home\site\wwwroot\env\Lib\site-packages中的Web App共享) - 将此代码块添加到您要在 WebJob zip 文件中使用的 Python 代码。
import sys
sitepackage = "D:\home\site\wwwroot\env\Lib\site-packages"
sys.path.append(sitepackage)