Reputation: 1584
I'm new to Python, but so far I know that in some languages you can "Inderectly" import a module and see it's content for a module that will be imported after that one.
For example, these modules:
main.py
ex1.py
ex2.p
And in them I have
Ex1.py
def Test1():
print('this is a test')
Ex2.py
def Test2():
Test1()
print('this is another test')
Main.py
from Ex1 import Test1
from Ex2 import Test2
Test2()
In python, this will throw me a error saying that Test1()
is not defined in Ex2. But since I imported Test1
before Test2
and then called a method from Test2 in Main.py (where both functions are imported), shouldn't this be allowed? I don't know if there's a way to do this, but some languages, like AutoIt, allow you to do so. I found nothing on this during research.-
Upvotes: 1
Views: 146
Reputation: 19885
No, because of how Python resolves names.
In Ex2
, at runtime, Test2
will look for Test1
in its own scope; in other words, for a function with the name Ex2.Test1
. This can be achieved by a from Ex1 import Test1
statement.
Conversely, in main
, when you execute from Ex1 import Test1
, the name of the function is now main.Test1
, which does not match the name of the function that Test2
is looking for.
It is possible to do that with some sys.modules
magic, but I wouldn't recommend it.
Upvotes: 1
Reputation: 385
Python doesn't work like that, it isolates module namespaces to prevent awful bugs.
Upvotes: 0
Reputation: 82
First off, it should be ex1
and in your python file, so that the names of the modules are correct.
Secondly, import ex2
in ex1
. This should do the trick.
Upvotes: 0