0

I have some script, i.e. main.py It uses a module module.py with some variables:

functions func1, func2...

integers int1, int2...

and so on

So, I need to extend the functionality of module.py by a nice brand-new submodule named submodule.py that should use variables of module.py to deliver me new features.

Here's some code of submodule.py:

class New_Feature:
    def __init__(self, param1):
        result = func1(param1, default_param)
        self.attr1 = func2(result.attr2)
    def inner_func(self)
        return self.attr1 + int1

So I'm using from submodule import * (I know there are no overriding names) in the module.py code and then using module.New_Feature in main.py

That code throws NameError: name 'func1' is not defined

So how I can import submodule to use module's variables in it?

EDIT: Just explaining why I need this. I'm writing an API wrapper with a bunch of features, but these features should be optional to not overweight main file so users can just use what they need.

ATernative
  • 139
  • 4
  • 1
    You can't. Importing a module brings that module's names into your namespace, not the other way around. – jonrsharpe Feb 02 '19 at 08:36
  • The way you have described it should work. So clearly there is something in the imported that is a bit different from the description. I would start by switching temporarily to fully-qualified names. That is likely to clarify things. – BoarGules Feb 02 '19 at 08:40
  • You should post your full code for module, submodule and main. Right now it isn't clear what the problem is. But I suspect you are not importing within submodule. You are probably missing `from module import func1, func2, ...` in submodule – Karl Feb 02 '19 at 11:25
  • @Karl I can't just use imports in submodule as variables in module might change. So I need to "insert" submodule in the module. Is it possible in any way? – ATernative Feb 02 '19 at 11:39
  • It isn't at all clear what you want to achieve. Please post a minimal, complete and verifiable examole. Show us what's in module, show us how that changes, show us how how that should be used in submodule, show us what you do in main. And explain what you expect to see happen. Chances are there is a much better way to do this – Karl Feb 02 '19 at 11:49
  • @Karl I want just insert new functions into a module from another file, but without changing of module file. Seems like Alex Yu's solution is the right – ATernative Feb 02 '19 at 12:14
  • Addition to my answer: if you realy-realy want to use `import` and feel yourself an evil magician - you can monkey patch `__builtin__.__import__`. In theory you can achive **truly amazing** results. (I hope you don't take it as advice) Look at [5 years of bad ideas] (http://mitsuhiko.pocoo.org/badideas.pdf) by Armin Ronacher. – Alex Yu Feb 02 '19 at 12:28

1 Answers1

0

So, I need to extend the functionality of module.py by a nice brand-new submodule named submodule.py that should use variables of module.py to deliver me new features.

What are you trying to achieve is execfile from Python 2.x

Note that you can pass locals and globals in execfile-call.

For Python 3.x you can use ideasman42 recipe from What is an alternative to execfile in Python 3?

def execfile(filepath, globals=None, locals=None):
    if globals is None:
        globals = {}
    globals.update({
        "__file__": filepath,
        "__name__": "__main__",
    })
    with open(filepath, 'rb') as file:
        exec(compile(file.read(), filepath, 'exec'), globals, locals)
Alex Yu
  • 3,412
  • 1
  • 25
  • 38
  • It seems like my interpreter looking on globals as on undefined local variable. Will see what I should fix there. So submodule would share variables and functions with module, am I right? It just seems like it would copy everything in another script – ATernative Feb 02 '19 at 11:43
  • Without explicit `globals`/`locals` code will be executed just as if it were written in your `module.py`. Substitution of them give you a new level of control what is accessible from `exec`-uted code. Refer to [exec documentation](https://docs.python.org/3/library/functions.html#exec). Beware: here be dragons! – Alex Yu Feb 02 '19 at 11:52