Search code examples
pythonvirtualenvsyspython-poetry

Fatal Python error: Py_Initialize: can't initialize sys standard streams


I have a pyproject.toml file that was created using poetry, and I'm trying to run the command poetry install in this directory to create a poetry.lock file. However, when I run poetry install, I get the following error:

  EnvCommandError

  Command C:\Users\myName\AppData\Local\pypoetry\Cache\virtualenvs\my-app-name-kS94etse-py3.8\Scripts\python.exe - 
  errored with the following return code 3, and output:
      Fatal Python error: Py_Initialize: can't initialize sys standard streams
      Traceback (most recent call last):
        File "c:\users\myName\anaconda3\lib\io.py", line 52, in <module>
        File "c:\users\myName\anaconda3\lib\abc.py", line 106
          print(f"Class: {cls.__module__}.{cls.__qualname__}", file=file)
                                                            ^
      SyntaxError: invalid syntax
      input was : import sys

This error seems pretty in depth but I dont understand why poetry thinks import sys is invalid syntax. I went to the directory it's throwing the error at, C:\Users\myName\AppData\Local\pypoetry\Cache\virtualenvs\my-app-name-kS94etse-py3.8\Scripts\python.exe and even just trying to run the python.exe there I get something similar:

Fatal Python error: Py_Initialize: can't initialize sys standard streams
Traceback (most recent call last):
  File "c:\users\myName\anaconda3\lib\io.py", line 52, in <module>
  File "c:\users\myName\anaconda3\lib\abc.py", line 106
    print(f"Class: {cls.__module__}.{cls.__qualname__}", file=file)
                                                      ^
SyntaxError: invalid syntax

Any suggestions on how to fix this?


Solution

  • Ended up resolving my own issue. For some reason my virtual environment created by poetry is copying/downloading a broken version of Python, which as I said in my initial question, it seemed the python.exe alone was what is throwing the fatal error.

    With that, I resolved my issue by:

    1. Downloading a new, non-broken version of Python (the same version that I defined in my pyproject.toml file to use): https://www.python.org/downloads/release/python-385/
    2. Navigated to the virtual env folder poetry told me the error was coming from: C:\Users\myName\AppData\Local\pypoetry\Cache\virtualenvs\my-app-name-kS94etse-py3.8\Scripts\python.exe
    3. Replaced the python.exe currently in that folder with the new one I just downloaded

    I'm sure long-run I'll need to find why this is occurring, but this is a quick fix for anyone else running into this issue. Happy poetry-ing!