1

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

  • Check your local `.vscode/settings.json`, it should have a key-value pair called pythonPath, the value of which should be like `env_name\\Scripts\\python.exe`. You don't really need to change the default python in VS Code. – agupta Jun 19 '20 at 04:47
  • I checked it. It is correct. Otherwise, the first debugging would be wrong. – complexfilter Jun 19 '20 at 17:10
  • Do you have Unicode characters in your path? That error would come up because you're trying to feed Python text which can't be decoded during startup. – Brett Cannon Jun 22 '20 at 20:47

1 Answers1

1

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.

Steven-MSFT
  • 7,438
  • 1
  • 5
  • 13