1

I am getting a strange error when defining variables in Python 4.1.5 (IDE:Spyder). However, even with the error, the code runs without any issues!

enter image description here

enter image description here

As you can see, the variable social_cost_of_carbon is stored as a variable, but I keep getting that error as show in picture 1 (the error writes: Undefined name 'social_cost_of_carbon' (Pyflakes E)

I feel that the way that i declare those variables might be the reason:

def convert_to_var(df):
    desc = []
    val = []  
    
    for i,row in df.iterrows():
        desc.append(i)
        val.append(row) 
        
    return dict(val)

val_dict = convert_to_var(IA)
locals().update(val_dict)

Since the code runs without any problems, I am not doing anything to resolve this. Do I need to worry and fix this, or do I just let it be and continue without dealing with the error since the code runs smoothly?

Thanking you in advance.

1 Answer 1

2

(Spyder maintainer here) As you guessed, the problem is that you're creating your variable dynamically with this line in your code:

locals().update(val_dict)

Since our linter can't find where that variable is properly declared, it reports that it's undefined. But it's safe for you to ignore that message.

Note: Since Spyder 5.1.0, you can avoid the linter error by adding a comment at the end of the problematic line of the form # noqa.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your answer Carlos, it really clear things up. Do you have an expectation of when this future version will be available?
Most probably in our 4.2.2 version, to be released in February 2021.
Excellent - would love to see the little red dots disappear from my code =]
I was presuming that, that's why I left my note at the end.
It works in Spyder 5.4.2. noqa = No Quality Assurance
|

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.