poetry: Poetry fails to run under pyenv managed Python environment under v3.6.8

  • I am on the latest Poetry version.

  • I have searched the issues of this repo and believe that this is not a duplicate.

  • If an exception occurs when executing a command, I executed it again in debug mode (-vvv option).

  • OS version and name: Mac OS 11.2

  • Poetry version: 1.1.4

  • Link of a Gist with the contents of your pyproject.toml file: NA

Issue

When running certain Poetry commands (ie: poetry export -f requirements.txt --without-hashes --dev -vvvv) from within a workspace that is managed by pyenv (ie: within a folder that contains a file named .python-version), the commands fail with the following exception (notice the change in site paths mid-way through the stack trace):

Error initializing plugin EntryPoint(name=‘macOS’, value=‘keyring.backends.macOS’, group=‘keyring.backends’). Traceback (most recent call last): File “/Users/username/.poetry/lib/poetry/_vendor/py3.6/keyring/backend.py”, line 198, in _load_plugins init_func = ep.load() File “/Users/username/.poetry/lib/poetry/_vendor/py3.6/importlib_metadata/init.py”, line 105, in load module = import_module(match.group(‘module’)) File “/Users/username/.pyenv/versions/3.6.8/lib/python3.6/importlib/init.py”, line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File “<frozen importlib._bootstrap>”, line 994, in _gcd_import File “<frozen importlib._bootstrap>”, line 971, in _find_and_load File “<frozen importlib._bootstrap>”, line 953, in _find_and_load_unlocked ModuleNotFoundError: No module named ‘keyring.backends.macOS’

After some ad-hoc testing, I’ve managed to narrow the problem down to an unusual interaction between Poetry and the Python interpreter installed by penv. For testing purposes I tried reinstalling the python 3.6.8 runtime, but still had the same results. Then I tried updating the workspace to use a different Python 3 runtime version (ie: 3.6.0) and rerunning the same operation, and the error disappeared. So the problem seems isolated just to Python v3.6.8. I also tried disabling the pyenv integration completely and re-running the test case using the system installed Python runtime (v3.8.2 in my case) and the error also disappeared. Finally, I tried re-enabling the Python 3.6.8 runtime managed by pyenv and installing the same version of the keyring package required by Poetry in that runtime (ie: $(pyenv which python3) -m pip install keyring~=21.2.0) and re-ran the same command, and the error was no longer reproducible.

From what I can tell, when running Poetry in a workspace where the Python runtime version is exactly 3.6.8, the tool attempts to locate its own runtime dependencies in the system defined runtime environment instead of looking in the Python runtime that ships with Poetry, and because the required dependency doesn’t exist there the tool fails to run. I’m not entirely sure if this problem is limited strictly to MacOS and/or Python runtimes managed by pyenv, however if my analysis is correct then there is a rather subtle bug lurking here with the way Poetry is looking up its runtime dependencies. It should only every look in the runtime environment that ships with Poetry to ensure the correct set of dependencies are used when running the tool.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 10
  • Comments: 19 (3 by maintainers)

Most upvoted comments

Also happening here, although I am using conda

I could fix it by following your advice and explictly setting a backend for keyring. So I created the file ~/.config/python_keyring/keyringrc.cfg with the following content:

[backend]
default-keyring=keyring.backends.SecretService.Keyring

Disabling keyring also got rid of the error but then poetry wouldn’t authenticate.

These are my findings for getting rid of the error message as upgrading keyring pip3 install --upgrade pip -U keyring --user did not help. I tried to upgrade it globally and inside the virtual env.

For MacOs 12.4 adding ‘keyring.backends.OS_X.Keyring’ as default solves the issue. ~/.config/python_keyring/keyringrc.cfg:

[backend]
default-keyring=keyring.backends.OS_X.Keyring

Just adding a comment in case it helps someone looking to get around this problem - I fixed it by ditching conda from my machine.

@Vichoko I had the same issue with Python 3.9 and Poetry 1.1.13, but switching to Python 3.7.10 resolved the error. Perhaps you can try that?