Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the PYTHONUTF8 environment variable to enable UTF-8 encoding by default in Python?

Python 3.7 introduced the PYTHONUTF8 environment variable to enable UTF-8 encoding by default. How do I set this variable from within a Python program? (I can't find it in my operating system's list of environment variables.)

like image 347
cosmarchia Avatar asked Jun 19 '18 16:06

cosmarchia


People also ask

Does Python use UTF-8 by default?

UTF-8 is one of the most commonly used encodings, and Python often defaults to using it.

How do I set an environment variable in Python?

To permanently modify the default environment variables, click Start and search for 'edit environment variables', or open System properties, Advanced system settings and click the Environment Variables button. In this dialog, you can add or modify User and System variables.

Why is UTF-8 a good choice for the default editor encoding in Python?

As a content author or developer, you should nowadays always choose the UTF-8 character encoding for your content or data. This Unicode encoding is a good choice because you can use a single character encoding to handle any character you are likely to need. This greatly simplifies things.


1 Answers

Usually you would specify this with a command line argument

python3.7 -X utf8

If you want to enable UTF-8 mode from environment variable:

export PYTHONUTF8=1  # linux / macOS
set PYTHONUTF8=1  # windows

It should be set before entering the Python runtime.

like image 171
wim Avatar answered Oct 14 '22 04:10

wim