2

Okay, so I know this may seem stupid but I am currently making a game using multiple files, one main one that receives all variables from other files and uses them in ways. I'm using the:

from SPRITES import *

to get these variable over, however now I need a variable that can only be defined in MAIN in SPRITES (as the platform the player is standing on is located in main, and this needs to change the controls defined in sprites), however if I just do a

from MAIN import *

this seems to break the connection completely. Help please

EDIT: Okay, currently my file is probs too large to post all code on here but I'll try to post whats relevent on here (first time here). This is the start to the main 'titleMAIN' file

import pygame as pg
import random
from titleSETTING import *
from titleSPRITE import *
cont = ("green")
class Game:
    def __init__(self):
        # initialize game window, etc
        pg.init()

and so on calling upon the Player class in the SPRITES file from the Game class - I need to be able to use the 'cont' variable in the Player class:

def new(self):
    # start a new game
    cont = ("green")
    ...
    self.player = Player(self)
    self.all_sprites.add(self.player)

And here is where I tried to call upon the MAIN file from the SPRITES file:

from titleSETTING import *
...

class Player(pg.sprite.Sprite):
    def __init__(self, game):

Sorry that I was vague, first time here and kinda a novice at coding, no matter how much I enjoy it. BTW by files I mean different python (.py) files in the same folder - using IDLE as an IDE, which sucks but it's what I got

EDIT 2: Thanks for the responses - I realize that it's probably better to just try to make this all one file instead of two, so to not over complicate the code, so I'll work with that mindset now

4
  • When you used starred imports you create a bunch of new module global variables. Use the fully qualified names Commented Aug 12, 2018 at 19:20
  • What exactly are you asking? If you want to stop a variable to not be imported during a wildcard import *, then prefix it with an underscore: _var Commented Aug 12, 2018 at 19:21
  • Hi, and welcome! We need some more context to go by. Could you provide a full code example? I'm not sure what you mean by files here. Are you talking about python files (modules)? Commented Aug 12, 2018 at 19:21
  • You seem to be trying to make a "Circular import" which is never a good idea anyway, you might be able to make a third python file (shared.py) that both can use but hard to say from such an abstract example. Commented Aug 12, 2018 at 19:28

2 Answers 2

1

The main reason this wasn't working for you is that circular imports are problematic. If some file A imports some other file B, then you do not want B to import A (even indirectly, via some other files).

So to make it work, I split main into two.

As an aside, using global variables (and import *) tends to make programs harder to read. Instead of a bunch of globals, consider perhaps a single global that has the values you need as fields. Instead of import *, consider explicit imports (just import sprites, and then sprites.foo).

main.py:

from sprites import *
from config import *

print "Hello from main. main_value is: ", main_value
print "sprite value is: ", sprite_value
do_sprite()

sprites.py:

from config import *
sprite_value=10

def do_sprite():
  print "main value is: ", main_value

config.py:

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

Comments

0

While technically possible (using some obscure Python features), what you're trying to achieve is neither easy, nor actually a good idea.

Note that the fact that you did from moduleX import *, doesn't make the variables defined in moduleX magically available in main (or where-ever you put the import statement). What it does, it creates new variables with the same names in your current module and make them point to the same objects as those in moduleX at the moment when the import is executed. Let's say there's A in some module named X and it was initialized to "foo". You did import * from X and now print(A) will show foo. If you now call a function from X and it changes A to bar, it won't affect what you have in main - that is still the object foo. Likewise, if you do a="baz" in main, functions from X that refer to A will still see X's copy of that variable.

If you need some data to be available to more than one module, it may be best to arrange for all that shared data to be stored in some common object and have an easily-accessible reference to that object in all the modules that need the shared data. Here are possible choices for this object, pick what suits your taste:

  • it can be a module that's just meant to keep common variables, let's say it is called data or common (have an empty file data.py). Import it everywhere you need to and set variables as data.A = "something" or use them as needed, e.g. print (data.A).

  • it can be an instance of a class that you define yourself, e.g.:

 

class data_class(object):
    # set initial values
    var1 = 10
    A = "foo"

Now, create an instance of it with data = data_class() and pass it to every module that needs it. E.g., define it in one module and import it from everywhere else.

  • you can also use a Python dictionary (and, like with the class instance, have a reference to it in all modules). You will then refer to your common data items as data["A"], etc.

Comments

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.