我正在尝试将一个模块导入到我的main.py python文件中,这两个文件都在同一个目录中
这是一个功能,我想导入到另一个文件(main.py)以如下方式工作:我正在尝试将一个模块导入到我的main.py python文件中,这两个文件都在同一个目录中,python,function,import,module,call,Python,Function,Import,Module,Call,这是一个功能,我想导入到另一个文件(main.py)以如下方式工作: 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 v
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
但是,当我尝试导入时:
elif shape == 'cube':
volumeofcube()
如果您尝试作为导入,则所有这些都无法找到volumes.py模块并将其导入
import volumes
or
from volumes import volumeofcube()
对volumeofcube
方法的调用应为
import volumes
如果您尝试导入为
volumes.volumeofcube()
您需要去掉括号,正确的语法是:
from volumes import volumeofcube()
from volumes import volumeofcube
是您尝试执行的正确语法。如果您的.py名为volumes
,则第二种情况下正确的语法应该是from volumes import volumeofcube
而不使用(
)(即末尾没有括号)。
from volumes import volumeofcube