我试图将模块导入我的 main.py python文件中,这两个文件都在同一个目录中


def volumeofcube():
a = int(input('Enter the length of the side:'))
volume = a**3
#Rounded to one decimal place
volume = str(round(volume, 1))
#The volume is added to its corresponding list
volumeofcubelist.append(volume)
print('The volume of the cube with the side', a,'is', volume)
return volume

这是一个我想导入到另一个文件 (main.py( 中的功能,如下所示:

elif shape == 'cube':
volumeofcube()

但是当我尝试导入时:

import volumes
or
from volumes import volumeofcube()

这些都无法找到 volumes.py 模块并将其导入

如果您尝试导入为

import volumes

volumeofcube方法的调用应该是

volumes.volumeofcube()

如果您尝试导入为

from volumes import volumeofcube()

您需要去掉括号,正确的语法是:

from volumes import volumeofcube

最新更新