Posts: 3
Threads: 1
Joined: Aug 2023
Aug-01-2023, 03:11 PM
(This post was last modified: Aug-01-2023, 03:55 PM by Gribouillis.)
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?
Posts: 741
Threads: 122
Joined: Dec 2017
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'.
Posts: 6,779
Threads: 20
Joined: Feb 2020
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.
Posts: 3
Threads: 1
Joined: Aug 2023
Aug-01-2023, 05:16 PM
(This post was last modified: Aug-01-2023, 10:40 PM by Larz60+.)
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.
Posts: 6,779
Threads: 20
Joined: Feb 2020
Aug-01-2023, 06:41 PM
(This post was last modified: Aug-01-2023, 06:41 PM by deanhystad.)
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.
Posts: 4,782
Threads: 76
Joined: Jan 2018
Aug-01-2023, 06:54 PM
(This post was last modified: Aug-01-2023, 06:54 PM by Gribouillis.)
Try this
>>> from importlib.util import find_spec
>>> spec = find_spec('fibo')
>>> print(spec)
Posts: 3
Threads: 1
Joined: Aug 2023
Aug-03-2023, 03:32 PM
(This post was last modified: Aug-03-2023, 04:05 PM by buran.)
(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.
Posts: 4,782
Threads: 76
Joined: Jan 2018
(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.
|