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

asked Mar 5, 2014 at 0:15

2 Answers 2

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
answered Mar 5, 2014 at 3:12
Sign up to request clarification or add additional context in comments.

4 Comments

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.
The if statement and sys.path.append() call have to appear before the import statement.
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.
This would be even better IMHO: sys.path.append(path.dirname(path.dirname(__file__))).
0
from Zimp.model import game_play

Should do the trick.

answered Mar 5, 2014 at 0:19

5 Comments

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!!
I think it might be better if you read up on how Python modules work: docs.python.org/2/tutorial/modules.html
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.
Are you sure? ;) 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.
Thanks, but I do have that file in the directories :(

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.