Skip to content

Referencing modules #81

New issue

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

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

Already on GitHub? Sign in to your account

Closed
asavaritayal opened this issue Mar 14, 2018 · 9 comments
Closed

Referencing modules #81

asavaritayal opened this issue Mar 14, 2018 · 9 comments

Comments

@asavaritayal
Copy link
Contributor

asavaritayal commented Mar 14, 2018

Currently, any file / module references in main.py have to be with respect to the top level function app.

For e.g. -

FunctionApp
    |__ HttpTrigger1
             |__ function.json
             |__ main.py
             |__ model.pkl
    |__ host.json
    |__ requirements.txt
# main.py
import os
import azure.functions

def main(req: azure.functions.HttpRequest):

    # file reference wrt top level function app
    f = open('HttpTrigger1/model.pkl', 'rb')
   

Can we have relative paths instead? E.g. - f = open('model.pkl', 'rb')

@elprans
Copy link
Collaborator

elprans commented Mar 14, 2018

The issue is that the current working directory is a global per-process setting. It's impossible to do a chdir without affecting other functions that may be executing concurrently.

@elprans
Copy link
Collaborator

elprans commented Mar 14, 2018

The recommended way to reference local files in Python in general is to use __file__:

import pathlib

open(pathlib.Path(__file__).parent / 'model.pkl', 'rb')

@asavaritayal
Copy link
Contributor Author

Okay. Do you recommend the same for importing modules as well?

Here's the issue I'm running into:

FunctionApp
    |__ HttpTrigger1
             |__ function.json
             |__ main.py
             |__ example.py
# main.py 

import example #fails
import HttpTrigger/example #also fails

@elprans
Copy link
Collaborator

elprans commented Mar 14, 2018

Modules should be imported using the relative import syntax:

from . import example

@asavaritayal
Copy link
Contributor Author

That works - thanks!

@paolosalvatori
Copy link

paolosalvatori commented May 22, 2018

What about if the function uses packages like helpers in the sample below?

FunctionApp
    |__ HttpTrigger1
             |__ function.json
             |__ main.py
             |__ example.py
             |__ helpers
                     |__ jwt
                             |__ __init__.py

It looks like placing the helpers package in the same folder as the python function is not enough. As a workaround, we had to explicitly add the current folder to the path as follows:

from sys import path
from os.path import dirname, join
path.append(dirname(__file__))

import json
from helpers.jwt import JWT
...

@elprans
Copy link
Collaborator

elprans commented May 22, 2018

Use from .helpers import jwt

@paolosalvatori
Copy link

Thanks @elprans

@anindya5
Copy link

Hi,

My issue is if I want to import a function say function1 in module1 of SharedCode, but for me the SharedCode is inside the API folder, like below

FunctionApp
|_ HttpTrigger1
     |_ __init__.py
     |_ function.json
     |_ SharedCode
        |_ module1.py
        |_ module2.py
|_ HttpTrigger2
     |_ __init__.py
     |_ function.json

The functions are defined inside module1 and module2

Usually I'd have done
from ShareCode.module1 import function1

That does not work.

from .ShareCode.module1 import function1 also gives module not found error, shown below

Executed 'Functions.HtmBatchAzureFunc' (Failed, Id=d70ee3dc-dca2-4d89-b39c-5c902ed64b11)
[5/22/19 8:54:05 PM] System.Private.CoreLib: Exception while executing function: Functions.HtmBatchAzureFunc. System.Private.CoreLib: Result: Failure
[5/22/19 8:54:05 PM] Exception: ModuleNotFoundError: No module named 'htm_shared_src'
[5/22/19 8:54:05 PM] Stack: File "/usr/local/Cellar/azure-functions-core-tools/2.7.1158/workers/python/deps/azure/functions_worker/dispatcher.py", line 229, in _handle__function_load_request

Can anyone help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants