Skip to content
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

Append /home/site/wwwroot to sys.path #726

Merged
merged 3 commits into from
Aug 26, 2020
Merged

Conversation

Hazhzeng
Copy link
Contributor

@Hazhzeng Hazhzeng commented Jul 16, 2020

Content In This PR

Create a unittest friendly folder structure: #469

  1. Append AzureWebJobsScriptRoot to sys.path in Core-Tools and VSCode environment
  2. Append function_app_directory to sys.path when doing specialization in Linux Consumption
  3. (No action on Linux Dedicated / Premium since /home/site/wwwroot is already in sys.path)
  4. Update unittests
    • Rename brokenimplicit to implicit_import since we now support import a as b statement
    • Add a case for testing name collision with implicit import name_collision, the worker should pick third-party package.
    • Add a case for testing name collision with __app__ import name_collision_app_import, the worker should pick the package from customer's code.
    • Add a case for testing third-party package import absolute_thirdparty
    • Add a case for importing non-existing module module_not_found

Description

The current Python function app folder structure suggested in https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-python#folder-structure is confusing:

  1. It introduces a virtual namespace __app__ which is not a Python standard. This makes Azure Functions Python code less portable.
  2. It gives our customer a hard time adding unit tests in Azure Functions.
  3. Customer are not able to treat their function triggers as Python module, using absolute import does not work.
from SharedCode import module  # Not working because function project root is not in sys.path

We want to enable unit tests and absolute import to our customers, and make our customers code more portable.

Fix sys.path

We found out the sys.path is inconsistent across Linux Consumption, Linux Dedicated/Premium, and Local Development environment. After we discussed with Brett and Steve, we decide to align these environments by introducing the project root into sys.path.

Linux Consumption:

sys.path = [
    "/tmp/functions\\standby\\wwwroot",
    "/home/site/wwwroot/.python_packages/lib/site-packages",
    "/azure-functions-host/workers/python/3.7/LINUX/X64"
]
# This PR appends /home/site/wwwroot during worker specialization
# in _handle__function_environment_reload_request,
# to avoid breaking existing customer apps.

Linux Dedicated/Premium:

sys.path = [
    "/home/site/wwwroot",
    "/home/site/wwwroot/.python_packages/lib/site-packages",
    "/azure-functions-host/workers/python/3.7/LINUX/X64"
# no action required since /home/site/wwwroot is already there.
]

Local Development using Core Tools and VSCode:

sys.path = [
    "C:\\Users\\username\\WorkerPath\\python\\3.7\\WINDOWS\\X64",
    "C:\\Users\\username\\ProjectPath\\.venv\\lib\\site-packages"
# append /home/site/wwwroot when the worker starts in worker.py, since customer code already exists and no specialization is required.
]

Message to customer

  1. Customer should not name function trigger with a third-party package name (i.e. Don't name an http trigger numpy if you're using numpy package).
  2. We will continue support __app__ import.
  3. To do absolute import from SharedCode, you need to add an __init__.py file into the SharedCode folder to make it a Python package, and feel free to use import SharedCode or from SharedCode import module in your function app.
  4. To run unit test, create a test folder along with other triggers. If __init__.py file does not exist in your function trigger, add one into the folder to make it a Python package, and you can test and treat this function trigger as a normal function package.

Attachment

NextSuggestedFolderStructure.zip


PR information

  • The title of the PR is clear and informative.
  • There are a small number of commits, each of which has an informative message. This means that previously merged commits do not appear in the history of the PR. For information on cleaning up the commits in your pull request, see this page.
  • If applicable, the PR references the bug/issue that it fixes in the description.
  • New Unit tests were added for the changes made and CI is passing.

Quality of Code and Contribution Guidelines

@Hazhzeng Hazhzeng changed the title [WIP] Add /home/site/wwwroot into sys.path Append /home/site/wwwroot to sys.path Jul 22, 2020
Copy link
Member

@vrdmr vrdmr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@Hazhzeng Hazhzeng merged commit ff38583 into dev Aug 26, 2020
@Hazhzeng Hazhzeng deleted the hazeng/folder-structure branch August 26, 2020 00:31
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

Successfully merging this pull request may close these issues.

2 participants