How can I import modules for a Python Azure Function?

As of writing this, Python support for Azure Functions is experimental. So right now there is no way to directly get a module from a package manager to be installed on your instance. You'll need to bring your own modules with code. No modules are available by default on Azure Functions. You can add them by uploading it via the portal UX or kudu (which is handy for lots of files).

If you don't mind using virtualenv, there is an alternative.

  • Create your python script on Azure Functions.

  • Open a Kudu console and cd to your script location.

  • Create a virtualenv in this folder (python -m virtualenv myvenv)

  • Load this venv (cd myvenv/Scripts and call activate.bat). Now your shell should be prefixed by (myvenv).

  • Update pip (python -m pip install -U pip)

  • Install your dependencies using pip. (python -m pip install django)

Now wherever you need to import scripts, append the sys.path variable with this env. For example,

import sys, os.path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname( __file__ ), 'myvenv/Lib/site-packages')))