简体   繁体   中英

Using earlier version of python with poetry

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

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
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM