Skip to content

poetry env use fails with NameError: name 'WindowsPath' is not defined #2698

@moochannel

Description

@moochannel
  • 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

When I run poetry env use "C:\Program Files\Python37\python.exe" in a directory without a virtual environment, it fails with NameError: name 'WindowsPath' is not defined.

(In empty directory C:\Users\xxxx\poetrytest)
> poetry init
> poetry env use "C:\Program Files\Python37\python.exe"

Creating virtualenv poetrytest in C:\Users\xxxx\poetrytest\.venv
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'WindowsPath' is not defined

[EnvCommandError]
Command ['python', '-'] errored with the following return code 1, and output:
'python' is not recognized as an internal or external command,
operable program or batch file.
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)

exit status 1

CREATE_VENV_COMMAND.format(path)) in EnvManager.build_venv converts path to WindowsPath(path), but CREATE_VENV_COMMAND doesn't contain pathlib.WindowsPath. So it results in an error.

My workaround here. I suppose the path should be converted from WindowsPath to str before it passed to CREATE_VENV_COMMAND.format(path)).

--- env.py.bak  2020-07-22 06:30:05.245529100 +0900
+++ env.py      2020-07-22 14:20:24.012698600 +0900
@@ -669,7 +669,7 @@
                     stdin=subprocess.PIPE,
                     shell=True,
                 )
-                p.communicate(encode(CREATE_VENV_COMMAND.format(path)))
+                p.communicate(encode(CREATE_VENV_COMMAND.format(str(path))))
             except CalledProcessError as e:
                 raise EnvCommandError(e)

After patching this, the command runs fine.

> poetry env use "C:\Program Files\Python37\python.exe"
Creating virtualenv poetrytest in C:\Users\xxxx\poetrytest\.venv
Using virtualenv: C:\Users\xxxx\poetrytest\.venv

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugSomething isn't working as expected

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions