Skip to content Skip to sidebar Skip to footer

Poetry Change Python Version To 3.x

According to poetry's docs, the proper way to setup a new project is with poetry new poetry-demo, however this creates a project based on the now deprecated python2.7 by creating t

Solution 1:

Whenever you change dependencies by hand in your pyproject.toml you have to take care of these points:

  1. Run poetry lock afterwards or remove the poetry.lock file to force recreation of it. The reasons for this is, that poetry install takes the poetry.lock as input if can find one and not the pyproject.toml.

  2. If you change the python version and uses in-project virtualenv, remove the .venv before running poetry install. poetry doesn't change the python version of a venv once it is created, because it uses the python version itself to create the virtualenv.

Solution 2:

Poetry makes it super easy to work with different Python versions or virtual environments. The recommended way to specify your Python version according to Poetry docs is

poetry env use /path/to/preferred/python/version

You can get the path to your Python version by running

which python3.7

Solution 3:

I had the same problem. I solve it by fixing the first line in the file /home/nordman/.poetry/bin/poetry (nordman is my local name).

Just change #!/usr/bin/env python to #!/usr/bin/env python3

Solution 4:

Interestingly, poetry is silently failing due to a missing package the tool itself relies on and continues to install a broken venv. Here's how you fix it.

sudo apt install python3-venv
poetry env remove python3
poetry install

I had to remove pytest, and then reinstall with poetry add pytest.

EDIT: I ran into this issue again when upgrading a project from python3.7 to python3.8 - for this instead of installing python3-venv, you'd want to install python3.8-venv instead

Solution 5:

You can change in pyproject.toml and execute de this command "poetry env use 3.x" that works for me.

Post a Comment for "Poetry Change Python Version To 3.x"