Skip to content
Advertisement

“SyntaxWarning: name ‘our_mongo’ is used prior to global declaration”

My code keeps giving me the above syntax warning. My code includes, but is not limited to:

JavaScript

I’ve tried everything and it’s still an issue. WHY is this a syntax error? What is the best way to fix this?

Advertisement

Answer

It is not a SyntaxError but a SyntaxWarning. Python warns that you’re using our_mongo variable before a global our_mongo within the function. Syntactically it does not actually matter on which line of the function the global statement is; but the idiomatic way would be to use global before the first access.

Another issue altogether is that you have multiple global our_mongo statements, where single would do, and also that you do not even need the global at all – it is only for the case when you want to assign a new value to the global variable; i.e.

JavaScript

Thus, just remove all global statements altogether from your on_message, they’re unnecessary.

Advertisement