Python Forum
Error on import: SyntaxError: source code string cannot contain null bytes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error on import: SyntaxError: source code string cannot contain null bytes
#1
I am following a Python tutorial and have created the following module(from the tutorial) in Notepad under Windows 11:

def fib(n):    # write Fibonacci series up to n
    a, b = 0, 1
    while a < n:
        print(a, end=' ')
        a, b = b, a+b
    print()

def fib2(n):   # return Fibonacci series up to n
    result = []
    a, b = 0, 1
    while a < n:
        result.append(a)
        a, b = b, a+b
    return result
I have saved the module as fibo.py. When I attempt to import the module(import fibo), Python returns the error:
Error:
SyntaxError: source code string cannot contain null bytes
Now when I save the module as kw.py or kirk.py, the import works and calls to the functions in the module also work.

What am I missing here?
Gribouillis write Aug-01-2023, 03:54 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Make sure your python program has a different filename than the module you are importing.
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
As Paul says, a file named fibo.py cannot "import fibo". Are you doing that? That would match the name change behaivior (changing name of fibo.py fixes problem), but I would not expect it to report as a syntax error. Instead you should get an AttributeError and there might be some mention of a circular import. I don't think you are telling us everything.
Reply
#4
Okay in the interest of full disclosure my Python session looks like this:

Python 3.11.4 (tags/v3.11.4:d2340ef, Jun  7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append("C:\\Users\\kirkw\\Documents\\Python")
>>> sys.path
['', 'C:\\Users\\kirkw\\AppData\\Local\\Programs\\Python\\Python311\\python311.zip', 'C:\\Users\\kirkw\\AppData\\Local\\Programs\\Python\\Python311\\DLLs', 'C:\\Users\\kirkw\\AppData\\Local\\Programs\\Python\\Python311\\Lib', 'C:\\Users\\kirkw\\AppData\\Local\\Programs\\Python\\Python311', 'C:\\Users\\kirkw\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages', 'C:\\Users\\kirkw\\Documents\\Python']
>>> import fibo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
SyntaxError: source code string cannot contain null bytes
>>> import kirk
>>> kirk.fib(1000)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
>>>
I previously created the file fibo.py and then saved it as kirk.py. fibo, as indicated in the session notes above, causes the interface to throw the indicated message. This is not stopping me from moving on in my learning process. However, nuisances like this have a tendency to bite you later on when building production code.

TIA
Larz60+ write Aug-01-2023, 10:40 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#5
Try renaming it fibo.py. I think the file only looks like it is named fibo.py. I almost never use notepad, so I don't know how you gave the file a bad name, but it looks like you somehow managed to imbed null characters in a string. Probably some unicode encoding/decoding oddity.
Reply
#6
Try this
>>> from importlib.util import find_spec
>>> spec = find_spec('fibo')
>>> print(spec)
Reply
#7
(Aug-01-2023, 06:54 PM)Gribouillis Wrote: Try this
>>> from importlib.util import find_spec
>>> spec = find_spec('fibo')
>>> print(spec)

Thanks very much. I had created the module using Word earlier and placed it in the path:
ModuleSpec(name='fibo', loader=<_frozen_importlib_external.SourceFileLoader object at 0x000002A2CA059D10>, origin='C:\\Users\\kirkw\\AppData\\Local\\Programs\\Python\\Python311\\fibo.py'). Of course I forgot about that. It has been a few years since I have done any serious coding other that sql scripts and I am making all kinds of rooky mistakes - duh.
Reply
#8
(Aug-03-2023, 03:32 PM)kirkwilliams2049 Wrote: I am making all kinds of rooky mistakes

The folder C:\\Users\\kirkw\\AppData\\Local\\Programs\\Python\\Python311 is certainly not the place to store your own programs! Move them away from here.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  delivery exe without source code py loky62 2 355 Apr-04-2024, 05:47 PM
Last Post: loky62
  Algorithm for extracting comments from Python source code Pavel1982 6 542 Feb-28-2024, 09:52 PM
Last Post: Pavel1982
  How to express null value klatlap 3 868 Mar-25-2023, 10:40 AM
Last Post: klatlap
  import module error tantony 5 3,464 Dec-15-2022, 01:55 PM
Last Post: Lauraburmrs
  Cryptic Error with import statement Led_Zeppelin 2 2,560 Jan-11-2022, 01:13 PM
Last Post: Led_Zeppelin
  Install any library via pip get an error cannot import name 'SCHEME_KEYS' from 'pip. Anldra12 2 10,678 Jan-04-2022, 01:05 PM
Last Post: Anldra12
  value null when update in json file 3lnyn0 6 3,293 Dec-30-2021, 05:52 PM
Last Post: ndc85430
  No Module found in other directory than source code [SOLVED] AlphaInc 1 2,072 Nov-10-2021, 04:34 PM
Last Post: AlphaInc
  Generating classes diagram from source code Pavel_47 3 5,677 Oct-01-2021, 03:31 AM
Last Post: Lou
  How do I open the Source code of a library? JaneTan 1 2,285 Aug-18-2021, 02:12 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020