简体   繁体   English

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

[英]Im trying to import a module into my main.py python file both of which are in the same directory

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

this is a funtion that i want to import into another file (main.py) to work like this: 这是一个功能,我想导入到另一个文件(main.py)中以使其工作:

elif shape == 'cube':
    volumeofcube()

however when I try to import: 但是,当我尝试导入时:

import volumes
or
from volumes import volumeofcube()

none of these are able to find the volumes.py module and import it 这些都无法找到volumes.py模块并将其导入

If you are trying to import as 如果您尝试导入为

import volumes

the call for volumeofcube method should be volumeofcube方法的调用应为

volumes.volumeofcube()

if you try to import as 如果您尝试导入为

from volumes import volumeofcube()

you need to take the parenthesis off, the right syntax is: 您需要取消括号,正确的语法是:

from volumes import volumeofcube

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将main.py导入另一个模块(包含在同一目录中) - Importing main.py into another module (contained in the same directory) 如何将 cog 导入我的 main.py 文件? - How can I import a cog into my main.py file? 如何从 main.py (在根目录中)将 object 导入到子目录中包含的模块中? - How do I import an object from main.py (in the root directory), into a module contained in a subdirectory? 如何从与main.py脚本处于同一层次结构级别的文件夹中导入脚本? - How to import script from a folder at the same hierarchy level at which the main.py script is? 项目设计 - 如何在 main.py 中导入子模块? - Project design - How to import a Child module in main.py? python:无法打开文件'main.py':[Errno 2]没有这样的文件或目录-docker - python: can't open file 'main.py': [Errno 2] No such file or directory - docker 为什么我的 cog 文件不想导入到 main.py? - Why my cog file don't want import to main.py? 无法打开文件 'main.py': [Errno 2] 没有那个文件或目录 - Can't open file 'main.py': [Errno 2] No such file or directory 如何使 python 下载文件到 main.py 的当前目录? - How can I make python download file to current directory of main.py? 如何更改 Pycharm 尝试查找我的 main.py 文件的位置? - How do I change where Pycharm is trying to find my main.py file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM