简体   繁体   English

如何导入另一个外部python模块?

[英]How to import another external python module?

this is probably a dumb question, but wasnt too sure what else to do. 这可能是一个愚蠢的问题,但是并不确定要做什么。

main.py main.py

import module2
global x
hello="Hello"
x=module2.message()
x.say()

module2.py module2.py

class message:
    def say():
        print hello

When I print hello, i am referring to the hello variable in main.py however this method will return an error. 当我打印hello时,我指的是main.py中的hello变量,但是此方法将返回错误。 Does anyone know the solution? 有谁知道解决方案? (i would prefer not to pipe the hello variable into the function) (我不希望将hello变量通过管道传递给函数)

The only reliable solution is called encapsulation . 唯一可靠的解决方案称为封装

So, basically, you could change your code to look like that: 因此,基本上,您可以将代码更改为如下所示:

main.py main.py

import module2

global x
hello="Hello"
x=module2.message(hello)
x.say()

module2.py module2.py

class message:
    def __init__(self, hello):
        self.hello = hello
    def say():
        print self.hello

Plus try to follow coding style of Python - life of you and future developers of your code will be easier. 另外,请尝试遵循Python的编码样式-您和以后的代码开发人员的生活将更加轻松。

Multiple options, but do note that one module cannot ever access the calling module directly. 有多个选项,但请注意,一个模块永远无法直接访问调用模块。

  1. Simply pass hello as a variable ( def say(msg): print msg ) 只需将hello作为变量传递( def say(msg): print msg
  2. Pass all variables in main.py to module2 : def say(g): print g['hello'] and say(globals()) main.py所有变量main.pymodule2def say(g): print g['hello']say(globals())
  3. Store it somewhere, then extract it when you need it. 将其存储在某个地方,然后在需要时将其提取。

Since main.py imports module2.py , you can access the globals defined in moule2 in main. 由于main.py导入module2.py ,因此您可以访问main中的moule2中定义的全局变量。

In your case since module2 is not importing main, so the globals in main is not accessed in module2. 在您的情况下,由于module2不导入main,因此在module2中不访问main中的全局变量。

one solution is that defined by @Tadeck 一种解决方案是@Tadeck定义的解决方案

In this particular example, it's totally OK for module2.py to import main.py, there are some gotcha's though. 在此特定示例中,module2.py导入main.py完全可以,但是有些陷阱。

The most obvious is that main.py is probably being run from the command line, like python main.py , which has the effect of making that file think it's called __main__ . 最明显的是main.py可能是从命令行运行的,就像python main.py ,它的作用是使该文件被称为__main__ You could import that module in module2, but that's sort of unusual; 您可以在module2中导入该模块,但这有点不寻常。 it's called "main.py" and you want to import main . 它称为“ main.py”,并且您要import main Doing so will cause the module to be imported a second time . 这样做将导致第二次导入该模块。

For that to be OK, you have to arrange for importing the file to have no side effects unless it's imported as __main__ . 为此, 除非您将文件导入为 __main__ 否则您必须安排文件没有副作用。 A very common idiom in python is to test that condition at the end of a module. python中一个非常常见的习惯用法是在模块末尾测试该条件。

import module2
global x
hello="Hello"

def main():
    x=module2.message()
    x.say()

if __name__ == '__main__':
    main()

And now it's just fine for module2.py to actually import main . 现在,module2.py实际上可以导入main On the other hand, importing variables from one module into another gets hard to predict when the imports can be recursive, you may not have that variable yet because the module is already trying to import you . 另一方面,很难将变量从一个模块导入到另一个模块中,很难预测何时可以递归导入,因为该模块已经在尝试导入 ,所以您可能还没有该变量。 On the other hand, it's always safe to refer to a variable in a module using dotted syntax. 另一方面,使用虚线语法在模块中引用变量始终是安全的。 So your module2.py should be: 因此,您的module2.py应该为:

import main
class message:
    def say():
        print main.hello

which also makes it more obvious just where hello came from. 这也使得hello来源更加明显。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM