nat-n / poethepoet

A task runner that works well with poetry.
https://poethepoet.natn.io/
MIT License
1.36k stars 56 forks source link

Poe script task uses global python path instead of the one in the virtual environment #230

Closed fritz-trawa closed 1 week ago

fritz-trawa commented 1 week ago
pyenv global 3.12
python -V 
# 3.12
cd my-project
pyenv local 3.10
poetry install --no-root
python -V 
# 3.10

Then I have a simple python script to do the same

# version.py
import sys

def echo():
    print("Python version:", sys.version)

if __name__ == "__main__":
    echo()

Then in my tasks file I have

[tool.poe.tasks.version]
script = "version:echo"

Then when I test it

python version.py
# Python version: 3.10.14 ....

python -m version
# Python version: 3.10.14 ....

poe version
# Poe => version
# Python version: 3.12.4 ...

How do I get poe to use the version in my local virtual environment? Thanks

nat-n commented 1 week ago

Hi @fritz-trawa, thanks for the question.

I need some more info to tell whether what you describe is expected behavior or not. If used with poetry then poe will by default use whatever python is referenced in the virtualenv managed by poetry, that is the env returned by poetry env info -p. Running poetry run python version.py should confirm whether poetry is using the expected python version for its virtualenv; could you try that please?

Another important question, is the version task defined in the main pyproject.toml file alongside poetry related config or is it imported from another file? If it's an import, does it start working if you move it to the pyproject.toml? If so then we have a bug.

This is because poe will only default to using the PoetryExecutor if it detects poetry related config, which should guarantee using the poetry virtualenv (if available).

Note that you can also configure poe to use a a specific virtualenv or to have no opinion about it.