python脚本运行后发生ModuleNotFoundError

python脚本运行后发生ModuleNotFoundError,python,python-3.x,Python,Python 3.x,我一直在努力寻找解决我的具体错误的方法,但没有成功 因此,在终端的Python3环境中,我为'problems1.py'键入以下内容: >>> import problems1.py import math print("Please enter a number 'x' ") x = input() x = float(x) print("Please enter a number 'y' ") y = input() y = f

我一直在努力寻找解决我的具体错误的方法,但没有成功

因此,在终端的Python3环境中,我为'problems1.py'键入以下内容:

>>> import problems1.py
import math

print("Please enter a number 'x' ")
x = input()
x = float(x)

print("Please enter a number 'y' ")
y = input()
y = float(y)

exp = x ** y
lgrm = math.log2(x)

print(f"The number 'x' raised to the power of 'y' is {exp}, and ...")
print(f"The log(base 2) of x is {lgrm}")
问题1.py:

>>> import problems1.py
import math

print("Please enter a number 'x' ")
x = input()
x = float(x)

print("Please enter a number 'y' ")
y = input()
y = float(y)

exp = x ** y
lgrm = math.log2(x)

print(f"The number 'x' raised to the power of 'y' is {exp}, and ...")
print(f"The log(base 2) of x is {lgrm}")
我只能输入请求的数字并输出打印语句一次,但是,在输出结束时,显示以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'problems1.py'; 'problems1' is not a package
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
ModuleNotFoundError:没有名为“problems1.py”的模块;'“问题1”不是一个包

以后任何导入问题1.py的尝试都只会返回错误。

我想我对你的问题有一个答案

我将简化您的文件
problems1.py
,使这个解释更容易写下来

假设您的文件
problems.py
中包含以下代码:

#contents of the file: problems.py
firstname = input("First name: ")
lastname = input("Last name: ")
print(f"Your full name is: {firstname} {lastname}")
如果启动该文件,则会得到以下输出:

First name: Kevin
Last name: Durant
Your full name is: Kevin Durant
基本上,当您发出
import
时,Python解释器会打开您导入的文件,并逐行检查它,并尝试为文件创建名称空间,并对代码进行一些优化

同样,当解释器到达行
firstname=input(“firstname:”)
时,它必须执行等号右侧的代码,以将值映射到命名空间
firstname

解释器将读取整个文件,以确保在调用
import
后返回执行任何其他命令之前,不会遗漏对整个名称空间的任何更改

因此,让我们启动一个解释器并发出导入问题:

>>> import problems
First name: Kevin
Last name: Durant
Your full name is: Kevin Durant
>>>
>>> problems.firstname
'Kevin'
>>> problems.lastname 
'Durant'
请注意,当我们发布导入时,它执行了其中的代码。您还可以看到,通过使用
problems.firstname
problems.lastname
访问其属性,我们可以从
problems
访问
firstname
problems.lastname

那么,让我们看看当您发出导入问题时会发生什么。firstname:

>>> import problems.firstname
First name: Kevin
Last name: Durant
Your full name is: Kevin Durant
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'problems.firstname'; 'problems' is not a package
>>导入问题。firstname
名字:凯文
姓:杜兰特
你的全名是:凯文·杜兰特
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
ModuleNotFoundError:没有名为'problems.firstname';'的模块“问题”不是一个包
这里发生的是,当您发出相对(点分隔)导入时,Python解释器假定您导入的是
problems
包的子文件


因为在那里找不到包,它会抛出一个错误,并使代码崩溃。

请尝试导入问题1您能显示目录结构吗?你在用水蟒吗?您可能想了解python的基本特性,特别是它的模块结构和_init__py文件。我们可以回答您的所有问题,但我非常肯定,您会从缓慢但肯定的阅读Python的基本文档/简介中获益匪浅。你看起来很聪明,很乐意@拉米雷斯:好的,当你输入一些你不应该得到任何输出的东西时,我很困惑。您只是在代码中包含文件或包。您仍然需要调用它来执行任何操作。@DwightFoster我第一次键入“导入问题1”并单击“返回”时,我能够输入请求的变量并输出它们的数学结果。第二次,或者其他任何时候,我键入“导入问题1”并点击return,什么也没发生。@J.F.Ramirez好的,现在我大致了解了您想要做什么。我认为您正在尝试运行正确的代码?如果是这样,您可能希望根据终端执行类似于
python problems1.py
python3 problems.py
的操作。这将运行代码导入,但不用于这样的场景。