SyntaxError: Non-ASCII character but no encoding declared
Try adding
# -*- coding: utf-8 -*-
at the beginning of the file,
make sure your file is really encoded in utf-8
SyntaxError of Non-ASCII character
You should define source code encoding, add this to the top of your script:
# -*- coding: utf-8 -*-
The reason why it works differently in console and in the IDE is, likely, because of different default encodings set. You can check it by running:
import sys
print sys.getdefaultencoding()
Also see:
- Why declare unicode by string in python?
- Changing default encoding of Python?
- Correct way to define Python source code encoding
Python: How to solve SyntaxError: Non-ASCII character?
You file is saved as UTF-16 with BOM (big-endian). I saved your sample code in Notepad++ with that encoding and reproduced the error:
File "test.py", line 1
SyntaxError: Non-ASCII character '\xfe' in file x.py on line 1, but no encoding declared; see http://python.org/dev/peps
/pep-0263/ for details
Make sure your file is saved in the encoding declared. You have to use an encoding compatible with ASCII for the hash bang and encoding lines to be read properly. UTF-16 is not compatible, hence the error message when it read the non-ASCII bytes of the byte order mark (BOM) character.
Python SyntaxError: Non-ASCII character '\xe2' in file
You've got a stray byte floating around. You can find it by running
with open("x.py") as fp:
for i, line in enumerate(fp):
if "\xe2" in line:
print i, repr(line)
where you should replace "x.py"
by the name of your program. You'll see the line number and the offending line(s). For example, after inserting that byte arbitrarily, I got:
4 "\xe2 lb = conn.create_load_balancer('my_lb', ['us-east-1a', 'us-east-1b'],[(80, 8080, 'http'), (443, 8443, 'tcp')])\n"
SyntaxError: Non-ASCII character '\xff' when running C executable using subprocess
sys.executable
is the Python interpreter. Sys hello_C
is a compiled C program, not a Python script, you shouldn't use it to run the program. Just run the program directly.
p = Popen(["./hello_C"], stdout=PIPE, bufsize=1)
Related Topics
Run a Linux System Command as a Superuser, Using a Python Script
R and Python in One Jupyter Notebook
R Expand.Grid() Function in Python
Conda Reports Packagesnotfounderror: Python=3.1 for Reticulate Environment
Running Selenium Webdriver with a Proxy in Python
Display Image as Grayscale Using Matplotlib
How to Print a String at a Fixed Width
Name This Python/Ruby Language Construct (Using Array Values to Satisfy Function Parameters)
Numpy Selecting Specific Column Index Per Row by Using a List of Indexes
How to Make a Cross-Module Variable
How to Mock Requests and the Response
Converting Integer to Binary in Python
How to Create a Reference to a Variable in Python
Why Is a List Comprehension So Much Faster Than Appending to a List
How to Get the Parent Directory in Python