开发者

Python import parent file

开发者 https://www.devze.com 2023-04-11 17:54 出处:网络
I have 3 files, file 1 imports file 2, file 2 imports file 3 and file 3 needs to refference something in file 1. A basic example of this is

I have 3 files, file 1 imports file 2, file 2 imports file 3 and file 3 needs to refference something in file 1. A basic example of this is

#---------------------File1.py---------------------
import File2

class core():
    def __init__(self):
        self.something = "Hello World"
        self.run = File2.run()

c = core()

#---------------------File2.py---------------------
import File3

class run():
    def __init__(self):
        self.somethingelse = "Cows"
        File3.func()

#---------------------File3.py---------------------
import File1

class func():
    print File1.c.something

So as you can see, File3 needs to access the variable 'c.something' which exists in File1, in part of the class that calls file 2 which in turn calls file 3.

This is from a piece of code I've "inhereted", and the rest of the code is quite complicated so I don't really want to have to change things around. To give a more realistic example of what's going on, here's basically what the actual code is doing;

#---------------------File1.py---------------------
import File2
import config

class core():
    def __init__(self):
        self.config = config.load() #Config.load is an expensive DB opperation, and should only be run once
        self.run = File2.run()

c = core()

#---------------------File2.py---------------------
import File3

class run():
    def __init__(self):

        #
        # Do some things that don't require access to 'config'
        #

        File3.func()

#---------------------File3.py---------------------
import File1

class func():
    #Do some stuff that requires access to the config
    print File1.c.config['some config']

We can only call config.load() once, but lots of functions must be able to access it. We also can't pass self.config down as arguments for the function call, there are far too many functions that depend on it for this to be reasonable. We'd like to avoid using global variables and keep it refferenced under a class if possible.

开发者_开发技巧

What's the best way to go about this?


I don't really want to have to change things around.

(...)

What's the best way to go about this?

I hate to break this to you, but the best way is to refactor the code; i.e., to shuffle things around. You don't want circular dependencies, they're going to make life hard on you in the long run. A hierarchical (tree- or DAG-structured) module setup is by far the easiest to handle.

The easiest way to achieve this is to merge the three modules into one, then perhaps move some stuff back into separate modules.


The easiest way to make this working is to move import File1 line in the end of 'File3.py'.

See description here - http://effbot.org/zone/import-confusion.htm#circular-imports.


You could import at runtime rather than at module load time, so File3 would be:

class func():
    def __init__(self):
        import File1
        #Do some stuff that requires access to the config
        print File1.c.config['some config']

Although that still has the issue of the c = core() line -- this line means that all the extra module initialisation (that happens in core()) happens before it is assigned to the c variable. To get around this too, you'd probably want to have File1 as something like this:

import File2

class core:
    def __init__(self):
        self.config = {}

    def run(self):
        self.run = File2.run()

c = core() # make sure the global variables are initialised nicely
c.run()    # after initialisation is finished, *then* do the work
0

精彩评论

暂无评论...
验证码 换一张
取 消