0

I am playing around with python (and pygame in particular), and I am organizing my project files:

/cls
    imageloader.py
    level.py
    __init__.py
/graphics
/maps
game.py
__init__.py
...

And comes the question about global constants in modules (like a configuration variables - FPS, TILESIZE, etc). Is there any way to access constant defined in game.py within /cls/level.py? And, what's more important - it's a wrong approach, isn't it? Should I pass it as an argument?

What's a pytonish way to do it?

Ruslan Osipov
  • 5,655
  • 4
  • 29
  • 44

1 Answers1

2

If you make the whole thing a package and also make cls a package (by providing __init__.py files in each directory), then from levels.py you can do from ..game import someConstant.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
  • But if I do that - I get `ValueError: Attempted relative import beyond toplevel package` – Ruslan Osipov Aug 14 '12 at 02:30
  • Did you use too many periods and/or forget `__init__.py` at the top level? – BrenBarn Aug 14 '12 at 02:34
  • Just checked - I did not... However, for some reason `__init__.py` at top level is being ignored (compiled pyc is not created). – Ruslan Osipov Aug 14 '12 at 02:42
  • OK, this http://stackoverflow.com/questions/1918539/can-anyone-explain-pythons-relative-imports explains it (you might want to add a link to your answer). – Ruslan Osipov Aug 14 '12 at 02:44
  • The other approach besides what is suggested there is that in `game` you should do `from .cls import levels` instead of `from cls import levels`. – BrenBarn Aug 14 '12 at 02:48