1

I want this structure:

Zimp/controller/game_play -->How do I: import Zimp/model/game_play module in the easiest way? Zimp/model/game_play

I made a folder called controller and a folder called model. Within those folders I put an empty __init__.py file (don't know why that would do anything). I didn't make a model.py file or a controller.py file. It didn't work. I just made a model.py and a controller.py that are empty except for the main block that automatically appears when creating a new module. No difference.

In controller/game_play.py I tried: from ..model import game_play_model

It says value error: attempted relative import in non-package

Is the idea not to actually put them in separate directories? What is the norm?

Thanks

user3164083
  • 1,053
  • 6
  • 18
  • 35

2 Answers2

1

The problem is you're trying to execute a subpackage module directly, see answers to the question Attempted relative import in non-package even with __init__.py.

First I think you need to set up your directory file structure like this:

Zimp/                       top-level package
    __init__.py                 package initalization
    controller/                     subpackage
        __init__.py                     subpackage initalization
        game_play.py                    subpackage module
    model/                          subpackage
        __init__.py                     subpackage initalization
        game_play_model.py              subpackage module

The __init__.py files can all be empty as they just indicate that the directory is a [sub]package.
For illustrative purposes let's say thegame_play_model.pyfile contained:

print 'hello from game_play_model.py'

and thegame_play.pyfile contains the following to detect when it was being executed directly and adds the name of the parent of its folder — Zimp — to the Python search path — thus allowing you to then directly import other things from the package when it's run that way.

if __name__ == '__main__' and __package__ is None:
    import sys, os.path as path
    # append parent of the directory the current file is in
    sys.path.append(path.dirname(path.dirname(__file__)))

import model.game_play_model

print 'hello from game_play.py'

And you executed it directly with something likepython game_play.pyit would output:

hello from game_play_model.py
hello from game_play.py
Community
  • 1
  • 1
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Thanks I have that file structure and code but it still won't recognise a module named model.game_play_model. My zimp folder also has a _pycache__ folder in it if that makes a difference. I copeied your if statement word for word. Not sure I was meant to. – user3164083 Mar 05 '14 at 04:15
  • 1
    The `if` statement and `sys.path.append()` call have to appear before the `import` statement. – martineau Mar 05 '14 at 08:26
  • 1
    Note you can also use `sys.path.append(path.abspath(path.join(path.dirname(__file__), '..')))` which some feel is clearer about what's being done. – martineau Mar 05 '14 at 20:45
  • 1
    This would be even better IMHO: `sys.path.append(path.dirname(path.dirname(__file__)))`. – martineau Mar 05 '14 at 21:00
0
from Zimp.model import game_play

Should do the trick.

Ruben
  • 1,427
  • 3
  • 17
  • 25
  • Thanks. I got "no module named Zimp.model". I only made Zimp as a folder..So I should create a new module named zimp. And inside it what should I write? Thank you!! – user3164083 Mar 05 '14 at 00:21
  • I think it might be better if you read up on how Python modules work: http://docs.python.org/2/tutorial/modules.html – Ruben Mar 05 '14 at 00:23
  • Thanks, I have already read that. It doesn't say how to create the structure. Only how to work with it once it is done. – user3164083 Mar 05 '14 at 00:24
  • Are you sure? ;) http://docs.python.org/2/tutorial/modules.html#packages. In short, packages are just (sub) directories. Just make sure you have a `__init__.py` file inside the directory. Otherwise it's not recognised as a package/module. Hope that helped. – Ruben Mar 05 '14 at 01:07
  • Thanks, but I do have that file in the directories :( – user3164083 Mar 05 '14 at 04:03