简体   繁体   English

尝试导入模块时出错

[英]Error when I try to import a module

This question may be trivial but I don't really get it. 这个问题可能很琐碎,但我真的不明白。 I have two python modules. 我有两个python模块。

This is module1 : 这是module1

import module2

def main():
    print funcion2(2,3)

if __name__ == '__main__':
    main()

This is module2 : 这是module2

def funcion2(a, b):
    return a + b

I get an error (function2 wasn't found). 我收到一个错误(找不到function2)。 If write " from module2 import * " it works fine. 如果将“ from module2 import *”写入,则可以正常工作。 Why? 为什么?

If you want to import module2 you will need to call function2 this way: module2.funcion2(2,3) . 如果要import module2 ,则需要以这种方式调用function2module2.funcion2(2,3)

You usually want to avoid from <module> import * so either do as above or from module2 import function2 and then you can simply call function2(2, 3) . 您通常想避免from <module> import *所以可以像上面那样或from module2 import function2避免,然后可以简单地调用function2(2, 3)

Use: 采用:

import module2

module2.funcion2(2, 3)

You import a module and should explicitly specify it while calling a method. 您导入模块,并在调用方法时应明确指定它。

You can also import only this function: 您还可以仅导入以下功能:

from module2 import funcion2

funcion2(2, 3)

暂无
暂无

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

相关问题 尝试导入任何模块时出现语法错误 - Syntax error when I try to import any module 当我尝试将mwclient模块用于Python时,为什么会出现错误“无法导入名称扫描仪”? - Why am I getting the error “cannot import name Scanner” when I try to use the mwclient module for Python? 尝试导入skimage时缺少什么模块? Python 3.5 - What module is missing when I try to import skimage? Python 3.5 当我尝试导入模块时在Jupyter Notebook NameError中 - In Jupyter Notebook NameError when I try to import a module 当我尝试导入 lxml.html 时出现导入错误 - import error when i try to import lxml.html 在 django 中,我创建了“工具”应用程序,当我尝试将工具导入其他文件时出现错误“没有名为‘工具’的模块” - In django I have created "tool" app, When I try to import tool to other file I got error "No module named 'tool' " 当我尝试运行“import matplotlib.pyplot as plt”时,我收到以下错误:“ModuleNotFoundError: No module named 'PIL'” - When I try to run “import matplotlib.pyplot as plt”, I receive the following error: “ModuleNotFoundError: No module named 'PIL'” 当我尝试导入 minerl 时出现错误 - I get an error when I try to import minerl 当我尝试导入乌龟时,我不断收到错误消息 - i keep getting an error when i try and import turtle 尝试导入tkFileDialog时出现错误 - I keep getting an error when I try to import tkFileDialog
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM