Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: source code string cannot contain null bytes

I'm using Max OS X 10.10.3, and I finally got the graphics.py to show in Python 3, before it was saying no module existed.

However, now when I try import graphics, or from graphics import *, I get the message:

"source code string cannot contain null bytes"

Does any Mac user (using Python 3) perhaps know what is wrong? Has anyone used the Zelle book and his graphics.py module? Thanks.

like image 748
Peter G. Williams Avatar asked Jul 05 '15 18:07

Peter G. Williams


4 Answers

For posterity: I had the same problem and fixed it using,

sed -i 's/\x0//g' FILENAME 

The file seemed to be messed up in numerous ways (wrong endings, etc); no idea how...

See https://stackoverflow.com/a/2399817/230468

like image 57
DilithiumMatrix Avatar answered Sep 20 '22 10:09

DilithiumMatrix


I am using Visual Studio Code, the encoding was set to UTF-16 LE. You can check the encoding on the right bottom side of VSCode. Just click on the encoding and select "save with encoding" and select UTF-8. It worked perfectly.

like image 36
Paco Meraz Avatar answered Sep 22 '22 10:09

Paco Meraz


I got this message when I wanted to use eval for my input for my function that sometimes it takes string or int/float but when it takes numpy numbers, it throws this exception, eval(number).

My solution was eval(str(number)).

like image 22
Habib Karbasian Avatar answered Sep 20 '22 10:09

Habib Karbasian


I just encountered this problem, which is usually caused by the encoding format. You can use Notepad++ to change the encoding format of python files to UTF-8.

like image 39
Chaplin Hwang Avatar answered Sep 19 '22 10:09

Chaplin Hwang