Packages installed using poetry are not available inside `poetry shell`

586 views Asked by At

Packages installed using poetry install are available in poetry run but not in poetry shell.

$ poetry add {package}
$ poetry install
# this works
$ poetry run python
> import {package}
# ...but not this
$ poetry shell
$ python
> import {package}

Am I doing something wrong or misunderstanding what poetry shell does?
I thought it would activate the virtual environment setup by poetry.
poetry --version prints Poetry (version 1.5.1)

2

There are 2 answers

0
WieeRd On BEST ANSWER

As suggested by @Brian61354270's comment, I checked the Python executable that was being used inside poetry shell using type python. Turns out it was pyenv that was overriding the python path whenever I open a new shell. I was able to fix this problem by removing the pyenv config file and letting poetry control the Python version.

0
Pado On

I understand your frustration, it should work.

What I think you should do:

  1. $ deactivate (in case you are still in poetry shell)
  2. delete manually the poetry ".venv" folder (I suppose you have set "virtualenvs.in-project to true)
  3. delete manually the .lock file (I assume that your pyproject.toml is up-to-date with the packages you want to import)
  4. $ poetry self update

Then, reinstall everything.

  1. $ poetry install
  2. $ poetry shell
  3. $ python
  4. > import *your_package*

Hope that helps,