Using earlier version of python with poetry
Asked Answered
M

2

12

What is the process for setting up a project and using an earlier version of Python which has not been installed as a system-wide binary?

Ideally, poetry add <package> should install to that previous version of python, and poetry shell should open up a virtual environment with the correct version.

I have tried:

mkdir myproj
cd myproj

eval "$(pyenv init -)"
pyenv install 3.8.9
pyenv local 3.8.9

poetry init --no-interaction --python="3.8.9"
poetry env use 3.8.9
poetry add numpy

echo '
import sys
print(sys.version)

import numpy
print(numpy.__version__)
' > main.py

poetry shell
eval "$(pyenv init -)"
python main.py

But this gives:

3.8.9 (default, May  1 2021, 22:43:00)
[GCC 10.2.0]
Traceback (most recent call last):
  File "main.py", line 5, in <module>
    import numpy
ModuleNotFoundError: No module named 'numpy'

...indicating that the correct version of python ran (as expected), but that the package was not installed to python 3.8.9. Indeed:

λ ls "$(poetry env info --path)/lib"
python3.9

λ grep "version_info" "$(poetry env info --path)/pyvenv.cfg"
version_info = 3.9.4.final.0
Mirianmirielle answered 2/5, 2021 at 5:35 Comment(2)
Since you tagged pyenv, you clearly know a possible answer - what have you tried and what is the problem exactly?Inscription
The added information makes the problem clear (i.e. you expect to have installed numpy into the environment, but when running a script, it appears not to be available) - I had a quick look, but see no clear mistake or solution, upvoting your question.Inscription
M
3

Turns out that everything works correctly for pyenv 2.x (released just recently). The new instructions indicate that we now need to update $PATH as well with $HOME/.pyenv/shims. This is done via:

eval "$(pyenv init -)"
eval "$(pyenv init --path)"

Poetry now correctly installs python3.8 to $(poetry env info --path)/lib rather than python3.9 (?!). It is unclear why poetry was doing that in the first place, but I assume it was a pyenv 1.x bug.


Full example:

eval "$(pyenv init -)"
eval "$(pyenv init --path)"

mkdir myproj
cd myproj

pyenv install 3.8.9
pyenv local 3.8.9
poetry init --no-interaction --python="3.8.9"
poetry env use 3.8.9
poetry add numpy

echo '
import sys
print(sys.version)

import numpy
print(numpy.__version__)
' > main.py

poetry run python main.py

Output:

3.8.9 (default, Sep 14 2021, 18:39:31)
[GCC 11.1.0]
1.21.2
Mirianmirielle answered 15/9, 2021 at 10:58 Comment(1)
asdf also works.Heber
T
0
poetry run python main.py

You try this command.


# Initialize pyenv when a new shell spawns
eval "$(pyenv init -)"

# Modify path for Python's poetry dependency management system
export PATH="$HOME/.poetry/bin:$PATH"

Here is a discussion of issues similar to yours: https://github.com/python-poetry/poetry/issues/571

Trimming answered 15/9, 2021 at 8:29 Comment(2)
Adding eval "$(pyenv init -)"; eval "$(pyenv init --path)" to ~/.bash_profile and ~/.zprofile (which are loaded only for login bash or zsh shells) doesn't seem to fix the fact that poetry is actually managing python 3.9 rather than 3.8. ls "$(poetry env info --path)/lib" outputs python3.9.Mirianmirielle
$HOME/.poetry/bin does not exist on my system, so I'm not sure if there's something else I should use there. However, poetry shell does activate the virtual environment in $HOME/.cache/pypoetry/virtualenvs/myproj-izxvOuET-py3.8.Mirianmirielle

© 2022 - 2024 — McMap. All rights reserved.