SyntaxError: invalid non-printable character U+00A0 in python

Question:

I’m getting the error:

SyntaxError: invalid non-printable character U+00A0

When I’m running the code below:

# coding=utf-8
from PIL import Image
 
img = Image.open("img.png")

I have tried to load different images with different formats (png, jpg, jpeg). I have tried using different versions of the Pillow library. I have also tried running it using python 2 and 3.

Asked By: cris.sol

||

Answers:

The problem was related to a fake space found in the third line (the empty one). It is a character that looks like a space but is actually something else which is not detected by python. By removing this character the error disappeared. The character is this:

 
Answered By: cris.sol
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.