poetry: [EnvCommandError] returned on `poetry install` and `poetry env`

  • 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: Windows 10 Pro

  • Poetry version: 1.0.9

  • Link of a Gist with the contents of your pyproject.toml file: https://github.com/ernopp/smilebot/blob/master/pyproject.toml

Issue

Hi

There is an issue with my poetry installation

I am on version 1.0.9

C:\Users\ernest\dev\smilebot>poetry -V
Poetry version 1.0.9

My TOML appears correct

C:\Users\ernest\dev\smilebot>poetry check
All set!

But when I try to run poetry env info or poetry install (and same is true of many other commands I suspect) I get:

C:\Users\ernest\dev\smilebot>poetry install -vvv

[EnvCommandError]
Command ['C:\\Users\\ernest\\AppData\\Local\\pypoetry\\Cache\\virtualenvs\\shoponsmile-python-ovOS_Rxi-py3.8\\Scripts\\python.exe', '-'] errored with the following return code 1, and output:
The system cannot find the path specified.
input was : import sys

if hasattr(sys, "real_prefix"):
    print(sys.real_prefix)
elif hasattr(sys, "base_prefix"):
    print(sys.base_prefix)
else:
    print(sys.prefix)


Traceback (most recent call last):
  File "C:\Users\ernest\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\clikit\console_application.py", line 131, in run
    status_code = command.handle(parsed_args, io)
  File "C:\Users\ernest\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\clikit\api\command\command.py", line 120, in handle
    status_code = self._do_handle(args, io)
  File "C:\Users\ernest\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\clikit\api\command\command.py", line 163, in _do_handle
    self._dispatcher.dispatch(PRE_HANDLE, event)
  File "C:\Users\ernest\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\clikit\api\event\event_dispatcher.py", line 22, in dispatch
    self._do_dispatch(listeners, event_name, event)
  File "C:\Users\ernest\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\clikit\api\event\event_dispatcher.py", line 89, in _do_dispatch
    listener(event, event_name, self)
  File "C:\Users\ernest\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\poetry\console\config\application_config.py", line 89, in set_env
    env = env_manager.create_venv(io)
  File "C:\Users\ernest\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\poetry\utils\env.py", line 489, in create_venv
    env = self.get(reload=True)
  File "C:\Users\ernest\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\poetry\utils\env.py", line 352, in get
    return VirtualEnv(venv)
  File "C:\Users\ernest\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\poetry\utils\env.py", line 1022, in __init__
    self._base = Path(self.run("python", "-", input_=GET_BASE_PREFIX).strip())
  File "C:\Users\ernest\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\poetry\utils\env.py", line 856, in run
    return self._run(cmd, **kwargs)
  File "C:\Users\ernest\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\poetry\utils\env.py", line 1089, in _run
    return super(VirtualEnv, self)._run(cmd, **kwargs)
  File "C:\Users\ernest\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\poetry\utils\env.py", line 893, in _run
    raise EnvCommandError(e, input=input_)

I can see and run the python executable at C:\Users\ernest\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\Local\pypoetry\Cache\virtualenvs\shoponsmile-python-ovOS_Rxi-py3.8\Scripts

This happens whether I install poetry via pip or via the curl command (tried both)

Any help appreciated

Thanks

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 30
  • Comments: 28 (5 by maintainers)

Commits related to this issue

Most upvoted comments

I was having the same issue when caching .venv directory on a Windows runner. It turns out restoring cache breaks symlinks in the virtual env.

The solution is to first create virtualenv manually with: python -m venv .venv --copies

Which does not rely on symlinks and then run: poetry install

I have the same issue, but removing virtualenv fix the thing…

I solve my issue here changed the Python version. If I use Python 3.9.0 I have this problem, but if I use Python 3.8.6 (the last version of Python 3.8) all works ok! I’m in the Debian GNU/Linux system!

@floatingpurr yep, I gave PDM a try because of some of Poetry’s issues and limitations (not the place, but I can elaborate if needed). I was immediately hooked, and am exclusively using PDM now. It’s really good.

Simplest test fix on windows for me was as above: $env:POETRY_CACHE_DIR = ‘your-project-dir\.poetry_cache’

but it looks to me like a single global .poetry_cache directory would also work fine with poetry since it handles the naming… PS> [System.Environment]::SetEnvironmentVariable(‘POETRY_CACHE_DIR’, ‘C:\Users\MyUser.poetry_cache’, [System.EnvironmentVariableTarget]::User)

Then see: https://crapts.org/2020/05/11/use-poetry-for-python-dependency-management-with-visual-studio-code/

I’m also experiencing the same thing. I just installed python 3.9 from windows store and installed poetry version 1.1.4.

Here is the error message:

$ poetry -V
Poetry version 1.1.4

$ poetry check
All set!

$ poetry install -vvv

  Stack trace:

  11  ~\.poetry\lib\poetry\_vendor\py3.9\clikit\console_application.py:131 in run
       129│             parsed_args = resolved_command.args
       130│
     → 131│             status_code = command.handle(parsed_args, io)
       132│         except KeyboardInterrupt:
       133│             status_code = 1

  10  ~\.poetry\lib\poetry\_vendor\py3.9\clikit\api\command\command.py:120 in handle
       118│     def handle(self, args, io):  # type: (Args, IO) -> int
       119│         try:
     → 120│             status_code = self._do_handle(args, io)
       121│         except KeyboardInterrupt:
       122│             if io.is_debug():

   9  ~\.poetry\lib\poetry\_vendor\py3.9\clikit\api\command\command.py:163 in _do_handle
       161│         if self._dispatcher and self._dispatcher.has_listeners(PRE_HANDLE):
       162│             event = PreHandleEvent(args, io, self)
     → 163│             self._dispatcher.dispatch(PRE_HANDLE, event)
       164│
       165│             if event.is_handled():

   8  ~\.poetry\lib\poetry\_vendor\py3.9\clikit\api\event\event_dispatcher.py:22 in dispatch
        20│
        21│         if listeners:
     →  22│             self._do_dispatch(listeners, event_name, event)
        23│
        24│         return event

   7  ~\.poetry\lib\poetry\_vendor\py3.9\clikit\api\event\event_dispatcher.py:89 in _do_dispatch
        87│                 break
        88│
     →  89│             listener(event, event_name, self)
        90│
        91│     def _sort_listeners(self, event_name):  # type: (str) -> None

   6  ~\.poetry\lib\poetry\console\config\application_config.py:119 in set_env
       117│
       118│         env_manager = EnvManager(poetry)
     → 119│         env = env_manager.create_venv(io)
       120│
       121│         if env.is_venv() and io.is_verbose():

   5  ~\.poetry\lib\poetry\utils\env.py:626 in create_venv
        624│
        625│         cwd = self._poetry.file.parent
     →  626│         env = self.get(reload=True)
        627│
        628│         if not env.is_sane():

   4  ~\.poetry\lib\poetry\utils\env.py:489 in get
        487│                 return SystemEnv(Path(sys.prefix))
        488│
     →  489│             return VirtualEnv(venv)
        490│
        491│         if env_prefix is not None:

   3  ~\.poetry\lib\poetry\utils\env.py:1248 in __init__
       1246│         # from inside the virtualenv.
       1247│         if base is None:
     → 1248│             self._base = Path(self.run("python", "-", input_=GET_BASE_PREFIX).strip())
       1249│
       1250│     @property

   2  ~\.poetry\lib\poetry\utils\env.py:1037 in run
       1035│         bin = self._bin(bin)
       1036│         cmd = [bin] + list(args)
     → 1037│         return self._run(cmd, **kwargs)
       1038│
       1039│     def run_pip(self, *args, **kwargs):

   1  ~\.poetry\lib\poetry\utils\env.py:1332 in _run
       1330│             self.unset_env("__PYVENV_LAUNCHER__")
       1331│
     → 1332│             return super(VirtualEnv, self)._run(cmd, **kwargs)
       1333│
       1334│     def execute(self, bin, *args, **kwargs):

  EnvCommandError

  Command C:\Users\foobar\AppData\Local\pypoetry\Cache\virtualenvs\helloworld-2Fe2M40X-py3.9\Scripts\python.exe - errored with the following return code 1, and output:
  The system cannot find the path specified.
  input was : import sys

  if hasattr(sys, "real_prefix"):
      print(sys.real_prefix)
  elif hasattr(sys, "base_prefix"):
      print(sys.base_prefix)
  else:
      print(sys.prefix)


  at ~\.poetry\lib\poetry\utils\env.py:1074 in _run
      1070│                 output = subprocess.check_output(
      1071│                     cmd, stderr=subprocess.STDOUT, **kwargs
      1072│                 )
      1073│         except CalledProcessError as e:
    → 1074│             raise EnvCommandError(e, input=input_)
      1075│
      1076│         return decode(output)
      1077│
      1078│     def execute(self, bin, *args, **kwargs):

This is my pyproject.toml file just in case:

[tool.poetry]
name = "helloworld"
version = "0.1.0"
description = ""
authors = ["tulis"]

[tool.poetry.dependencies]
python = "^3.9"

[tool.poetry.dev-dependencies]
pytest = "^4.6"
black = "^20.8b1"
isort = "^5.7.0"
flake8 = "^3.8.4"
mypy = "^0.800"
pre-commit = "^2.10.0"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

Any help would be appreciated.

@1Mark The issue is that this is years old, the OP never provided sufficient detail to reproduce/check that this was fixed for them, and many users have chimed in with related but not the same (mostly configuration/usage) issues in the comments.

It’s hard enough to reason about a multiple years and version old issue report with no response or reproduction, let alone what has become a grab bag of “me too” that may or may not be the same root cause or even operating system.

It does create a higher barrier if someone runs into trouble, but this issue was already not helpful to both those experiencing this issue and those trying to solve it – without high quality issue reports it is very hard to fix bugs that are not obvious.

I ran into this issue trying to use Poetry with Windows + Python 3.6 and 3.9 in GitHub actions, where I have actions/cache configured to cache and restore Poetry’s virtualenv directory. After doing a little digging, I discovered that Poetry’s own GitHub Actions CI for Windows + Python 3.6 (which also caches the .venv folder and restores it) is experiencing this same issue.

Poetry’s CI workflow includes a step to nuke the virtualenv if it appears unhealthy after a cache restore, which looks to be how they work around it.

https://github.com/python-poetry/poetry/blob/527882380fd7aecd7c34922a7bc888226a5737ff/.github/workflows/main.yml#L75-L78

Here’s what I ended up with in CI. Took me the entire evening to make things work on Windows.

env:
  # To fix an error when running Poetry on Windows
  # (https://github.com/python-poetry/poetry/issues/2629),
  # we set Poetry's cache directory to .poetry_cache in the current directory.
  # It makes it easier to later remove the virtualenv when it's broken.
  # Absolute path is necessary to avoid this issue:
  # https://github.com/python-poetry/poetry/issues/3049
  POETRY_CACHE_DIR: ${{ github.workspace }}/.poetry_cache

jobs:

  tests:

    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        python-version: [3.6, 3.7, 3.8, 3.9]

    runs-on: ${{ matrix.os }}

    steps:

    # checkout repo, setup python, install poetry, etc.

    - name: Set up the cache
      uses: actions/cache@v1
      with:
        path: .poetry_cache
        key: tests-poetry-cache-${{ matrix.os }}-py${{ matrix.python-version }}

    - name: Set up the project
      run: poetry install -vv || { rm -rf .poetry_cache/virtualenvs/*; poetry install -vv; }

Can you make sure that the virtual environment in C:\\Users\\ernest\\AppData\\Local\\pypoetry\\Cache\\virtualenvs\\shoponsmile-python-ovOS_Rxi-py3.8 was properly created?

It looks like you’re having conflicting pythons running. Do you happen to either be in an existing virtual environment or using PyCharm by chance/bad luck?

Error at the top mentions: [‘C:\Users\ernest\AppData\Local\pypoetry\Cache\virtualenvs\shoponsmile-python-ovOS_Rxi-py3.8\Scripts\python.exe’, ‘-’]

Error in the stack mentions C:\Users\ernest\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38.…

I was having the same issue when caching .venv directory on a Windows runner. It turns out restoring cache breaks symlinks in the virtual env.

The solution is to first create virtualenv manually with: python -m venv .venv --copies

Which does not rely on symlinks and then run: poetry install

Thank you this worked for me.

I really dislike this approach, an issue was reported against an old version, has all the details, and it wasn’t fixed yet it’s closed, then another user will have to open another ticket for the same problem. I appreciate the original reported version is no longer supported but if the issue exists in the latest poetry version it doesn’t make sense to close this ticket.

I’m having the same issue:

Creating virtualenv tests-F6YQ1RRR-py3.9 in /home/river/.cache/pypoetry/virtualenvs
Updating dependencies
Resolving dependencies... (11.9s)

Writing lock file

Package operations: 13 installs, 0 updates, 0 removals

  • Installing pyparsing (2.4.7)
  • Installing attrs (20.3.0)
  • Installing iniconfig (1.1.1)
  • Installing packaging (20.9)
  • Installing pluggy (0.13.1)
  • Installing py (1.10.0)
  • Installing toml (0.10.2)
  • Installing asgiref (3.3.1)
  • Installing pytest (6.2.2)
  • Installing pytz (2021.1)
  • Installing sqlparse (0.4.1)
  • Installing django (3.1.7): Failed

  EnvCommandError

  Command ['/home/river/.cache/pypoetry/virtualenvs/tests-F6YQ1RRR-py3.9/bin/pip', 'install', '--no-deps', 'file:///home/river/.cache/pypoetry/artifacts/80/9f/e8/7dbac946baeff06413dea73548571fa188758ff084c6258f4e50a09137/Django-3.1.7-py3-none-any.whl'] errored with the following return code 2, and output: 
  Processing /home/river/.cache/pypoetry/artifacts/80/9f/e8/7dbac946baeff06413dea73548571fa188758ff084c6258f4e50a09137/Django-3.1.7-py3-none-any.whl
  ERROR: Exception:
  Traceback (most recent call last):
    File "/home/river/.cache/pypoetry/virtualenvs/tests-F6YQ1RRR-py3.9/lib/python3.9/site-packages/pip/_internal/cli/base_command.py", line 216, in _main
      status = self.run(options, args)
    File "/home/river/.cache/pypoetry/virtualenvs/tests-F6YQ1RRR-py3.9/lib/python3.9/site-packages/pip/_internal/cli/req_command.py", line 182, in wrapper
      return func(self, options, args)
    File "/home/river/.cache/pypoetry/virtualenvs/tests-F6YQ1RRR-py3.9/lib/python3.9/site-packages/pip/_internal/commands/install.py", line 324, in run
      requirement_set = resolver.resolve(
    File "/home/river/.cache/pypoetry/virtualenvs/tests-F6YQ1RRR-py3.9/lib/python3.9/site-packages/pip/_internal/resolution/legacy/resolver.py", line 183, in resolve
      discovered_reqs.extend(self._resolve_one(requirement_set, req))
    File "/home/river/.cache/pypoetry/virtualenvs/tests-F6YQ1RRR-py3.9/lib/python3.9/site-packages/pip/_internal/resolution/legacy/resolver.py", line 391, in _resolve_one
      dist = abstract_dist.get_pkg_resources_distribution()
    File "/home/river/.cache/pypoetry/virtualenvs/tests-F6YQ1RRR-py3.9/lib/python3.9/site-packages/pip/_internal/distributions/wheel.py", line 29, in get_pkg_resources_distribution
      with ZipFile(self.req.local_file_path, allowZip64=True) as z:
    File "/home/river/.pyenv/versions/3.9.0/lib/python3.9/zipfile.py", line 1257, in __init__
      self._RealGetContents()
    File "/home/river/.pyenv/versions/3.9.0/lib/python3.9/zipfile.py", line 1324, in _RealGetContents
      raise BadZipFile("File is not a zip file")
  zipfile.BadZipFile: File is not a zip file
  WARNING: You are using pip version 20.2.2; however, version 21.0.1 is available.
  You should consider upgrading via the '/home/river/.cache/pypoetry/virtualenvs/tests-F6YQ1RRR-py3.9/bin/python -m pip install --upgrade pip' command.
  

  at ~/.poetry/lib/poetry/utils/env.py:1074 in _run
      1070│                 output = subprocess.check_output(
      1071│                     cmd, stderr=subprocess.STDOUT, **kwargs
      1072│                 )
      1073│         except CalledProcessError as e:
    → 1074│             raise EnvCommandError(e, input=input_)
      1075│ 
      1076│         return decode(output)
      1077│ 
      1078│     def execute(self, bin, *args, **kwargs):

  • Installing pytest-django (4.1.0)