Microsoft Azure Open Source Development Support Team Blog

Support for Open Source Technologies on Microsoft Azure App Service

Install Python Modules on Azure App Services

Installing Python packages in Azure App Services is little tricky using pip. In this blog, I would provide best practice to do that.

Pip Install on Azure App Services might fail because

  • It may simply be that the package is not available on the Python Package Index.
  • It could be that a compiler is missing (Azure App Service is a sandbox environment and does not have all the modules/compilers installed inside it).

Below is one such common scenario

building 'Crypto.Random.OSRNG.winrandom' extension  
warning: GMP or MPIR library not found; Not building Crypto.PublicKey._fastmath.  
error: Microsoft Visual C++ 9.0 is required (Unable to find vcvarsall.bat). Get it from https://aka.ms/vcpython27  

I would recommend to use wheels for installing Python dependencies. Many of the popular modules already provide wheel files listed in PyPI.

Let’s say if I want to install below list of Python modules

django<2  
pycrypto==2.6.1  
pandas==0.18.1  
numpy==1.11.1  

Creating Wheel Files in Local Environment

I’m using Python 3.5.2 x64 for this blog. So i will create wheel files for Python35 x64.

Note : You don’t have to create wheels for all the Python modules. Do this only for modules which have trouble installing in Azure App Services

  • I have python35 x64 installed in my local environment @ C:\Python35 folder
  • Create a requirements.txt file and add above modules inside it.
  • Install wheel module using below command (we would use this module to create wheels later)
> C:\Python35\python.exe -m pip install wheel
  • Use Below Command to create wheel files inside wheelhouse folder in Local environment
> C:\Python35\python.exe -m pip wheel -r requirements.txt -w wheelhouse

Above step would have created a wheelhouse folder with wheel files for modules listed in requirements.txt file (In my cases it created wheel files for Python35 arch as you can see in file names)
Creating Wheel Files

Install requirements on Azure App Services

  • While moving your code to Azure App services make sure to include below line of code as first line in requirements.txt file
--find-links wheelhouse

find links in requirements.txt

Install Manually using Kudu
> D:\home\Python35\python.exe -m pip install --upgrade -r requirements.txt
  • After running above command, all the Python modules listed in requirements.txt file would be installed @ D:\home\Python35\Lib\ folder

You might have different path for python.exe depending on python version you are using.

Install using Deployment Script

If you are using Deployment script, Include below line of code in deploy.cmd

:: 2. Install packages
echo Pip install requirements.  
D:\home\Python35\python.exe -m pip install --upgrade -r requirements.txt  
IF !ERRORLEVEL! NEQ 0 goto error  

For more details refer Django app with HttpPlatformHandler in Azure App Services – Windows

Install modules in non-default folder

As I have mentioned earlier, running above command would install modules
@ D:\home\Python35\Lib\ folder.

  • Use -t option to install dependencies in non-default folder
D:\home\Python35\python.exe -m pip install --upgrade -r requirements.txt -t D:\home\site\wwwroot\pymodules  

Above Command would install Python modules @ D:\home\site\wwwroot\pymodules folder

  • Set PYTHONPATH in App settings at Azure portal for web app

I have seen few wheel files/Python modules having trouble installing with older version of Pip/Python. Try with newer version’s of Python/pip if you have any issues.