Skip to content

How to manage shared code #1987

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
NVolcz opened this issue May 13, 2020 · 7 comments
Closed

How to manage shared code #1987

NVolcz opened this issue May 13, 2020 · 7 comments

Comments

@NVolcz
Copy link

NVolcz commented May 13, 2020

I have a bunch of Azure Python Function Apps that share some code and is not sure how I am sure how this is supposed work. The shared code is proprietary and cannot be uploaded to PyPi. My issue is similar to: Azure/azure-functions-python-worker#219

This is how I would like the folder structure to look like:

AzureFunctions
|_ FunctionApp1
    |_ requirements.txt
    |_ HttpTrigger1
        |_ __init__.py
        |_ function.json
|_FunctionApp2
    |_ requirements.txt
    |_ HttpTrigger1
        |_ __init__.py
        |_ function.json
|_ SharedCode
    |_ setup.py
    |_ requirements.txt
    |_ src
        |_ __init__.py
        |_ common1.py
        |_ common2.py
    |_ tests
        |_ test1.py
        |_ test2.py

I have tried to use the file: protocol in FunctionApp1 and FunctionApp2's requirements.txt:
file:../SharedCode
Most tooling understands this except for pip freeze. Skipping the file: part also seems works well with other tooling but neither does not work well with func. Remote build and --build-native-deps cannot build these function apps since they do not get access to the SharedCode folder, only the functions individual folders. I have therefor tried to build locally. It seems like the first problem with this lies with the packapp script: https://github.com/Azure/azure-functions-core-tools/blob/dev/tools/python/packapp/__main__.py#L73-L79
This SO thread seems related: https://stackoverflow.com/questions/11147667/is-there-a-way-to-list-pip-dependencies-requirements
pip download outputs this:

Processing <PATH>/AzureFunctions/SharedCode
    Link is a directory, ignoring download_dir

What should I do?

@zsolt-halo
Copy link

zsolt-halo commented May 14, 2020

Did you take a look at this?

It works for me. Not very clear as the comments say so but once you figure out the example it will work.

 __app__
 | - my_first_function
 | | - __init__.py
 | | - function.json
 | | - example.py
 | - my_second_function
 | | - __init__.py
 | | - function.json
 | - shared_code
 | | - my_first_helper_function.py
 | | - my_second_helper_function.py
 | - host.json
 | - requirements.txt
 | - Dockerfile
 tests

and the following imports will work from within any of the __init.py__ -s

from . import example #(explicit relative)  # importing from local folder
from ..shared_code import my_first_helper_function #(explicit relative) # 1 folder up
from __app__ import shared_code #(absolute) # using the special function app var __app__ which means the root of your project (the folder which contains your whole azure functions project)
import __app__.shared_code #(absolute) # same as above but exporting everything from shared_code

@NVolcz
Copy link
Author

NVolcz commented May 15, 2020

The difference between function apps and functions is confusing to say the least (and there are no such things on the other platforms). The above example "works" but is not ideal. We wanted separate function apps for separate managed identities. Also, how many functions can fit in a functions app? We currently have four but are planning to add many more and without the possibility to share code between functions apps all with end up in the same function app. In an ideal world I would not like to manage the function app at all.

@maximeLeurent
Copy link

Hello,

I faced the same problem of folder structure.
I managed to make it work by using simlink and -r inside requirements.txt

Here the Folder Structure I use. (Almost what you propose but without test in SharedCode, and setup.py)

AzureFunctions
|_ FunctionApp1
    |_ requirements.txt
    | SharedCode(simlink)
    |_ HttpTrigger1
        |_ __init__.py
        |_ function.json
|_FunctionApp2
    |_ requirements.txt
    | SharedCode(simlink)
    |_ HttpTrigger1
        |_ __init__.py
        |_ function.json
|_ SharedCode
    |_ requirements.txt
    |_ __init__.py
    |_ common1.py
    |_ common2.py

I created the simlink with :

cd FunctionApp1
ln -s ../SharedCode/ SharedCode

requirements.txt in FunctionApp1 look like

lib_for_FunctionApp1
-r ./SharedCode/requirements.txt

@apawast
Copy link

apawast commented Nov 9, 2021

@NVolcz are you still experiencing this issue?

@NVolcz
Copy link
Author

NVolcz commented Nov 9, 2021

We gave up on trying to use Python with Azure Functions after not receiving any help on this issue.
Have you changed anything?

@maximeLeurent
Copy link

maximeLeurent commented Nov 10, 2021

@NVolcz Did you see my answer?
Have you changed anything? => If the question was for me, no I didn't, I implement the way I described.

@apawast
Copy link

apawast commented Nov 12, 2021

Closing as duplicate of Azure/azure-functions-host#5373. The Python team is working on a new programming model.

@apawast apawast closed this as completed Nov 12, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Dec 12, 2021
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Projects
None yet
Development

No branches or pull requests

5 participants