nammerkage
nammerkage

Reputation: 304

Python package can't see global variable in another file

I'm having a lot of fun with a Python packing I'm creating. However I have a problem;

A variable that is suppose to be global can't be seen! This is the files

__init__.py
main.py
helpers.py

In main.py I have the following

from .helpers import *
class scene:
    def __init__(self,variables):
        self.variables = variables

global _scene
_scene = scene(100)

So I'm making a _scene that should be globally accessible. However this does not work in helpers.py;

from .main import *
print(_scene)

When i run I get the error :

NameError: name '_scene' is not defined

I think __init__.py is fine

from .main import *
from .helpers import *

What am I missing? Would love your input!

Upvotes: 1

Views: 931

Answers (1)

devReddit
devReddit

Reputation: 2947

This is a bad idea if you want to use global variables from a file and use them in other files and vice versa. It creates cyclical dependency. If you need to use a global variable in both files, you can define a third file MyGlobals.py and declare the global variables there. Then import that file into both helpers.py and main.py and access through MyGlobals._scene

Upvotes: 1

Related Questions