在 Azure WebApps 上,Python 默认安装在路径 D:\Python27\ 上,除了路径 D:\home\ 之外,用户无权执行任何写入操作,例如命令 pip install <packages> 将 Python 包安装到 libs。
所以首先你需要通过 Kudu 站点扩展在路径D:\home 安装一个新的 Python 运行时,如下图。
然后就可以看到D:\home下的Python目录,你有写操作权限。
如需安装所需的 Python 包,请执行以下命令安装 pip 工具。
D:\home> cd Python27
D:\home\Python27> curl -o get-pip.py https://bootstrap.pypa.io/get-pip.py
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1558k 100 1558k 0 0 5069k 0 --:--:-- --:--:-- --:--:-- 6546k
D:\home\Python27> python get-pip.py
Requirement already up-to-date: pip in d:\home\python27\lib\site-packages
Collecting wheel
Downloading wheel-0.30.0-py2.py3-none-any.whl (49kB)
Installing collected packages: wheel
Successfully installed wheel-0.30.0
接下来,你可以通过python -m pip install <package-name>安装这些包,比如python -m pip install django==1.11.5如下。
D:\home\Python27> python -m pip install django==1.11.5
Collecting django==1.11.5
Downloading Django-1.11.5-py2.py3-none-any.whl (6.9MB)
Collecting pytz (from django==1.11.5)
Downloading pytz-2017.2-py2.py3-none-any.whl (484kB)
Installing collected packages: pytz, django
正如官方文档所说,对于Troubleshooting - Package Installation,如下,对于包Pillow,需要编译C代码。
疑难解答 - 软件包安装
在 Azure 上运行时,某些包可能无法使用 pip 安装。可能只是因为该包在 Python 包索引中不可用。可能需要编译器(在 Azure 应用服务中运行 Web 应用的计算机上没有编译器)。
您需要在 Kudu CMD 上通过命令curl -o <wheel-file-name> <wheel-file-url> 从here 下载包轮文件,并通过命令python -m pip install <wheel-file-name> 安装它们。
安装完所有包后,你可以将你的django webapp上传到D:\home\site\wwwroot,这个路径下的文件结构看起来像官方的sample,包含了PTVS在VS 2017上创建的app、<your-django-project-name>这些目录。
最后,请配置您的web.config 文件以使您的应用正常运行。
<configuration>
<appSettings>
<add key="WSGI_HANDLER" value="<your-django-project-name>.wsgi.application"/>
<add key="PYTHONPATH" value="D:\home\site\wwwroot"/>
<add key="WSGI_LOG" value="D:\home\LogFiles\wfastcgi.log"/>
</appSettings>
<system.webServer>
<handlers>
<add name="PythonHandler" path="handler.fcgi" verb="*" modules="FastCgiModule" scriptProcessor="D:\home\Python27\python.exe|D:\home\Python27\wfastcgi.py" resourceType="Unspecified" requireAccess="Script"/>
</handlers>
<rewrite>
<rules>
<rule name="Static Files" stopProcessing="true">
<conditions>
<add input="true" pattern="false" />
</conditions>
</rule>
<rule name="Configure Python" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{REQUEST_URI}" pattern="^/static/.*" ignoreCase="true" negate="true" />
</conditions>
<action type="Rewrite" url="handler.fcgi/{R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
希望对您有所帮助。有任何问题,请随时告诉我。