How to add Python package to Azure Function? - azure

I am new to Azure, how do I get to install a Python package for an App Function? I followed the steps to add the required package into the requirement.txt file in the wwwroot folder. However this does not work. I also tried installing the package directly in the code (init.py )but that doesn't work wither.
Also, I cannot access the Kudu console.
How do install the required python packages I need for my code?

Now Ideally, we add the requirements.txt with the source code files and deploy them together as project and during the deployment process all the dependencies will be installed.
Here the file structure should look like this :-
where the requirments.txt is in the root directory of your project. Also, here I am installing flask and django along with this function. Now after we deploy this function, the packages will appear in the app service.
Now my http trigger function is like this where it basically returns the version of the flask package
import azure.functions as func
from importlib.metadata import version
def main(req: func.HttpRequest) -> func.HttpResponse:
return func.HttpResponse(f"Hello, {version('flask')}. This HTTP triggered function executed successfully.")
Here I have deployed the function using the VScode extensions.
The other option would be to use the Console from the portal

Related

How can I set up my imports in order to run my python application without installing it, and still be able to run tox using poetry?

I have a python 3.6 code-base which needs to be installed in the environment's site-packages directory in order to be called and used. After moving it to docker, I decided that I should set up a shared volume between the docker container and the host machine in order to avoid copying and installing the code on the container and having to rebuild every time I made a change to the code and wanted to run it. In order to achieve this, I had to change a lot of the import statements from relative to absolute. Here is the structure of the application:
-root
-src
-app
-test
In order to run the application from the root directory without installing it, I had to change a lot of the import statements from
from app import something
to:
import src.app.something
The problem is that I use poetry to build the app on an azure build agent, and tox to run the tests. The relevant part of my pyproject.toml file looks like this:
[tool.poetry]
name = "app"
version = "0.1.0"
packages = [{include = 'app', from='src'}]
The relevant part of my tox.ini file looks like this:
[tox]
envlist = py36, bandit, black, flake8, safety
isolated_build = True
[testenv:py36]
deps =
pytest
pytest-cov
pytest-env
pytest-mock
fakeredis
commands =
pytest {posargs} -m "not external_service_required" --junitxml=junit_coverage.xml --cov report=html --cov-report=xml:coverage.xml
I'm not an expert in tox or poetry, but from what I could tell, the problem was that the src directory wasn't being included in the build artifact, only the inner app directory was, so I added a parent directory and changed the directory structure to this:
-root
-app
-src
-app
-test
And then changed the poetry configuration to the following in order to include the src directory
[tool.poetry]
name = "app"
version = "0.1.0"
packages = [{include = 'src', from='app'}]
Now when I change the imports in the tests from this:
from app import something
to this:
from app.src.app import something
The import is recognized in Pycharm, but when I try to run tox -r, the I get the following error:
E ModuleNotFoundError: No module named 'app'
I don't understand how tox installs the application, and what kind of package structure I need to specify in order to be able to call the code both from the code-base directory and from site packages. I looked at some example projects, and noticed that they don't use the isolated_build flag, but rather the skip_dist flag, but somehow they also install the application in site packages before running their tests.
Any help would be much appreciated.
Specs:
poetry version: 1.1.6
python version:3.6.9
tox version:3.7
environment: azure windows build agent
You have to change the imports back to from app import something, the src part is, with respect to the code as a deliverable, completely transient. Same goes for adding in another app directory, your initial project structure was fine.
You were right about going from relative imports to absolute ones though, so all that is necessary thereafter is telling your python runtime within the container that root/src should be part of the PYTHONPATH:
export PYTHONPATH="{PYTHONPATH}:/path/to/app/src"
Alternatively, you can also update the path within your python code right before importing your package:
import sys
sys.path.append("/path/to/root/src")
import app # can be found now
Just to state the obvious, meddling with the interpreter in this way is a bit hacky, but as far as I'm aware it should work without any issues.

Overriding package libraries in Google App Engine project

I am writing a Google App Engine Django REST Framework project that uses external libraries through requirements.txt.
In one of the of files in a module installed in requirements.txt, I am manually editing some code there. How do I get GAP to use this modified version instead of original one.
The way I am doing this is installing the packages in a folder called lib, modifying the package inside it and then creating a file called appengine_config.py which contains this:
from google.appengine.ext import vendor
vendor.add('lib')
But when I deploy it, it still uses the original package in requirements.txt. Any idea how to make this work?
GAE will use requirements.txt and install those libraries in the lib folder when you deploy. That is just how it works.
Nothing prevents you from deploying code outside the lib folder. You can structure your project like this:
GAE_folder:
-- app.yaml
-- requirements.txt
-- lib
-- my_app
-- my_custom_lib

How to give different pip for google appengine to use while installing during gcloud app deploy python 3?

I want to deploy a flask application on Google standard appengine in python 3.7 runtime . I have a private dependency and want to connect to my personal feed for that. How to i ensure that the pip that GAE use is connected to my feed.
I checked the official docs of gcloud and found -
Dependencies
During deployment, App Engine uses the Python package manager pip to install dependencies defined in the requirements.txt metadata file located in your project's root directory. You do not need to upload dependencies as App Engine performs a fresh install.
Dependency specification using the Pipfile/Pipfile.lock standard is currently not supported and your project must not have these files present. in https://cloud.google.com/appengine/docs/standard/python3/runtime
If you are using App Engine Standard, you will not be able to do these kinds of specifications. My advice if you need to specify the pip for App Engine would be to use App Engine Flex in Custom Runtime and specify this on the Dockerfile.
You can find more information about this in the documentation for App Engine Custom Runtime and the Setup section section.
Hope you find this useful!

How to include dependencies for TypeScript Azure Function

I've exhausted the Azure Function documentation and cannot figure out how to deploy a TypeScript Azure Function and include the Node.js dependencies. I'm using VSCode Azure Functions tool to deploy my serverless code on a Linux server.
I cannot use the Kudu service as it is only available for Windows Azure Functions
I've tried including a package.json in both the root directory of my Azure Function app and the literal function folder itself.
Any pointers would be much appreciated!
Here is few things.
Create project using func tools func init --worker-runtime typescript
Install typings module #azure/functions - if not installed
Build project npm run build:production
If you want you can even publish using func azure functionapp publish <APP_NAME>
To use types
import { AzureFunction, Context, HttpRequest } from "#azure/functions"
You can check documentation

How to package python3 modules for google app engine

I'm trying to figure out how to add a internal package to a Google App Engine deployment with Python 3 (standard).
For Python 2 the way to package modules was using a local lib/ folder and appengine_config.py. This seems not to work anymore for Python 3? At least my app cannot find modules which are in the lib/ folder.
For Python 3 it's possible to just pip3 install -t . the package. But this gets really messy as all packages are just installed in the app root and will also be added to the git repository of our app.
We cannot use requirements.txt as the module is internal and will not be available on PyPI.
Is there another way to package modules for Google App Engine using Python 3?
The Python 3.7 Standard runtime is an "idiomatic" Python runtime, which means it doesn't automatically add the special lib directory to the search path for modules.
You should continue "vendoring" your private modules into a lib directory, but you'll need to make a change to how you import them.
If your private package is foobar, and you've done pip install -t lib foobar, then in your project, instead of:
import foobar
you do:
import lib.foobar
(You'll also need to add an empty __init__.py file to your lib directory, to make it a module.)

Resources