poetry icon indicating copy to clipboard operation
poetry copied to clipboard

[WinError 193] when `virtualenvs.prefer-active-python` is `true`

Open IvanaGyro opened this issue 2 years ago • 9 comments

  • Poetry version: 1.6.0.dev0
  • Python version: 3.12.0
  • OS version and name: Window 11
  • pyproject.toml: https://github.com/devnoname120/google-play-book-downloader/blob/545b4535f24fb834dc888e7cb36223341178b857/pyproject.toml
  • [x] I am on the latest stable Poetry version, installed using a recommended method.
  • [x] I have searched the issues of this repo and believe that this is not a duplicate.
  • [x] I have consulted the FAQ and blog for any relevant entries or release notes.
  • [x] If an exception occurs when executing a command, I executed it again in debug mode (-vvv option) and have included the output below.

Due to #8160 I can't install the latest stable version via the command in the document.

Issue

I got

[WinError 193] %1 is not a valid Win32 application.

with virtualenvs.prefer-active-python set to true. There may be a related thread #7075.

I use pyenv to manage my Python version. To use the different Python version from the version used to install poetry, I enable virtualenvs.prefer-active-python following the document.

poetry config virtualenvs.prefer-active-python true

My current Python versions are

pyenv global: 3.12.0b4
pyenv local: 3.10.11

However poetry doesn't use 3.10.11. Here is the output of poetry debug info -vvv


Poetry
Loading configuration file C:\Users\Ivana\AppData\Roaming\pypoetry\config.toml
Version: 1.6.0.dev0
Python:  3.12.0

  Stack trace:

  18  ~\AppData\Roaming\pypoetry\venv\Lib\site-packages\cleo\application.py:327 in run
       325│
       326│             try:
     → 327│                 exit_code = self._run(io)
       328│             except BrokenPipeError:
       329│                 # If we are piped to another process, it may close early and send a

  17  ~\AppData\Roaming\pypoetry\venv\Lib\site-packages\poetry\console\application.py:190 in _run
       188│         self._load_plugins(io)
       189│
     → 190│         exit_code: int = super()._run(io)
       191│         return exit_code
       192│

  16  ~\AppData\Roaming\pypoetry\venv\Lib\site-packages\cleo\application.py:431 in _run
       429│             io.input.interactive(interactive)
       430│
     → 431│         exit_code = self._run_command(command, io)
       432│         self._running_command = None
       433│

  15  ~\AppData\Roaming\pypoetry\venv\Lib\site-packages\cleo\application.py:473 in _run_command
       471│
       472│         if error is not None:
     → 473│             raise error
       474│
       475│         return terminate_event.exit_code

  14  ~\AppData\Roaming\pypoetry\venv\Lib\site-packages\cleo\application.py:457 in _run_command
       455│
       456│             if command_event.command_should_run():
     → 457│                 exit_code = command.run(io)
       458│             else:
       459│                 exit_code = ConsoleCommandEvent.RETURN_CODE_DISABLED

  13  ~\AppData\Roaming\pypoetry\venv\Lib\site-packages\cleo\commands\base_command.py:119 in run
       117│         io.input.validate()
       118│
     → 119│         status_code = self.execute(io)
       120│
       121│         if status_code is None:

  12  ~\AppData\Roaming\pypoetry\venv\Lib\site-packages\cleo\commands\command.py:62 in execute
        60│
        61│         try:
     →  62│             return self.handle()
        63│         except KeyboardInterrupt:
        64│             return 1

  11  ~\AppData\Roaming\pypoetry\venv\Lib\site-packages\poetry\console\commands\debug\info.py:27 in handle
        25│         command = self.get_application().get("env info")
        26│
     →  27│         exit_code: int = command.run(self.io)
        28│         return exit_code
        29│

  10  ~\AppData\Roaming\pypoetry\venv\Lib\site-packages\cleo\commands\base_command.py:119 in run
       117│         io.input.validate()
       118│
     → 119│         status_code = self.execute(io)
       120│
       121│         if status_code is None:

   9  ~\AppData\Roaming\pypoetry\venv\Lib\site-packages\cleo\commands\command.py:62 in execute
        60│
        61│         try:
     →  62│             return self.handle()
        63│         except KeyboardInterrupt:
        64│             return 1

   8  ~\AppData\Roaming\pypoetry\venv\Lib\site-packages\poetry\console\commands\env\info.py:28 in handle
        26│         from poetry.utils.env import EnvManager
        27│
     →  28│         env = EnvManager(self.poetry).get()
        29│
        30│         if self.option("path"):

   7  ~\AppData\Roaming\pypoetry\venv\Lib\site-packages\poetry\utils\env\env_manager.py:236 in get
       234│             "virtualenvs.prefer-active-python"
       235│         )
     → 236│         python_minor = self.get_python_version(
       237│             precision=2, prefer_active_python=prefer_active_python, io=self._io
       238│         ).to_string()

   6  ~\AppData\Roaming\pypoetry\venv\Lib\site-packages\poetry\utils\env\env_manager.py:108 in get_python_version
       106│
       107│         if prefer_active_python:
     → 108│             executable = EnvManager._detect_active_python(io)
       109│
       110│             if executable:

   5  ~\AppData\Roaming\pypoetry\venv\Lib\site-packages\poetry\utils\env\env_manager.py:86 in _detect_active_python
        84│         )
        85│
     →  86│         executable = EnvManager._full_python_path("python")
        87│
        88│         if executable is not None:

   4  ~\AppData\Roaming\pypoetry\venv\Lib\site-packages\poetry\utils\env\env_manager.py:69 in _full_python_path
        67│
        68│         try:
     →  69│             executable = subprocess.check_output(
        70│                 [path_python, "-c", "import sys; print(sys.executable)"], text=True
        71│             ).strip()

   3  ~\.pyenv\pyenv-win\versions\3.12.0b4\Lib\subprocess.py:466 in check_output
        464│         kwargs['input'] = empty
        465│
     →  466│     return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
        467│                **kwargs).stdout
        468│

   2  ~\.pyenv\pyenv-win\versions\3.12.0b4\Lib\subprocess.py:548 in run
        546│         kwargs['stderr'] = PIPE
        547│
     →  548│     with Popen(*popenargs, **kwargs) as process:
        549│         try:
        550│             stdout, stderr = process.communicate(input, timeout=timeout)

   1  ~\.pyenv\pyenv-win\versions\3.12.0b4\Lib\subprocess.py:1026 in __init__
       1024│                             encoding=encoding, errors=errors)
       1025│
     → 1026│             self._execute_child(args, executable, preexec_fn, close_fds,
       1027│                                 pass_fds, cwd, env,
       1028│                                 startupinfo, creationflags, shell,

  OSError

  [WinError 193] %1 不是有效的 Win32 應用程式。

  at ~\.pyenv\pyenv-win\versions\3.12.0b4\Lib\subprocess.py:1538 in _execute_child
      1534│             sys.audit("subprocess.Popen", executable, args, cwd, env)
      1535│
      1536│             # Start the process
      1537│             try:
    → 1538│                 hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
      1539│                                          # no special security
      1540│                                          None, None,
      1541│                                          int(not close_fds),
      1542│                                          creationflags,

IvanaGyro avatar Jul 24 '23 09:07 IvanaGyro

It happens to me too, but is not only when virtualenvs.prefer-active-python is set to true.

for instance: Poetry version: 1.6.1 Python version: 3.12.0 OS version and name: Window 11 pyenv: 3.1.1 (installed when python 3.12.0 was configured as global in pyenv)

Changed to python 3.11.6

pyenv global 3.11.6 pyenv versions

  • 3.11.6 (set by %USERPROFILE%.pyenv\pyenv-win\version) 3.12.0

In a new folder run poetry new:

poetry new .

Then edit my newly generated pyproject.toml to use python ~3.11

[tool.poetry]
name = "test"
version = "0.1.0"
description = ""
authors = [""]
readme = "README.md"

[tool.poetry.dependencies]
python = "~3.11"

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

Then executing either poetry shell, poetry install or any command that tries to create the venv returns this:

The currently activated Python version 3.12.0 is not supported by the project (~3.11).
Trying to find and use a compatible version.

[WinError 193] %1 is not a valid Win32 application

poetry shell -vvv

Loading configuration file C:\Users\Fa\AppData\Roaming\pypoetry\config.toml
The currently activated Python version 3.12.0 is not supported by the project (~3.11).
Trying to find and use a compatible version.
Trying python3

  Stack trace:

  13  ~\AppData\Roaming\pypoetry\venv\Lib\site-packages\cleo\application.py:327 in run
       325│
       326│             try:
     → 327│                 exit_code = self._run(io)
       328│             except BrokenPipeError:
       329│                 # If we are piped to another process, it may close early and send a

  12  ~\AppData\Roaming\pypoetry\venv\Lib\site-packages\poetry\console\application.py:190 in _run
       188│         self._load_plugins(io)
       189│
     → 190│         exit_code: int = super()._run(io)
       191│         return exit_code
       192│

  11  ~\AppData\Roaming\pypoetry\venv\Lib\site-packages\cleo\application.py:431 in _run
       429│             io.input.interactive(interactive)
       430│
     → 431│         exit_code = self._run_command(command, io)
       432│         self._running_command = None
       433│

  10  ~\AppData\Roaming\pypoetry\venv\Lib\site-packages\cleo\application.py:473 in _run_command
       471│
       472│         if error is not None:
     → 473│             raise error
       474│
       475│         return terminate_event.exit_code

   9  ~\AppData\Roaming\pypoetry\venv\Lib\site-packages\cleo\application.py:454 in _run_command
       452│
       453│         try:
     → 454│             self._event_dispatcher.dispatch(command_event, COMMAND)
       455│
       456│             if command_event.command_should_run():

   8  ~\AppData\Roaming\pypoetry\venv\Lib\site-packages\cleo\events\event_dispatcher.py:26 in dispatch
        24│
        25│         if listeners:
     →  26│             self._do_dispatch(listeners, event_name, event)
        27│
        28│         return event

   7  ~\AppData\Roaming\pypoetry\venv\Lib\site-packages\cleo\events\event_dispatcher.py:89 in _do_dispatch
        87│                 break
        88│
     →  89│             listener(event, event_name, self)
        90│
        91│     def _sort_listeners(self, event_name: str) -> None:

   6  ~\AppData\Roaming\pypoetry\venv\Lib\site-packages\poetry\console\application.py:299 in configure_env
       297│
       298│         env_manager = EnvManager(poetry, io=io)
     → 299│         env = env_manager.create_venv()
       300│
       301│         if env.is_venv() and io.is_verbose():

   5  ~\AppData\Roaming\pypoetry\venv\Lib\site-packages\poetry\utils\env\env_manager.py:514 in create_venv
       512│                     self._io.write_error_line(f"Trying {python_name}")
       513│
     → 514│                 python = self._full_python_path(python_name)
       515│                 if python is None:
       516│                     continue

   4  ~\AppData\Roaming\pypoetry\venv\Lib\site-packages\poetry\utils\env\env_manager.py:69 in _full_python_path
        67│
        68│         try:
     →  69│             executable = subprocess.check_output(
        70│                 [path_python, "-c", "import sys; print(sys.executable)"], text=True
        71│             ).strip()

   3  ~\.pyenv\pyenv-win\versions\3.12.0\Lib\subprocess.py:466 in check_output
        464│         kwargs['input'] = empty
        465│
     →  466│     return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
        467│                **kwargs).stdout
        468│

   2  ~\.pyenv\pyenv-win\versions\3.12.0\Lib\subprocess.py:548 in run
        546│         kwargs['stderr'] = PIPE
        547│
     →  548│     with Popen(*popenargs, **kwargs) as process:
        549│         try:
        550│             stdout, stderr = process.communicate(input, timeout=timeout)

   1  ~\.pyenv\pyenv-win\versions\3.12.0\Lib\subprocess.py:1026 in __init__
       1024│                             encoding=encoding, errors=errors)
       1025│
     → 1026│             self._execute_child(args, executable, preexec_fn, close_fds,
       1027│                                 pass_fds, cwd, env,
       1028│                                 startupinfo, creationflags, shell,

  OSError

  [WinError 193] %1 is not a valid Win32 application

  at ~\.pyenv\pyenv-win\versions\3.12.0\Lib\subprocess.py:1538 in _execute_child
      1534│             sys.audit("subprocess.Popen", executable, args, cwd, env)
      1535│
      1536│             # Start the process
      1537│             try:
    → 1538│                 hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
      1539│                                          # no special security
      1540│                                          None, None,
      1541│                                          int(not close_fds),
      1542│                                          creationflags,

I have found a workaround if (having configured global in pyenv 3.11.6) execute:

poetry env use python.bat

Note that the extension .bat is added to the end of python

Creating virtualenv test in D:\Python\test\.venv
Using virtualenv: D:\Python\test\.venv

poetry env info

Virtualenv
Python:         3.11.6
Implementation: CPython
Path:           D:\Python\test\.venv
Executable:     D:\Python\test\.venv\Scripts\python.exe
Valid:          True

System
Platform:   win32
OS:         nt
Python:     3.11.6
Path:       C:\Users\**\.pyenv\pyenv-win\versions\3.11.6
Executable: C:\Users\**\.pyenv\pyenv-win\versions\3.11.6\python.exe

The location of python.bat and its contents are:

where python.bat

C:\Users\**\.pyenv\pyenv-win\shims\python.bat

Contents:

@echo off
chcp 1250 > NUL
call pyenv exec %~n0 %*

FabianToledo avatar Oct 22 '23 23:10 FabianToledo

if I were you I would put some print statements in EnvManager._full_python_path() to see what it is doing. The windows error is strangely unhelpful (what is %1?)

perhaps that method should catch OSError?

dimbleby avatar Oct 23 '23 07:10 dimbleby

I get the same error, I'm on Windows 10

franceschiniandrea avatar Nov 16 '23 01:11 franceschiniandrea

I also get the same error. I'm on Windows 11.

gsfaller avatar Mar 26 '24 12:03 gsfaller

Having the same issue as well on Windows 11.

tamireinhorn avatar Apr 01 '24 00:04 tamireinhorn

Folks, "I am also hitting this" does not make any progress.

One of you who is having a problem will need to put in some work to debug eg https://github.com/python-poetry/poetry/issues/8229#issuecomment-1774626721

dimbleby avatar Apr 01 '24 09:04 dimbleby

Having the same issue as well on Windows 11.

rfaels avatar Apr 22 '24 11:04 rfaels

I was hitting this also, but to progress this along a bit ;) let me leave a bit more info:

Virtualenv
Python:         3.12.0
Implementation: CPython
Path:           C:\Users\x\AppData\Local\pypoetry\Cache\virtualenvs\x-O5vkSaiN-py3.12
Executable:     C:\Users\x\AppData\Local\pypoetry\Cache\virtualenvs\x-O5vkSaiN-py3.12\Scripts\python.exe
Valid:          True

Base
Platform:   win32
OS:         nt
Python:     3.12.0
Path:       C:\Users\x\.conda\envs\pipx
Executable: C:\Users\x\.conda\envs\pipx\python.exe

I was getting this cryptic [WinError 193] %1 is not a valid Win32 application. error in association with trying to switch the Python interpreter (poetry env use 3.10) to a version I know is installed but it seems poetry was struggling to find it. I solved it by first deleting the poetry environment (poetry env list and then poetry env remove x-O5vkSaiN-py3.12), then running poetry env use "C:\Users\x\.pyenv\pyenv-win\versions\3.10.11\python.exe" - i.e. using the full path. I had to delete the environment because I was running into an circular dependency catch-22 where it was refusing to let me change the version because I didn't have a valid version in my pyproject.toml - or maybe I misunderstood something, but either way deleting it before switching the python executable worked for me.

To the best of my knowledge, WinError 193 occurs when an attempt to execute a non-executable file is made, and given that my problem was resolved by more explicitly pointing it to a correct python.exe, I think this issue is from failing to resolve to the correct python interpreter. The %1 hints that the executable path/filename provided wasn't valid (first argument to spawn a process), so I imagine the error message occurred for the OP because the behavior virtualenvs.prefer-active-python enables fails to point to fill in a valid value for that argument.

I'd encourage everyone running into this issue to try find the python.exe they explicitly want to use and then using poetry env use "<path>\python.exe", rather than relying on any built-in python resolution mechanisms.

the-xentropy avatar Aug 06 '24 19:08 the-xentropy

As an aside, it took me embarrassingly long to realize that I could use an absolute path for the python executable in 'env use'.

Description:
  Activates or creates a new virtualenv for the current project.

Usage:
  env use [options] [--] <python>

Arguments:
  python                     The python executable to use.

Part of the confusion is because in documentation online values like 3.12 are valid values for the python argument but this doesn't map to any actual executable on my system, so I naturally assumed it was a more ambiguous definition of 'executable'. Could we perhaps change the documentation to e.g:

Description:
  Activates or creates a new virtualenv for the current project.

Usage:
  env use [options] [--] <python>

Arguments:
  python                     The python executable to use. Accepts an absolute path, or a version string.
                                   If version string is used, poetry will try but may fail to find the corresponding version.

the-xentropy avatar Aug 06 '24 19:08 the-xentropy