简体   繁体   中英

How to import another external python module?

this is probably a dumb question, but wasnt too sure what else to do.

main.py

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

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. Does anyone know the solution? (i would prefer not to pipe the hello variable into the function)

The only reliable solution is called encapsulation .

So, basically, you could change your code to look like that:

main.py

import module2

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

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.

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 )
  2. Pass all variables in main.py to module2 : def say(g): print g['hello'] and 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.

In your case since module2 is not importing main, so the globals in main is not accessed in module2.

one solution is that defined by @Tadeck

In this particular example, it's totally OK for module2.py to import main.py, there are some gotcha's though.

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__ . You could import that module in module2, but that's sort of unusual; it's called "main.py" and you want to 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__ . A very common idiom in python is to test that condition at the end of a module.

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 . 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:

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

which also makes it more obvious just where hello came from.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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