Solution for Windows Git Bash Conda Activation Error 'UnicodeDecodeError: gbk codec can't decode byte'

2023-09-30
#python, #conda
290 Words
2 min

Background

Starting graduate school, I brought a new Windows computer. This Windows computer had no environments installed—even Anaconda was only installed after school started.

During the Mid-Autumn and National Day holidays, I planned to back up the photos from the sm.ms image hosting service. During undergraduate, I would back up sm.ms every month.

So I pulled my sm.ms Python script from my GitHub repository, created a new Python environment with conda. When I tried to activate that Python environment, the terminal showed an error: “UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xaf in position 271: illegal multibyte sequence”.

conda activation error

Clearly an encoding issue.

Solution

After some searching and attempts, I solved the error by adding the following line to the .condarc file in the C drive user directory (create the file if it doesn’t exist):

1
2
# Tell Python interpreter to use UTF-8 encoding by default when processing text data
export PYTHONUTF8=1

GPT-4’s explanation of this line:

GPT-4 explanation

Successfully activated Python environment. (The image below shows exporting in git bash terminal, which is temporary. For permanent effect, it’s recommended to modify the .condarc configuration file)

Successfully activated Python environment

Now I can happily back up my image hosting pictures.

Supplement

Initially, I changed the Windows “Language for non-Unicode programs” setting to English (United States), which also solved the encoding issue. But I found the PicGo software went completely black screen. When I changed it back to Chinese (Simplified, Singapore), PicGo no longer had the black screen issue.

Change Windows “Language for non-Unicode programs” setting


Emoji Reaction


© 2022-2026 Made with ❤️ By Jiakai