question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to properly install python modules with venv using DevOps Pipeline build? Module Not Found Error

See original GitHub issue

Note: There are already several issues mentioning this problem, but several of these were closed after finding some quickfix which were not working for me. See i.e. this issue.

Issue Statement: The general problem is that it seems not to be possible for me to import packages installed during DevOps pipline in an app. In my case i want to use azure.servicebus and have to use the SDK as the bindings don’t fullfill my requirements. However, after building with devops and uploading the artifact via SCM_RUN_FROM_PACKAGE the app starts running, but is not able to import the module from neither worker_env nor from .python_packages...

When deploying from local via function app core tools the problem does not occur. However, for deploying to production I have to use DevOps pipline!

Investigative information

Please provide the following:
  • Timestamp: 2020-07-15
  • Function App name: functestkjskdj
  • Core Tools version: 3.0.2534

Repro steps

Provide the steps required to reproduce the problem:

I use the following YAML in my Azure Pipelines Build:

trigger:
  - master
  - dev-*

variables:

  functionAppName: '$(FUNCTION_APP_NAME)'
  azureSubscription: '$(SERVICE_CONNECTION_NAME)'
  storageName: '$(FUCTION_APP_STORAGE_ACCOUNT_NAME)'
  resourceGroupName: '$(FUNCTION_APP_RESOURCE_GROUP_NAME)'

pool:
  vmImage: ubuntu-18.04
steps:
- task: UsePythonVersion@0
  displayName: "Setting python version to 3.6 as required by functions"
  inputs:
    versionSpec: '3.6' # Change Python version if necessary
    architecture: 'x64'
- script: |
    # dotnet restore # COMMENT OUT IF NOT USING FUNCTION EXTENSIONS
    # dotnet build --output './bin/' # COMMENT OUT IF NOT USING FUNCTION EXTENSIONS
    #source $(workingDirectory)/worker_venv/bin/activate
    #pip install -r requirements.txt
    #worker_venv/lib/python3.6/site-packages
    #pip install --target="$(workingDirectory)/.python_packages/lib/site-packages" -r requirements.txt
    pip install --target="worker_venv/lib/python3.6/site-packages" -r requirements.txt

- task: ArchiveFiles@2
  displayName: "Archive files"
  inputs:
    rootFolderOrFile: "$(System.DefaultWorkingDirectory)"
    includeRootFolder: false
    archiveFile: "$(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip"
- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'
- task: AzureFunctionApp@1
  displayName: "Azure functions app deploy"
  inputs:
    # todo: make service connection dynamic for alpha,beta,ga
    azureSubscription: $(azureSubscription) # here the subscription or service connection name
    appType: functionAppLinux
    appName: $(functionAppName)
    packageForLinux: '$(Build.ArtifactStagingDirectory)/*.zip'

Expected behavior

Provide a description of the expected behavior.

After deployment i would expect that I can import the modules from requirements.txt from my function modules.

Actual behavior

Provide a description of the actual behavior observed.

Imports of modules from requirements.txt produce Module Not Found Error

Known workarounds

Provide a description of any known workarounds.

I was not finding any workaround except doing zip deploy via core tools which is not really usable when using CI/CD via DevOps.

Contents of the requirements.txt file:

Provide the requirements.txt file to help us find out module related issues.
azure-servicebus
numpy

Related information

Provide any related information

I shared the testapp i am using on github: https://github.com/georghildebrand/functestkjskdj Note that i am not breaking on import error. Instead i string the errors and output it at REST. I am doing this as the monitoring is very slow which makes debugging at higher iteration speed very hard.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
AvinZarlezcommented, Jul 16, 2020

Here is our working YAML for installing dependencies that is working for us.

    - bash: |
        python -m venv worker_venv
        source worker_venv/bin/activate
        pip install --target="$(workingDirectory)/.python_packages/lib/site-packages" -r requirements.txt
      workingDirectory: $(workingDirectory)
      displayName: 'Install application dependencies'

Perhaps you need to define workingDirectory as ‘$(System.DefaultWorkingDirectory)/’?

1reaction
AvinZarlezcommented, Jul 16, 2020

We just ran into this same issue.

Changing pip install -r requirements.txt in the YAML file to pip install --target="$(workingDirectory)/.python_packages/lib/site-packages" -r requirements.txt resolved it for us.

We definitely would expect to not have to specify the target in order for pip install -r requirements.txt to be deployed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't find installed module in my Azure DevOps pipeline
I believe what's happening is that your script section is an isolated process and it does not do the preceding tasks within the...
Read more >
Installing packages using pip and virtual environments
To create a virtual environment, go to your project's directory and run venv. If you are using Python 2, replace venv with virtualenv...
Read more >
Troubleshoot Python errors in Azure Functions - Microsoft Learn
Troubleshoot ModuleNotFoundError. This section helps you troubleshoot module-related errors in your Python function app.
Read more >
Python Virtual Environments: A Primer
The instructions in this tutorial use Python's venv module to create virtual environments. This module is part of Python's standard library, ...
Read more >
Django Deployment on App Service Linux -
You should now see Django successfully installed after running this: ... not be built, thus causing errors like ModuleNotFound or others ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found