Skip to content

Instantly share code, notes, and snippets.

@emzo
Forked from embayer/pythonUtf8.md
Created October 7, 2019 08:56
Show Gist options
  • Save emzo/9071880de6a464bfee6c69c254e3e99b to your computer and use it in GitHub Desktop.
Save emzo/9071880de6a464bfee6c69c254e3e99b 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 -*-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment