-
Notifications
You must be signed in to change notification settings - Fork 100
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
Error importing Shared Code into Python function #219
Comments
You don't need any hacks. Import adjacent modules using the relative import syntax: from . import SharedCode @asavaritayal Perhaps we should document this in a conspicuous fashion, since this is not the first time this issue has come up: #81 |
@elprans Not working for my case.
I would like to import a submodule using the following or similar syntax Folder structure looks like the following
|
@wbreza In your case your shared module code is one level up, so the correct syntax would be from .. import SharedCode |
To import submodules directly: from ..SharedCode import module1 |
@elprans I changed all of our imports to relative imports like you indicated above:
I get the following error:
This does not occur when using the hack described above:
Just for reference, this is running on a clean
|
@wbreza Change |
@elprans - Thanks - this does work but based on Python best practices that I have read relative imports are frowned upon. Some pros/cons and discussion @ https://realpython.com/absolute-vs-relative-python-imports/ I am not a python expert myself but in refactoring our project to support relative import it caused many downstream issue with how pytest and other modules are invoked. Any idea how to make this process easier by allowing Absolute imports? |
@wbreza If you have a lot of shared code, the best approach would be to put it into a standalone pip-installable package and add that as a dependency of your function app. This way you would be able to perform absolute imports. |
@asavaritayal is there a plan to record these best practice recommendations in the developer guide? Especially since these are idiomatic to Python. |
@elprans - Take your point, but keep in mind not all consumers of this will be able to push their IP to pypi unless they could host it privately. We may unfortunately actually just stick with our path workaround mentioned above. |
@wbreza We can add support for building dependencies from local sources when publishing. Pushing to PyPI should not be necessary, all you'll need to do is put a path in
or, if you have pre-built wheels:
We chose against adding the function app folder to |
Yes, we should add this to the developer guide. @elprans can you update the wiki? |
@asavaritayal Done. |
@elprans, @brettcannon ran into the same issue yesterday. Is there a way we can work around the relative import syntax for shared modules? |
@asavaritayal, @brettcannon What exactly is wrong with relative imports? |
@elprans it leads to an odd project layout. For instance, my project is now laid out as:
I'm working entirely within the pvscbot directory. So I have I also don't know how I want to check this into GitHub. I.e. do I check in |
OK, but the function app directory layout isn't really dictated by the worker, it's how host/CLI expect it to be. The issue at hand is how you address the Python modules that comprise your function app (which is currently to import them using the relative import syntax). Am I missing something? |
Basically if the docs get updated to say "put everything in a single package" then that's fine but then I want to move
https://github.com/Azure/azure-functions-python-worker/wiki/Developer-Guide#folder-structure suggests that currently isn't possible. |
I think that's reasonable. But then again, it's not up to the worker how the code is laid out. The host dictates the path to the function directory, and that's it. |
To clarify, we currently employ this importlib hackery to make even the relative imports work properly: azure-functions-python-worker/azure/functions_worker/loader.py Lines 23 to 29 in 5ce1d21
Again, this is because we can't really rely on the layout or the setting of |
@elprans thanks for the details! @asavaritayal not sure if these layout requirements can change, but that's my feedback about where you are currently asked to place files. |
The following issue has the approach that we'll be using for handling absolute imports with our current layout: #278. Docs on this coming shortly. Closing this as they have significant overlap. |
I still don't get it. I followed the instructions in the documentation page (matching the quoted comment here). I get this in pylint and at runtime:
Clearly, the documentation is wrong. What is the right way to share code between multiple functions in the same app? Can someone please fix the documentation? |
Hi @polarapfel, could you create a separate issue? We can follow up there with further details. |
@maiqbal11 I logged the issue here MicrosoftDocs/azure-docs#30588 |
I was having the same issue as the original poster, and was able to get things working by adding the sys.path.append workaround. However we have found that fails occasionally when deployed with an error of unable to import module. So I tried the method suggested by @elprans:
That works but now my tests are failing. So, my question is now, what is the recommended folder structure for unittests? The folder structure in the python function developers guide doesn't talk about a test folder. |
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
Usually I'd have done That does not work.
Can anyone help |
@anindya5 which module in your example file hierarchy are you trying to do the import from? Also note you don't have an |
I had the same issue with needing absolute imports. sys.path.append(os.path.abspath("")) > did not work |
Same thing is happening to me now. I had a function that was deployed and working fine. Now the same code stop working with this error. I didn't change or deploy anything. I had to add the path append like @jaymegordo. sys.path.append('/home/site/wwwroot') |
Codes are not finding or navigating to proper files suddenly everywhere
On Mon, Aug 12, 2019 at 7:22 AM Brad Bitterman ***@***.***> wrote:
Same thing is happening to me now. I had a function that was deployed and
working fine. Now the same code stop working with this error. I didn't
change or deploy anything. I had to add the path append like @jaymegordo
<https://github.com/jaymegordo>.
sys.path.append('/home/site/wwwroot')
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#219?email_source=notifications&email_token=ACQLYQHHQMHA7I7LRP2JDM3QEFIXBA5CNFSM4FZXKBMKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD4CLLMI#issuecomment-520402353>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ACQLYQBQPUGUFIK4K3VYR2LQEFIXBANCNFSM4FZXKBMA>
.
--
Anindya Sankar Dey
|
@jaymegordo, @bhbitter, @anindya5, it would be best if you are able to open separate issues with details/repro of the exact issue you are facing. There are different flavors of import issues that can happen and having separate issues with details will help us determine if there is some common cause and help keep our responses as specific as possible. Thanks! |
facing same issue... all imports working with SharedCode.MODULE 2 weeks ago, now it's not working with error "Module not Found" |
@elprans I am having similar problems, described in detail in a new issue: #577. My function was working in my local environment on Oct 31, but is not working since upgrading VSC and Azure functions core tools to the latest versions in the last few days. I have tried changing |
A better workaround is to add to function's
With doing that absolute imports can be used |
@oleksandr-yatsuk THANK YOU for that My imports randomly stopped working a day ago and using that syntax instead works! now: |
@oleksandr-yatsuk Good suggestion. I had to create that for each of my functions, but I like having it there, rather than in the import section in each code file. |
Just put your modules in the following directory and import them as usually:
|
Hi guys, Description |
How are non-Python code such as Outside of Azure Functions, I simply put the Since they are not Thank you |
Disregard. Just had to include the folder path of the Function. |
Hack required to import Shared Code modules into Python functions
Investigative information
Repro steps
SharedCode
adjacent to your function foldersSharedCode
folderSharedCode
modules into existing Python Azure FunctionExpected behavior
Module should be available by default because it is inside the same virtual environment without requiring any PYTHONPATH mods.
Actual behavior
Error:
Known workarounds
Add the following lines to the top of your
__init__.py
in your Azure Function. This will allow any modules defined in your FunctionApp to be available for importThe text was updated successfully, but these errors were encountered: