Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
3 tasks done
moochannel opened this issue Jul 22, 2020 · 11 comments · Fixed by #2666
Closed
3 tasks done

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

moochannel opened this issue Jul 22, 2020 · 11 comments · Fixed by #2666
Labels
kind/bug Something isn't working as expected

Comments

@moochannel
Copy link

  • 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
@moochannel moochannel added kind/bug Something isn't working as expected status/triage This issue needs to be triaged labels Jul 22, 2020
@abn
Copy link
Member

abn commented Jul 22, 2020

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

@moochannel
Copy link
Author

@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?

@abn
Copy link
Member

abn commented Jul 22, 2020

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.

@abn abn linked a pull request Jul 22, 2020 that will close this issue
1 task
@moochannel
Copy link
Author

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

@intelligent174
Copy link

intelligent174 commented Aug 20, 2020

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'

@jonapich
Copy link
Contributor

@intelligent174 upgrade to the pre-release version:

pip install --upgrade poetry --pre

@rcoulter13
Copy link

@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.

@jonapich
Copy link
Contributor

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

@rcoulter13
Copy link

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.

@rcoulter13
Copy link

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!

@abn abn removed the status/triage This issue needs to be triaged label Sep 25, 2020
Copy link

github-actions bot commented Mar 3, 2024

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind/bug Something isn't working as expected
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants