2

I've created an Azure function and want to be able to use various packages in the Python code for the function; take Numpy, for example. Obviously, the code is not going to run from my local machine once it's published to Azure. This means I can't install Numpy to whatever infrastructure it runs on, so I can't import Numpy in my code. How can I use a package like Numpy in the code?

2 Answers 2

9

From the official doc:

Dependencies are obtained remotely based on the contents of the requirements.txt file

All you need to do is create that file and upload it together with your Function code

https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-python#python-version-and-package-management

Sign up to request clarification or add additional context in comments.

Comments

-1

Install python packages from the python code itself with the following snippet: (Tried and verified on Azure functions)

def install(package):
    # This function will install a package if it is not present
    from importlib import import_module
    try:
        import_module(package)
    except:
        from sys import executable as se
        from subprocess import check_call
        check_call([se,'-m','pip','-q','install',package])


for package in ['numpy','pandas']:
    install(package)

Desired libraries mentioned the list gets installed when the azure function is triggered for the first time. for the subsequent triggers, you can comment/ remove the installation code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.