我正在通过Python教程学习
在下面的教程中,我编写了fibo模块代码
但它无效
我通过Ubuntu使用Python解释器
使用原子编辑文件
有ModuleNotFoundError
我也重新安装了' apt_pkg'
这是tracekback
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'fibo.py'; 'fibo' is not a package
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
from apport.packaging_impl import impl as packaging
File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 23, in <module>
import apt
File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Original exception was:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'fibo.py'; 'fibo' is not a package
&#13;
这是fibo.py
# Fibonacci numbers module
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while b < n:
print(b, end=' ')
a, b = b, a+b
print()
def fib2(n): # return Fibonacci series up to n
result = []
a, b = 0, 1
while b < n:
result.append(b)
a, b = b, a+b
return result
&#13;
==========================
模块是包含Python定义和语句的文件。文件名是附加后缀.py的模块名称。在模块中,模块的名称(作为字符串)可用作全局变量名称的值。例如,使用您喜欢的文本编辑器在当前目录中创建名为fibo.py的文件
答案 0 :(得分:0)
我猜您使用import fibo.py
导入模块。您应该使用import fibo
代替。