Python3 Coding errors under Linux

Source: Internet
Author: User

' ASCII ' codec can't encode characters in position 162-165:ordinal not in range

aaa.py file is already set

#!/usr/bin/env Python3
# coding = ' Utf-8 '

But execution always gets an error.

' ASCII ' codec can ' t encode characters in position 162-165:ordinal not in range (128)

After the online query this kind of error is basically 2.7 of the problem in the environment, through the following methods to solve.
    1. # Encoding=utf8
    2. Import Sys
    3. Reload (SYS)
    4. Sys.setdefaultencoding (' UTF8 ')

But why am I always wrong in the PYTHON3 environment?

By modifying the code in the codec Chinese and other operations are not valid, toss half a day to consider whether there is a Linux language environment variable problem

Check the current shell environment

$ echo $SHELL

Csh

Check locale variables

$ env |grep LANG
Lang=zh

Change the language environment variable to

Setenv LANG en_US. UTF-8

Problem solving

Python3 Coding errors under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.