question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

poetry install fails on KeyError even for defined environment variables

See original GitHub issue
  • 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).

Issue

We run poetry install --remove-untracked and the process randomly fails on various β€œmissing” environment variables. This has been reported in #3144 for the PATH variable (which happens quite a lot, more than other variables), but I have seen it for many other variables (often GitLab or Docker related). In all cases they are defined.

It fails really randomly. Different dependencies being installed, different environment variables. Sometimes it does not fail (but most often it does). Sometimes it fails on multiple environment variables. Like KeyError 'PATH', KeyError 'CI_JOB_STAGE' or KeyError 'DOCKER_ENV_CI_DISPOSABLE_ENVIRONMENT' in the same run. It seems to me like some kind of race condition in the parallel install. I can’t prove this hypothesis until we can disable parallel installation. I think it’s scheduled for an upcoming release.

Here is an example output where it fails on the PATH variable.

Creating virtualenv bp-xxx-3RqKXHa2-py3.7 in /builds/xxx/.cache/poetry/pypoetry/virtualenvs
Installing dependencies from lock file
Package operations: 106 installs, 0 updates, 0 removals
  β€’ Installing attrs (20.2.0)
  β€’ Installing six (1.15.0)
  β€’ Installing cattrs (1.0.0)
...
Stack trace:
  8  /usr/local/lib/python3.7/site-packages/poetry/installation/executor.py:199 in _execute_operation
      197β”‚ 
      198β”‚             try:
    β†’ 199β”‚                 result = self._do_execute_operation(operation)
      200β”‚             except EnvCommandError as e:
      201β”‚                 if e.e.returncode == -2:
  7  /usr/local/lib/python3.7/site-packages/poetry/installation/executor.py:273 in _do_execute_operation
      271β”‚             return 0
      272β”‚ 
    β†’ 273β”‚         result = getattr(self, "_execute_{}".format(method))(operation)
      274β”‚ 
      275β”‚         if result != 0:
  6  /usr/local/lib/python3.7/site-packages/poetry/installation/executor.py:408 in _execute_install
      406β”‚ 
      407β”‚     def _execute_install(self, operation):  # type: (Install) -> None
    β†’ 408β”‚         return self._install(operation)
      409β”‚ 
      410β”‚     def _execute_update(self, operation):  # type: (Update) -> None
  5  /usr/local/lib/python3.7/site-packages/poetry/installation/executor.py:446 in _install
      444β”‚             args.insert(2, "-U")
      445β”‚ 
    β†’ 446β”‚         return self.run_pip(*args)
      447β”‚ 
      448β”‚     def _update(self, operation):
  4  /usr/local/lib/python3.7/site-packages/poetry/installation/executor.py:297 in run_pip
      295β”‚     def run_pip(self, *args, **kwargs):  # type: (...) -> int
      296β”‚         try:
    β†’ 297β”‚             self._env.run_pip(*args, **kwargs)
      298β”‚         except EnvCommandError as e:
      299β”‚             output = decode(e.e.output)
  3  /usr/local/lib/python3.7/site-packages/poetry/utils/env.py:916 in run_pip
       914β”‚         pip = self.get_pip_command()
       915β”‚         cmd = pip + list(args)
    β†’  916β”‚         return self._run(cmd, **kwargs)
       917β”‚ 
       918β”‚     def _run(self, cmd, **kwargs):
  2  /usr/local/lib/python3.7/site-packages/poetry/utils/env.py:1183 in _run
      1181β”‚     def _run(self, cmd, **kwargs):
      1182β”‚         with self.temp_environ():
    β†’ 1183β”‚             os.environ["PATH"] = self._updated_path()
      1184β”‚             os.environ["VIRTUAL_ENV"] = str(self._path)
      1185β”‚ 
  1  /usr/local/lib/python3.7/site-packages/poetry/utils/env.py:1215 in _updated_path
      1213β”‚ 
      1214β”‚     def _updated_path(self):
    β†’ 1215β”‚         return os.pathsep.join([str(self._bin_dir), os.environ["PATH"]])
      1216β”‚ 
      1217β”‚ 
  KeyError
  'PATH'
  at /usr/local/lib/python3.7/os.py:681 in __getitem__
       677β”‚         try:
       678β”‚             value = self._data[self.encodekey(key)]
       679β”‚         except KeyError:
       680β”‚             # raise KeyError with the original key value
    β†’  681β”‚             raise KeyError(key) from None
       682β”‚         return self.decodevalue(value)
       683β”‚ 
       684β”‚     def __setitem__(self, key, value):
       685β”‚         key = self.encodekey(key)
  β€’ Installing pluggy (0.13.1)
  β€’ Installing py (1.9.0)
  β€’ Installing wcwidth (0.2.5)
  β€’ Installing yarl (1.6.0)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:22
  • Comments:33 (10 by maintainers)

github_iconTop GitHub Comments

9reactions
gsemetcommented, Dec 1, 2020

I see this issue from time to time on our gitlab ci (kubernetes runner). I works pretty much all the time after a retry (so same environment, same container, same git commit)

4reactions
cglacetcommented, Nov 13, 2020

That’s strange I got a similar error on gitlab CI (even though I don’t get the same locally, even using the gitlab-runner):

 β€’ Installing multidict (5.0.0)
  Stack trace:
  12  /usr/local/lib/python3.8/site-packages/poetry/installation/executor.py:202 in _execute_operation
      result = self._do_execute_operation(operation)
  11  /usr/local/lib/python3.8/site-packages/poetry/installation/executor.py:276 in _do_execute_operation
      result = getattr(self, "_execute_{}".format(method))(operation)
  10  /usr/local/lib/python3.8/site-packages/poetry/installation/executor.py:411 in _execute_install
      return self._install(operation)
   9  /usr/local/lib/python3.8/site-packages/poetry/installation/executor.py:437 in _install
      archive = self._download(operation)
   8  /usr/local/lib/python3.8/site-packages/poetry/installation/executor.py:580 in _download
      return self._download_link(operation, link)
   7  /usr/local/lib/python3.8/site-packages/poetry/installation/executor.py:589 in _download_link
      archive = self._download_archive(operation, link)
   6  /usr/local/lib/python3.8/site-packages/poetry/installation/executor.py:615 in _download_archive
      response = self._authenticator.request(
   5  /usr/local/lib/python3.8/site-packages/poetry/installation/authenticator.py:66 in request
      settings = session.merge_environment_settings(
   4  /usr/local/lib/python3.8/site-packages/requests/sessions.py:711 in merge_environment_settings
      env_proxies = get_environ_proxies(url, no_proxy=no_proxy)
   3  /usr/local/lib/python3.8/site-packages/requests/utils.py:775 in get_environ_proxies
      return getproxies()
   2  /usr/local/lib/python3.8/urllib/request.py:2502 in getproxies_environment
      for name, value in os.environ.items():
   1  /usr/local/lib/python3.8/_collections_abc.py:744 in __iter__
      yield (key, self._mapping[key])
  KeyError
  'VIRTUAL_ENV'
  at /usr/local/lib/python3.8/os.py:675 in __getitem__
       671β”‚         try:
       672β”‚             value = self._data[self.encodekey(key)]
       673β”‚         except KeyError:
       674β”‚             # raise KeyError with the original key value
    β†’  675β”‚             raise KeyError(key) from None
       676β”‚         return self.decodevalue(value)
       677β”‚ 
       678β”‚     def __setitem__(self, key, value):
       679β”‚         key = self.encodekey(key)

On the other hand, locally I have the environment variable defined:

❯ echo $VIRTUAL_ENV
/Users/cglacet/.pyenv/versions/3.7.7/envs/env-name

The suggested fix did work for me too:

poetry config experimental.new-installer false
Read more comments on GitHub >

github_iconTop Results From Across the Web

poetry install fails on KeyError even for defined environment ...
It fails really randomly. Different dependencies being installed, different environment variables. Sometimes it does not fail (but most often itΒ ...
Read more >
Python os.environ throws key error? - Stack Overflow
I'm accessing an environment variable in a script with os.environ.get and it's throwing a KeyError . It doesn't throw the error from theΒ ......
Read more >
History | Poetry - Python dependency management and ...
Fix an issue where the installation of dependencies failed if pip is a dependency and ... Fixed KeyError when PATH is not defined...
Read more >
Options - cibuildwheel - Read the Docs
A list of environment variables to set during the build. Bash syntax should be used, even on Windows. You must set this variable...
Read more >
Xonsh Change Log - xonsh 0.13.2 documentation
Fixed error caused by unintialized Xonsh session env when using Xonsh as a ... They also did not support defining variables and then...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found