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

Created on 22 Jul 2020  路  10Comments  路  Source: python-poetry/poetry

  • [x] I am on the latest Poetry version.
  • [x] I have searched the issues of this repo and believe that this is not a duplicate.
  • [x] 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
Bug

All 10 comments

@moochannel can you try the develop branch please? This shouldn't be an issue anymore.

@abn Thank you for your quick reply.
I tried the develop branch with pip install git+https://github.com/python-poetry/poetry.git@develop.
The problem has been gone馃槈

Since poetry -V shows as 1.1.0a3 in the develop branch, I think this will be fixed in the next 1.1.0x release. Will this change apply to 1.0.x?

Perfect. This will be released with the next 1.1.0 pre-release. As for this being ported to 1.0.x, unfortunately 1.0.10 was the last planned release. However, the stable release for 1.1.0 will be coming soon anyway.

Understood. I'll wait for the 1.1.0 release.
Closing.

Hello! I decided to start using poetry, but faced such a problem. Tell me what's wrong? Thank you in advance!

D:\flexites\project> poetry env use D:\Python38\python.exe
Creating virtualenv project-ZWWETv50-py3.8 in C:\Users\admin\AppData\Local\pypoetry\Cache\virtualenvs
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'WindowsPath' is not defined

[FileNotFoundError]
[Errno 2] No such file or directory: 'C:\\Users\\admin\\AppData\\Local\\pypoetry\\Cache\\virtualenvs\\envs.toml'

@intelligent174 upgrade to the pre-release version:

pip install --upgrade poetry --pre

@jonapich Hello! I attempted your solution but am still getting the same results as @intelligent174 . I checked the directory it is attempting to find and it is accurate. There is no pypoetry file in that location. Does this mean the original download was flawed and should be uninstalled and re-downloaded? I appreciate any help you have to offer.

"poetry --version" should give you something like 1.1.0b1 or something. Not 1.0.10 that's where the bug occurs.

Unfortunately mine is not doing that. After running the pip install you provided to upgrade to the pre-release version, I still have 1.0.10. I must have done something wrong but I'm not sure what.

Followup: the newest version requires the syntax "poetry self update --preview" in order to work, but even at that it denies permission even if PowerShell runs with administrator access. Every time it gets really close to upgrading but it always runs into a file where access is denied. I'll keep exploring but so far it looks like uninstalling the current version of poetry and then downloading the desired version might be the best workaround. If anyone knows of a better solution please let me know. Thanks!

Was this page helpful?
0 / 5 - 0 ratings