Skip to content

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

Closed

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

Activity

  1. abn commented on Jul 22, 2020

    @abn
    Member

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

  2. moochannel commented on Jul 22, 2020

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

  3. abn commented on Jul 22, 2020

    @abn
    Member

    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.

  4. linked a pull request that will close this issueStandardise on using virtualenv package #2666on Jul 22, 2020
  5. moochannel commented on Jul 22, 2020

    @moochannel
    Author

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

  6. intelligent174 commented on Aug 20, 2020

    @intelligent174

    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'
    
  7. jonapich commented on Aug 25, 2020

    @jonapich
    Contributor

    @intelligent174 upgrade to the pre-release version:

    pip install --upgrade poetry --pre
    
  8. rcoulter13 commented on Aug 25, 2020

    @rcoulter13

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

  9. jonapich commented on Aug 25, 2020

    @jonapich
    Contributor

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

  10. rcoulter13 commented on Aug 26, 2020

    @rcoulter13

    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.

  11. rcoulter13 commented on Aug 26, 2020

    @rcoulter13

    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!

  12. github-actions commented on Mar 3, 2024

    @github-actions

    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.

  13. locked as resolved and limited conversation to collaborators on Mar 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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