poetry: poetry and pyenv don't work well together

I am using pyenv to manage my python versions and virtualenvs. This seems to cause some problems with poetry.

>> which python
~/.pyenv/shims/python
>> which pip
~/.pyenv/shims/pip
>> poetry run which python
~/.cache/pypoetry/virtualenvs/XXXXX-py3.6/bin/python
>> poetry run which pip
~/.pyenv/shims/pip

This seems to cause the issue, that packages are installed in to the python version set by pyenv, but the python interpreter is used from the poetry virtualenv. This is no problem, when I activate the virtualenv first and then run poetry install But if I want to rely on poetry’s automatic virtualenv management, this doesn’t work anymore.

If you need more info I’ll gladly provide it.

Thanks for your great work and tool, it’s a real pleasure to work with 😃

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 38
  • Comments: 27 (7 by maintainers)

Most upvoted comments

I am currently using pyenv on MacOS and I don’t have any problem making it working.

I didn’t do anything special, I only have this on my .zshrc:

# pyenv
export PYENV_ROOT=/usr/local/var/pyenv

if [[ -z "$VIRTUAL_ENV" ]]; then
    eval "$(pyenv init -)"
    eval "$(pyenv virtualenv-init -)"
fi

Just to give my feedback here, I’m new to poetry and encountered issues when using pyenv and poetry together. Basically, it installed packages to a newly created virtualenv directory that was then nowhere to be found, poetry shell didn’t work, etc.

I installed poetry using curl -sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python. My python used there is python 3.7.1 from ~/.pyenv/shims/python. Then I went to my code repository, activated my pyenv virtualenv, executed poetry install and had the problems mentioned above.

I’m just commenting here, because I believe there is some underlying issue here that’s not fixed.

Extra feedback on this issue, I’m new to both pyenv and poetry, here are the steps to reproduce my environment.

$ pyenv local 2.7.16
$ which python
/home/lurst/.pyenv/shims/python
$ poetry shell
Spawning shell within ...
$ which python
/home/lurst/.pyenv/shims/python
$ ⏎  ( Ctrl+D to exit shell)
$ poetry run which python
/home/lurst/.cache/pypoetry/virtualenvs/myproject/bin/python

Which means that I cannot use the shell to interact with poetry’s virtualenv.

poetry shell spawns a new shell adding a new folder to PATH env variable. But new shell also loads all rc scripts like .bash_profile. That file likely has pyenv initialization line eval "$(pyenv init -)" that overrides venv python

Avoiding initializing pyenv within spawned shell works for me.

if [ -z "$PYENV_INITIALIZED" ]; then
   eval "$(pyenv init -)"
   export PYENV_INITIALIZED=1
fi

But something doesn’t look good - it’s the same case as in first post in this thread:

> poetry new .
Created package 1 in .

> poetry run which python
Creating virtualenv 1-py3.6 in /home/kossak/.cache/pypoetry/virtualenvs
/home/kossak/.cache/pypoetry/virtualenvs/1-py3.6/bin/python

> poetry run which pip
/home/kossak/.pyenv/shims/pip

python points to virtualenv python, but pip points to pyenv shim. Shouldn’t pip point to virtualenv too?

If people are still having issues with pyenv I suggest switching from pyenv to asdf. It can manage versions of not only python and for me works perfectly fine with poetry.

I use pyenv and I am getting inconsistent behavior between poetry run and poetry shell.

> which python
/Users/nackjicholson/.pyenv/shims/python
> python --version
Python 3.6.6
> poetry run python --version
Python 3.6.6
> poetry run which python
/Users/nackjicholson/Library/Caches/pypoetry/virtualenvs/source-data-service-py3.6/bin/python
> poetry run which pip
/Users/nackjicholson/Library/Caches/pypoetry/virtualenvs/source-data-service-py3.6/bin/pip
> poetry shell
Spawning shell within /Users/nackjicholson/Library/Caches/pypoetry/virtualenvs/source-data-service-py3.6
> which pip
/Users/nackjicholson/.pyenv/shims/pip
> which python
/Users/nackjicholson/.pyenv/shims/python
>

I would not expect the which command to point at different paths between the shell and run commands as it is here.

Edit, the .zshrc solution above with the $(pyenv virtualenv-init -) fixed this for me. I would say that is doing something “special” to make it work though.

If you’re using pipsi to install poetry, make sure you’re using the latest version available, from the git repository, because the one on PyPI is about three years old.

pip install -U --user git+https://github.com/mitsuhiko/pipsi/

The old version always uses virtualenv, even if you’re on Python 3, and that messes the venv module up.

Deleting .python-version on every other base directory than project works…

/Users/abc/based-dir/project-dir/

Then I deleted the /Users/abc/.python-version and /Users/abc/based-dir/.python-version Just kept ``/Users/abc/based-dir/project-dir/.python-version`

@tkossak I guess pipsi is the way to go then.

I think the PYENV_VERSION=system approach worked for me because the system python is actually the one installed with homebrew, which doesn’t require root privileges.

edit: typos, clarifications

I’ve had this same issue. For me, it only works if I tell pyenv to use the system python version:

curl -sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | env PYENV_VERSION=system python

Installed this way, the poetry command is available everywhere. @tkossak does this work for you?