axiros / terminal_markdown_viewer

Styled Terminal Markdown Viewer
Other
1.81k stars 106 forks source link

Installation failed on windows of python 3.8 #89

Closed liudonghua123 closed 2 years ago

liudonghua123 commented 3 years ago

I tried to install mdv in my Windows, the version of python is 3.8.6. Then it failed with the following errors.

D:\code\weixin\emp-ide>which pip
/c/Users/Liu.D.H/AppData/Local/Microsoft/WindowsApps/pip

D:\code\weixin\emp-ide>which python
/c/Users/Liu.D.H/AppData/Local/Microsoft/WindowsApps/python

D:\code\weixin\emp-ide>pip install mdv
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting mdv
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/70/6d/831e188f8079c9793eac4f62ae55d04a93d90979fd2d8271113687605380/mdv-1.7.4.tar.gz (54 kB)
     |████████████████████████████████| 54 kB 318 kB/s
    ERROR: Command errored out with exit status 1:
     command: 'C:\Users\Liu.D.H\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\python.exe' -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\Liu.D.H\\AppData\\Local\\Temp\\pip-install-ut58pvn5\\mdv\\setup.py'"'"'; __file__='"'"'C:\\Users\\Liu.D.H\\AppData\\Local\\Temp\\pip-install-ut58pvn5\\mdv\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base 'C:\Users\Liu.D.H\AppData\Local\Temp\pip-pip-egg-info-0f_vidqt'
         cwd: C:\Users\Liu.D.H\AppData\Local\Temp\pip-install-ut58pvn5\mdv\
    Complete output (5 lines):
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\Liu.D.H\AppData\Local\Temp\pip-install-ut58pvn5\mdv\setup.py", line 20, in <module>
        md = fd.read()
    UnicodeDecodeError: 'gbk' codec can't decode byte 0x82 in position 12007: illegal multibyte sequence
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
WARNING: You are using pip version 20.2.4; however, version 20.3.3 is available.
You should consider upgrading via the 'C:\Users\Liu.D.H\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\python.exe -m pip install --upgrade pip' command.

D:\code\weixin\emp-ide>

The key error seems to be UnicodeDecodeError: 'gbk' codec can't decode byte 0x82 in position 12007: illegal multibyte sequence.

The current chcp is 936, I also tried chcp 65001, however, it failed with the same errors.

liudonghua123 commented 3 years ago

I checked the code, and find it's maybe a problem with the default encoding of python 3 on windows. And even I set PYTHONIOENCODING, it would not work as expected.

A quick fix would be to set encoding (utf-8) of open in setup.py.

https://github.com/axiros/terminal_markdown_viewer/blob/80f333ba51dc2f1dfa854e203d3374a112aecbd3/setup.py#L19-L20

D:\code\weixin\emp-ide>python -c "import sys, os, locale; print(os.environ.get('PYTHONIOENCODING')); print(sys.stdin.encoding); print(sys.stdout.encoding); print(locale.getdefaultlocale()); print(locale.getpreferredencoding())"
None
utf-8
utf-8
('en_US', 'cp936')
cp936

D:\code\weixin\emp-ide>set PYTHONIOENCODING=utf-8

D:\code\weixin\emp-ide>python -c "import sys, os, locale; print(os.environ.get('PYTHONIOENCODING')); print(sys.stdin.encoding); print(sys.stdout.encoding); print(locale.getdefaultlocale()); print(locale.getpreferredencoding())"
utf-8
utf-8
utf-8
('en_US', 'cp936')
cp936

D:\code\weixin\emp-ide>

see also https://github.com/ipython/ipython/issues/10011, and the actual implementation of _locale._getdefaultlocale.

liudonghua123 commented 3 years ago

I also tried to install mdv programmatically but failed too.

C:\Users\Liu.D.H>python
Python 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:52:53) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import _locale
>>> _locale._getdefaultlocale = (lambda *args: ['zh_CN', 'utf-8'])
>>> import sys,os,locale
>>> print(os.environ.get('PYTHONIOENCODING')); print(sys.stdin.encoding); print(sys.stdout.encoding); print(locale.getdefaultlocale()); print(locale.getpreferredencoding())
utf-8
utf-8
utf-8
('zh_CN', 'utf-8')
utf-8
>>> import pip
>>> pip.main(['install','mdv'])
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting mdv
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/70/6d/831e188f8079c9793eac4f62ae55d04a93d90979fd2d8271113687605380/mdv-1.7.4.tar.gz (54 kB)
    ERROR: Command errored out with exit status 1:
     command: 'C:\Users\Liu.D.H\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\python.exe' -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\Liu.D.H\\AppData\\Local\\Temp\\pip-install-81aeum8n\\mdv\\setup.py'"'"'; __file__='"'"'C:\\Users\\Liu.D.H\\AppData\\Local\\Temp\\pip-install-81aeum8n\\mdv\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base 'C:\Users\Liu.D.H\AppData\Local\Temp\pip-pip-egg-info-xx5mibv6'
         cwd: C:\Users\Liu.D.H\AppData\Local\Temp\pip-install-81aeum8n\mdv\
    Complete output (5 lines):
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\Liu.D.H\AppData\Local\Temp\pip-install-81aeum8n\mdv\setup.py", line 20, in <module>
        md = fd.read()
    UnicodeDecodeError: 'gbk' codec can't decode byte 0x82 in position 12007: illegal multibyte sequence
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
WARNING: You are using pip version 20.2.4; however, version 20.3.3 is available.
You should consider upgrading via the 'C:\Users\Liu.D.H\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\python.exe -m pip install --upgrade pip' command.
1
>>>
DingJunyao commented 2 years ago

103

Maybe I solved it, you can try it.

liudonghua123 commented 2 years ago

I checked Beta: Use Unicode UTF-8 for worldwide language support of Region Settings temporarily. And then the installation is successful.

image