Search code examples
pythonmsys2

Resolving errors in python environment on Windows


I just started having some problems with python, and would like to fix this the proper way, as a last resort, reinstalling if I have to.

Before Python 3, I had Python 2.7 installed. I then installed Python 3.7 along side it. I don't remember, if the path environment was automatically set, or if i manually set it. My system variables path contains :

C:\Python27\;C:\Python27\Scripts;

So I believe this was set automatically. My user variables path however contains :

C:\Python37\;C:\Python37\Scripts;

Would the python installer use the user environment? I'm not sure, so I don't know if I set that myself.

However, the user variables path also contains :

%PYTHON_DIR%\Python37\Scripts\;%PYTHON_DIR%\Python37\;%PYTHON_DIR%\Python36\Scripts\;%PYTHON_DIR%\Python36\

Again, I don't know if I set this myself, and I have no recollection of setting this in my user variables :

PYTHON_DIR = C:\Users\pcuser\AppData\Local\Programs\Python

...but it's there, and I have two folders in that path. enter image description here

I also have this in my user variables path :

PYTHON = os.path.expanduser(os.getenv('PYTHON', 'C:\\Python37\\python.exe'))

I know I created a python environment. I just have to look for it, as I don't remember where to find it right now.

Here is the problem I am having. I also am using Python in MSYS, and get this error from Python in C:\msys64\usr\bin :

Could not find platform independent libraries <prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: initfsencoding: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

Current thread 0x0000000800018040 (most recent call first):
      0 [main] python 1489 cygwin_exception::open_stackdumpfile: Dumping stack t
race to python.exe.stackdump

I set PYTHONHOME to C:\msys64\usr\bin. That doesn't solve the problem. I also removed PYTHON_DIR to see if there was a conflict there, but that didn't solve the problem. Rather, I got another error :

Fatal Python error: initfsencoding: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

Current thread 0x0000000800018040 (most recent call first):
      0 [main] python 143 cygwin_exception::open_stackdumpfile: Dumping stack tr
ace to python.exe.stackdump

So it seems to me my Python environment is messed up, and I want to learn how to set it up correctly, hopefully without having to start over. How can I get rid of these errors, and get a proper python environment?


Solution

  • After a while of troubleshooting, I was able to discover where the problem was. Some files in C:\msys64\usr\lib\python3.8 somehow got deleted, or python 3.8 was installed, but not properly. So I did a reinstall, and python stopped complaining about missing encodings and modules.

    Running a test on all python installations

    MINGW64 ~
    # python
    Python 3.8.2 (default, Feb 27 2020, 05:27:33)  [GCC 9.2.0 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    
    # python setup_build.py install
    running install
    running build
    running build_ext
    skipping 'geotools/geotools.c' Cython extension (up-to-date)
    running install_lib
    copying build/lib.mingw-3.8/geotools-cpython-38.dll -> C:/msys64/mingw64/lib/python3.8/site-packages
    running install_egg_info
    Writing C:/msys64/mingw64/lib/python3.8/site-packages/geotools-0.1.0-py3.8.egg-info
    
    MINGW32 ~
    # python
    Python 3.8.2 (default, Feb 27 2020, 06:39:26)  [GCC 9.2.0 32 bit] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    
    # python setup_build.py install
    running install
    running build
    running build_ext
    dllwrap: WARNING: dllwrap is deprecated, use gcc -shared or ld -shared instead
    
    skipping 'geotools/geotools.c' Cython extension (up-to-date)
    running install_lib
    running install_egg_info
    Removing C:/msys64/mingw32/lib/python3.8/site-packages/geotools-0.1.0-py3.8.egg-info
    Writing C:/msys64/mingw32/lib/python3.8/site-packages/geotools-0.1.0-py3.8.egg-info
    
    C:\WINDOWS\system32>py -3.7 -m pip list
    Package                       Version
    ----------------------------- ---------
    ...
    
    C:\WINDOWS\system32>py -2.7 -m pip list
    DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please
     upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop s
    upport for Python 2.7 in January 2021. More details about Python 2 support in pi
    p can be found at https://pip.pypa.io/en/latest/development/release-process/#pyt
    hon-2-support pip 21.0 will remove support for this functionality.
    Package                       Version
    ----------------------------- ----------
    ...
    
    C:\WINDOWS\system32>python
    Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:58) [MSC v.1500 64 bit (AM
    D64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys
    >>> sys.path
    ['', 'C:\\WINDOWS\\SYSTEM32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\
    \lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27
    ', 'C:\\Users\\pcUser\\AppData\\Roaming\\Python\\Python27\\site-packages', 'C:\\Py
    thon27\\lib\\site-packages', 'C:\\Python27\\lib\\site-packages\\pybind11-2.6.2-p
    y2.7.egg']
    >>> 
    
    C:\WINDOWS\system32>py -3
    Python 3.7.7 (tags/v3.7.7:d7c567b08f, Mar 10 2020, 10:41:24) [MSC v.1900 64 bit
    (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys
    >>> sys.path
    ['', 'C:\\Users\\pcUser\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip'
    , 'C:\\Users\\pcUser\\AppData\\Local\\Programs\\Python\\Python37\\DLLs', 'C:\\User
    s\\pcUser\\AppData\\Local\\Programs\\Python\\Python37\\lib', 'C:\\Users\\pcUser\\App
    Data\\Local\\Programs\\Python\\Python37', 'C:\\Users\\pcUser\\AppData\\Roaming\\Py
    thon\\Python37\\site-packages', 'C:\\Users\\pcUser\\AppData\\Local\\Programs\\Pyth
    on\\Python37\\lib\\site-packages']
    >>>
    
    C:\WINDOWS\system32>py -3.6 -m pip list
    Package         Version
    --------------- --------
    ...
    

    My python environment is good. All working nicely. :)

    What I learned... Using PYTHONHOME and PYTHONPATH is not neccesary, as was suggested by many... although equally suggessted by many. From my experience - speaking fron a novice perspective - I would not recommend using them. Maybe they work for others, but I got errors regardless of how I set it up - whether C:\Python37 or C:\Python37\lib or C:\Users\pcUser\AppData\Local\Programs\Python\Python37 or C:\msys64\usr\lib\python3.8;C:\msys64\usr\lib\python3.8\site-packages;C:\msys64\usr\lib\python3.8\lib-dynload;C:\msys64\usr\lib\python3.8\distutils

    They all return a heap of errors on one installment or other. So it does not work for me. However, probably works for others. I am happy though that after probably more than 60 hours, my python environment is back in working order. :)