Skip to content

Instantly share code, notes, and snippets.

@embayer
Last active October 30, 2020 06:21
Show Gist options
  • Select an option

  • Save embayer/2774afb51b188dc53ed8 to your computer and use it in GitHub Desktop.

Select an option

Save embayer/2774afb51b188dc53ed8 to your computer and use it in GitHub Desktop.
set python default encoding to utf-8

change default encoding

cd ~/.virtualenvs/myvirtualenv/lib/python2.x/site-packages
echo 'import sys;sys.setdefaultencoding("utf-8")' > sitecustomize.py

set default encoding for each new virtualenv

to automatically create a sitecustomize.py every time you create a virtualenv, edit your

~/.virtualenvs/postmkvirtualenv:

#!/bin/bash
# This hook is run after a new virtualenv is activated.
PY_VERSION=`ls $VIRTUAL_ENV/lib/`
echo 'import sys;sys.setdefaultencoding("utf-8")' > $VIRTUAL_ENV/lib/$PY_VERSION/site-packages/sitecustomize.py

Set the encoding in your Python file

# -*- coding: utf-8 -*-
@axgkl

axgkl commented Oct 30, 2020

Copy link
Copy Markdown

pip install setdefaultencoding

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment