Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Poetry with PyEnv and having Python version issues

I'm working on WSL2 Ubuntu. I've been taking a course in backend API development using Fastapi. My Ubuntu default python is 3.8 I believe. I'm trying to use python 3.10.0 for my development. I've done the following:

  1. pyenv install 3.10.0
  2. cd to my project folder and pyenv local 3.10.0 (creates .python-version file containing 3.10.0)
  3. poetry shell (this creates a .venv folder and all dependencies are added there during development). Bash Prompt has (.venv) in it.
  4. python --version shows 3.10.0 as current version

My problem is that when I look into the .venv/bin file, all of the symbolic links show python 3.8.

drwxr-xr-x 2 mryan mryan 4096 Feb  1 23:12 .
drwxr-xr-x 4 mryan mryan 4096 Feb  1 23:12 ..
-rw-r--r-- 1 mryan mryan 2152 Feb  1 23:12 activate
-rw-r--r-- 1 mryan mryan 1444 Feb  1 23:12 activate.csh
-rw-r--r-- 1 mryan mryan 3033 Feb  1 23:12 activate.fish
-rw-r--r-- 1 mryan mryan 1303 Feb  1 23:12 activate.nu
-rw-r--r-- 1 mryan mryan 1754 Feb  1 23:12 activate.ps1
-rw-r--r-- 1 mryan mryan 1199 Feb  1 23:12 activate_this.py
-rw-r--r-- 1 mryan mryan  333 Feb  1 23:12 deactivate.nu
-rwxr-xr-x 1 mryan mryan  249 Feb  1 23:12 pip
-rwxr-xr-x 1 mryan mryan  249 Feb  1 23:12 pip-3.8
-rwxr-xr-x 1 mryan mryan  249 Feb  1 23:12 pip3
-rwxr-xr-x 1 mryan mryan  249 Feb  1 23:12 pip3.8
lrwxrwxrwx 1 mryan mryan   18 Feb  1 23:12 python -> /usr/bin/python3.8
lrwxrwxrwx 1 mryan mryan    6 Feb  1 23:12 python3 -> python
lrwxrwxrwx 1 mryan mryan    6 Feb  1 23:12 python3.8 -> python
-rwxr-xr-x 1 mryan mryan  236 Feb  1 23:12 wheel
-rwxr-xr-x 1 mryan mryan  236 Feb  1 23:12 wheel-3.8
-rwxr-xr-x 1 mryan mryan  236 Feb  1 23:12 wheel3
-rwxr-xr-x 1 mryan mryan  236 Feb  1 23:12 wheel3.8

When I try using the command poetry env use 3.10.0 I get the following:

(.venv) mryan ~/python/fastapi1/.venv/bin $poetry env use 3.10.0
/bin/sh: 1: python3.10: not found

  EnvCommandError

  Command python3.10 -c "import sys; print('.'.join([str(s) for s in sys.version_info[:3]]))" errored with the following return code 127, and output:


  at ~/.local/share/pypoetry/venv/lib/python3.8/site-packages/poetry/utils/env.py:378 in activate
       374│                     shell=True,
       375│                 )
       376│             )
       377│         except CalledProcessError as e:
    →  378│             raise EnvCommandError(e)
       379│
       380│         python_version = Version.parse(python_version.strip())
       381│         minor = "{}.{}".format(python_version.major, python_version.minor)
       382│         patch = python_version.text
(.venv) mryan ~/python/fastapi1/.venv/bin $python3.10
python3.10: command not found

What am I doing wrong here? I feel I'm missing something significant here. I've been through a zillion web pages on poetry and virtual environments.

like image 786
Mark Ryan Avatar asked Nov 19 '25 10:11

Mark Ryan


2 Answers

I had the same problem you are having, I used to be able to use:

pyenv local 3.10.0

poetry new example-project

And poetry would create example-project with a venv in it based on python 3.10.0. This no longer worked for me so I found that these commands did work for me:

poetry new example-project
cd example-project/
poetry env use 3.10.0
poetry update

In case your project already had a venv installed it might be a good idea to remove that first before using the poetry env command. I'm pretty sure you haven't added pyenv to your PATH properly. Check out the installation instructions for pyenv, specifically the 'Configure your shell's environment for Pyenv' section.

like image 116
SWHJ Avatar answered Nov 22 '25 00:11

SWHJ


I am surprised that poetry shell creates .venv. Are you certain about that? Or did you maybe create it with virtualenv?

I just kind of replicated what you probably did and did not get the error. I would recommend you to create a minimal, reproducible example. Then you will probably catch what is wrong. For this I case I recommend creating a bash script. A start could be:

mkdir ~/python/test_error
cd ~/python/test_error
cp ~/python/fastapi1/pyproject.toml .
pyenv local 3.10.0
poetry env use 3.10.0

This script works for me (when I change the folders).

like image 40
flyingdutchman Avatar answered Nov 21 '25 23:11

flyingdutchman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!