简体   繁体   中英

python unable to start in virtual environment in VS code: UnicodeDecodeError: 'charmap' codec can't decode byte

I recently used windows' virtual environment to set up the environment in order to run PyTorch deep learning code.

Starting from the very simplistic code in PyTorch's official tutorial, I managed to run the code for the first time. However, after my first running and executing "python" in command line, it shows:

(venv) PS C:\Users\xxx\demo_pytorch\venv> python
Fatal Python error: initsite: Failed to import the site module
Traceback (most recent call last):
  File "C:\Users\xxx\Miniconda3\lib\site.py", line 579, in <module>
    main()
  File "C:\Users\xxx\Miniconda3\lib\site.py", line 562, in main
    known_paths = venv(known_paths)
  File "C:\Users\xxx\Miniconda3\lib\site.py", line 494, in venv
    addsitepackages(known_paths, [sys.prefix])
  File "C:\Users\xxx\Miniconda3\lib\site.py", line 349, in addsitepackages
    addsitedir(sitedir, known_paths)
  File "C:\Users\xxx\Miniconda3\lib\site.py", line 207, in addsitedir
    addpackage(sitedir, name, known_paths)
  File "C:\Users\xxx\Miniconda3\lib\site.py", line 163, in addpackage
    for n, line in enumerate(f):
  File "C:\Users\xxx\Miniconda3\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 654: character maps to <undefined>

Can anyone tell me how to fix this? The default python in the VS code has been set as.\Scripts\python.exe

It seems like the file encoding caused this. If you have code:

file = open(filename)

you need to change it to:

file = open(filename, encoding="utf8")

you can refer to this page or another page for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM