报错代码
#第一个python程序
print("hello worcld")
报错原因:
SyntaxError: Non-ASCII character '\xe7' in file /Users/haihong/代码/python/helloworld/hello.py on line 2, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
原因
python的默认编码文件是ASCII码,而在自己的python文件中使用了中文等非英文字符。
解决方法
方法1.加入 #coding=UTF-8或者 #coding:UTF-8
#coding=UTF-8
#第一个python程序
print("hello world")
运行结果
方法2.加入#-- coding:UTF-8 –
#-- coding:UTF-8 --
#第一个python程序
print("hello world")
方法3.加入# - * - coding: utf-8 -*-
# -*- coding: utf-8 -*-
#第一个python程序
print("hello world")
方法4.加入# vim: set fileencoding=utf-8 :
# vim: set fileencoding=utf-8 :
#第一个python程序
print("hello wcorld")
方法5.加入#!/usr/bin/python3
#!/usr/bin/python3
#第一个python程序
print("hello wcorld")