44

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.

WhiZTiM
  • 21,207
  • 4
  • 43
  • 68
Peter G. Williams
  • 627
  • 1
  • 6
  • 13
  • 2
    According to [this](http://stackoverflow.com/questions/12988619/python-cturtle-module-not-loading), that message appears when there's a null byte in the file. It doesn't say how exactly to remove it, but I'd try to edit the file with something like Notepad++. – TigerhawkT3 Jul 05 '15 at 20:14
  • 1
    From [this answer](http://superuser.com/a/492715), this might remove the null bytes: `python -c 'import sys; sys.stdout.write(sys.stdin.read().replace("\0", ""))' < graphics.py > graphics_new.py`. –  Jul 05 '15 at 22:26
  • But how did you create this file? That should answer how you ended up with null bytes in the first place. –  Jul 05 '15 at 22:27
  • Evert, I didn't create the graphics.py module, it's designed for beginners wanting an introduction to GUI in Python: http://mcsp.wartburg.edu/zelle/python/ – Peter G. Williams Jul 06 '15 at 02:06
  • And thanks for the advice, I'll try that out. But how many different lines do I use for those instructions you gave me? Thanks. – Peter G. Williams Jul 06 '15 at 02:06

9 Answers9

41

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

Community
  • 1
  • 1
DilithiumMatrix
  • 17,795
  • 22
  • 77
  • 119
  • 1
    I created my .py file using windows terminal. echo print("test") > test.py and that file was corrupted from start. – Mangs Oct 22 '21 at 08:04
29

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.

Paco Meraz
  • 407
  • 1
  • 8
  • 12
7

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)).

Habib Karbasian
  • 556
  • 8
  • 18
5

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.

Chaplin Hwang
  • 61
  • 1
  • 7
2

This kind of error is not from your project source code. This kind of error emerges from your python interpreter. So the best solution is to set your python interpreter in your project env directory. or set the interpreters virtual env properly using your IDE's interpreter configuration.

2

Open your file with an editor that can show you all invisible character. You will see where is the invalid char, just delete and re-type it.

If you are on mac you can do it with Coda > Open your file > Show invisible characters.

Fabio Caccamo
  • 1,871
  • 19
  • 21
2
  • Go to python folder python36/Lib/site-packages/scipy/
  • Open __init__.py

Change:

from scipy._lib._testutils import PytestTester

to:

from scipy._lib.test__testutils import PytestTester

It worked on my windows 7

Jay Dorsey
  • 3,563
  • 2
  • 18
  • 24
  • I am using google colab and I got the message: source code string cannot contain null bytes. Do you have any Idea how can I fix it? – Sara Apr 14 '20 at 16:18
-2

I created a file using windows terminal echo "hello" > hello.py and got this issue. I resolved by creating a new file from notepad

Janarthanan Ramu
  • 1,331
  • 16
  • 17
-2

My problem was the python interpreter, try changing it to the right python directory path

Harel Malichi
  • 105
  • 1
  • 4
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/30376323) – David Bern Nov 19 '21 at 00:26