0

I have 2 files, the first is named function_call_test.py and contains the following code;

from Strategy_File import strategy

def function_1():
    print('This works')

strategy()

The second file is called Strategy_File.py and contains the following code;

def strategy():
    print('got here')
    function_1()

When running the first script I get 'NameError: name 'function_1' is not defined'. I thought that when you imported a function that it was added to the importing modules namespace. If that is the case why can't strategy() see function_1()?

Just as importantly, how do I make this work. The above is for demo purposes only, I have reasons for wanting strategy() to be in a separate module.

Python 3.6, Windows 7-64, Visual Studio 2019 and IDLE

1
  • Is there really not a duplicate for this? Commented Aug 13, 2021 at 22:19

3 Answers 3

4

Python is statically scoped. Lookup for a free variable (such as function_1) proceeds though the scopes where strategy is defined, not where it is called. As strategy is defined in the global scope of the module Strategy_File, that means looking for Strategy_File.function_1, and that function is not defined.

If you want strategy to call something in the current global scope, you need to define it to accept a callable argument, and pass the desired function when you call strategy.

# Strategy_File.py

# f is not a free variable here; it's a local variable
# initialized when strategy is called.
def strategy(f):
    print('got here')
    f()

and

# function_call_test.py

from Strategy_File import strategy


def function_1():
    print('This works')

# Assign f = function_1 in the body of strategy
strategy(function_1)
Sign up to request clarification or add additional context in comments.

Comments

0

You have to import every name into the file where it is used. So you need to modify Strategy_File.py to this:

from function_call_test import function_1

def strategy():
    print('got here')
    function_1()

But now you encounter a new problem: circular imports. Python won't allow this. So you will have to figure out a different way to organize your functions.

2 Comments

You’re right, but you haven’t actually answered the question have you. This is more like a comment.
@balmy The question answers how to fix the NameError. Someone else explains how to fix the circular import, so I'll leave it at that.
0

This error caused by Strategy_File.py is missing definition of function_1().

adding this line in Strategy_File.py on the top will helps.

edit: circular imports will not help. sorry for the misinformation.

4 Comments

Python doesn't allow circular imports, so OP needs something like dependency injection to fix this.
While this solves the immediate error, it leads to another error.
Circular imports are fine, as long as you don't need a value defined in one module in order to define the other.
ah, you guys are right. Did pay attention that function 2 is called in the first file. edited. sorry for this misinformation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.