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

No VIRTUAL_ENV variable available? #359

Closed
4 of 5 tasks
jenstroeger opened this issue Mar 25, 2022 · 10 comments
Closed
4 of 5 tasks

No VIRTUAL_ENV variable available? #359

jenstroeger opened this issue Mar 25, 2022 · 10 comments
Assignees
Labels
bug Something isn't working

Comments

@jenstroeger
Copy link

jenstroeger commented Mar 25, 2022

Description:
In an activated Python virtual environment (venv), the environment variable VIRTUAL_ENV contains the path to the base of the venv. That variable doesn’t exists, and it doesn’t seem possible to activate the environment in the action either? I could pass in that variable but then I’d have to make an assumption about where this Action installs Python on the runner.

Action version:
v3, latest

Platform:

  • Ubuntu
  • macOS
  • Windows

Runner type:

  • Hosted
  • Self-hosted

Tools version:
I tried with 3.9 and 3.10 and it failed for both.

Repro steps:
Failed activate:

No VIRTUAL_ENV variable:

Expected behavior:
Either being able to activate the environment (which is tricky from an Action considering the differences between Windows (Scripts/ folder) and Linux/Mac (bin/ folder). Ideally, the VIRTUAL_ENV would be set?

Actual behavior:
The variable isn’t available.

@jenstroeger jenstroeger added bug Something isn't working needs triage labels Mar 25, 2022
@marko-zivic-93
Copy link
Contributor

Hello @jenstroeger
Thank you for pointing this issue out to us!
We will get back to you as soon as possible with some meaningful information on the progress.

@vsafonkin
Copy link

vsafonkin commented Mar 28, 2022

Hi @jenstroeger

We cannot set pre-defined VIRTUAL_ENV variable because we are not able to know which directory a customer will be use for venv.

If you use Windows and Linux runners in the single workflow file you can use if condition for venv activation step.

Workflow example:

jobs:
  run_master:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ ubuntu-latest, macos-latest, windows-latest ]
        python: [ '3.9', '3.10' ]

    steps:
    - uses: actions/checkout@v3
      with:
        fetch-depth: 0
        
    - name: Set up Python
      uses: actions/setup-python@v3
      with:
        python-version: ${{ matrix.python }}
        
    - name: venv activate Linux
      if: ${{ matrix.os == 'ubuntu-latest' || matrix.os == 'macos-latest' }}
      run: |
        mkdir test-venv
        cd test-venv
        python -m venv .
        source ./bin/activate
        echo "VIRTUAL_ENV: $VIRTUAL_ENV"
        
    - name: venv activate Windows
      if: ${{ matrix.os == 'windows-latest' }}
      run: |
        mkdir test-venv
        cd test-venv
        python -m venv .
        ./Scripts/Activate.ps1
        Write-Host "VIRTUAL_ENV: $env:VIRTUAL_ENV"

It works as expected and VIRTUAL_ENV is set: example_workflow_run

@jenstroeger
Copy link
Author

Hmm, thank you @vsafonkin!

I’m curious: why does the setup-python Action not provide a ready-to-go virtual environment, and instead the user is required to create it?

@vsafonkin
Copy link

vsafonkin commented Mar 29, 2022

@jenstroeger, the setting up and activation of venv are not in scope of setup-python action because using of venv is not mandatory for all python developers, someone can use the other tools and packages. The main goal of setup-python action is installation of required python version on the runner.

@jenstroeger
Copy link
Author

Thanks @vsafonkin, that makes sense. Ok to close this issue?

@vsafonkin
Copy link

@jenstroeger, yes, I believe this issue can be closed.

@jenstroeger
Copy link
Author

@vsafonkin Following up on the above conversation. Is it correct that every step of a workflow job runs inside of its own shell? For example

jobs:
  foo:
    steps:
    - name: This.
      run: echo "This"
    - name: That.
      run: echo "That"

The two echos above run in separate shells? And that means that activating a virtual environment in one step does not carry over into the next step?

@vsafonkin
Copy link

@jenstroeger, it's correct. You can use GITHUB_ENV to pass values between steps.

- name: venv activate Linux
  run: |
    mkdir test-venv
    cd test-venv
    python -m venv .
    source ./bin/activate
    echo "VIRTUAL_ENV: $VIRTUAL_ENV"
    echo "VIRTUAL_ENV=$VIRTUAL_ENV" >> $GITHUB_ENV

- name: test
  run: |
    echo "VIRTUAL_ENV: $VIRTUAL_ENV"

@jenstroeger
Copy link
Author

And regarding the $PATH variable which is expanded by the activate script, that can also be passed down like you showed and the change to $PATH is confined to the step which activates the virtual environment… 👍🏼

@Kurt-von-Laven
Copy link
Contributor

Even running the activate script and persisting both VIRTUAL_ENV and PATH via $GITHUB_ENV isn't sufficient to activate the venv in future steps that are run via shell: python, because shell: python expands to the Python set up by setup-python, not the one in the virtual environment. You have to use shell: bash and run python explicitly. Is there any way to activate the venv for steps that use shell: python that I may have overlooked?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants