I have 2 scripts and both need to use a function which is defined on the other script. I want to do something like this:
file1.py:
from file2 import function2
def function1():
someCode...
... some code where I use function2...
file2.py:
from file1 import function1
def function2():
someCode...
... some code where I use funcion1 ...
The problem is that it doesn't work and i don't know neither why nor how to fix it. How can I do it?
Option #1 move function1 and function2 to a common file:
common.py:
def function1():
# some stuff
pass
def function2():
# some stuff
pass
And import function1 and function2 from common.
Option #2 use a local import instead:
def function1():
someCode...
def some_method_where_function_2_is_used():
from .file2 import function2
... some code where I use function2...
The problem is that the code to run the function from the other file runs on import, so you end up with an endless cycle, which python doesn't like.
To fix, perhaps run the functions in two more separate .py s and define them in another two.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With