poetry: ``poetry new`` fails when creating a new system environment

  • 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).
  • OS version and name: Windows 10 Version 21H1 (OS Build 19043.1415)
  • Poetry version: 1.1.12
  • Python version: Python 3.9.9 (installed via Windows store)
  • Link of a Gist with the contents of your pyproject.toml file: N/A

Issue

When I run poetry new {name}, i get the following OSError (full traceback from poetry new {name} -vvv here:

  OSError

  [WinError 1920] The file cannot be accessed by the system: 'C:\\Users\\yngve.moe\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\\python.exe\\Scripts'

  at C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2544.0_x64__qbz5n2kfra8p0\lib\pathlib.py:1232 in stat
      1228│
      1229│         Return the result of the stat() system call on this path, like
      1230│         os.stat() does.
      1231│         """
    → 1232│         return self._accessor.stat(self)
      1233│
      1234│     def owner(self):
      1235│
      1236│         Return the login name of the file owner.

I’ve done some debugging, and it seems like the bug is due to the following lines (at time of writing, commit e34277e): This line creates a new system environment with the path to the python.exe executable (C:\\Users\\yngve.moe\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\\python.exe) https://github.com/python-poetry/poetry/blob/e34277e24026d80bc88898a5710183383686962f/src/poetry/console/commands/new.py#L66

Then, the self._bin_dir is set to the path to the Python executable (not its parent directory) https://github.com/python-poetry/poetry/blob/e34277e24026d80bc88898a5710183383686962f/src/poetry/utils/env.py#L1082

The next problem arises during this function call: https://github.com/python-poetry/poetry/blob/e34277e24026d80bc88898a5710183383686962f/src/poetry/utils/env.py#L1087

Specifically, it happens on this line, since self._bin_dir.glob is the path to the Python executable, not its parent directory. https://github.com/python-poetry/poetry/blob/e34277e24026d80bc88898a5710183383686962f/src/poetry/utils/env.py#L1139

I am able to circumvent this problem locally by modifying https://github.com/python-poetry/poetry/blob/e34277e24026d80bc88898a5710183383686962f/src/poetry/console/commands/new.py#L66 to be

    current_env = SystemEnv(Path(sys.executable).parent) 

EDIT: I am relatively certain that there is something with my Windows setup that leads to this problem, since I have been able to get Poetry to work with Miniconda (which I have uninstalled now) on the same computer before, but I’ll leave this issue open since it should work with the installation from Windows store too.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 16 (1 by maintainers)

Commits related to this issue

Most upvoted comments

I uninstalled Python from Windows store, and reinstalled it via the installer I downloaded from https://www.python.org/downloads/release/python-3102/, then the issue resolved. I suspect it has something with the Windows store version of Python.

Same problem here, with poetry init This generally seems to happen when you install python via the Windows Store installer. In my case installing with the python.org installer is a little tricky because I’m not an adminstrator on the machine in question

poetry/src/poetry/utils/env.py I think it is because SystemEnv calls the Env constructor wich appends /Scripts to the path it gets via sys.executable on Windows. The problem is the resulting path just does not exist. There is no Scripts directory.

This seems to work for the python.org installer but the Store installer seems to layout the directory structure a little differently.

After some debugging. I find the reason is that pathlib.Path of ms_python_path throws OSError while official_python_path return expected False.

C:\Users\Liu.D.H>python
Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from pathlib import Path
>>> ms_python_path=Path('C:/Users/Liu.D.H/AppData/Local/Microsoft/WindowsApps/PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0/python.exe/Scripts')
>>> ms_python_path.is_dir()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Liu.D.H\AppData\Local\Programs\Python\Python310\lib\pathlib.py", line 1303, in is_dir
    return S_ISDIR(self.stat().st_mode)
  File "C:\Users\Liu.D.H\AppData\Local\Programs\Python\Python310\lib\pathlib.py", line 1095, in stat
    return self._accessor.stat(self, follow_symlinks=follow_symlinks)
OSError: [WinError 1920] The file cannot be accessed by the system: 'C:\\Users\\Liu.D.H\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\python.exe\\Scripts'
>>> official_python_path=Path(r'C:\Users\Liu.D.H\AppData\Local\Programs\Python\Python310\python.exe\Scripts')
>>> official_python_path.is_dir()
False
>>>

The related code is in https://github.com/python/cpython/blob/v3.10.2/Lib/pathlib.py#L405, https://github.com/python/cpython/blob/v3.10.2/Lib/pathlib.py#L1303, https://github.com/python/cpython/blob/v3.10.2/Lib/pathlib.py#L1095, https://github.com/python-poetry/poetry/blob/1.1.13/poetry/utils/env.py#L929-L935, https://github.com/python-poetry/poetry/blob/1.1.13/poetry/utils/env.py#L1092.

C:\Users\Liu.D.H\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0 is a special directory which contains a lot zero sized python file, maybe called App execution Alias.

image

C:\Users\Liu.D.H\AppData\Local\Programs\Python\Python310 is an ordinary python installation folder structure.

image

If I use C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.752.0_x64__qbz5n2kfra8p0 instead of C:\Users\Liu.D.H\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0, maybe it will works. But I have encountered another problem.

image

image