views:

30

answers:

2

I want to be able to import things from applications in my project without referring to my project name.

My folder structure is like so; I have a project called djangoproject1, and inside I have a folder called apps, and then I have my individual apps in that folder.

djangoproject1, apps, and all my applications have an empty "__init__.py" file in them.

In my settings.py file I have the following code:

import os
import sys
PROJECT_ROOT = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(PROJECT_ROOT, "apps"))

I've tried adding my apps to INSTALLED_APPS several ways:

'djangoproject1.apps.app1', 'djangoproject1.apps.app2',

or

'apps.app1', 'apps.app2',

or

'app1', 'app2',

but nothing works. Whenever I try to do:

from app1 import *

I got an unresolved import error. I'm using the latest versions of eclipse and django

A: 

The statement sys.path.insert(0, os.path.join(PROJECT_ROOT, "apps")) looks OK to me. Following this you only need to add app1 to INSTALLED_APPS and everything should work. But apparently they are not working.

Try the following: 1. Print your sys.path and verify that your project's app directory is in the list. 2. Double check that your have an __init__.py inside your apps folder.

Update

OK. Now can you fire up Django shell and try importing again? If it fails, please post the stack trace here.

Manoj Govindan
1. 'C:\\Users\\jec23\\My Java Projects\\djangoproject1\\src\\djangoproject1\\apps' This is the relevant sys.path entry.2. Yup, thats there.
JPC
A: 

Ok, so I got it to work by adding the apps folder to the PYTHONPATH through eclipse under Project Properties. Is this eclipse only though? I'm not sure how this will work when I try and deploy the site. What do you guys think?

JPC
Ah. That explains things. If you add to `settings.py` and do not execute `shell`, your `PYTHONPATH` does not change. If you want the app to be importable without using `shell` then you will have to add it to the _system variable_ `PYTHONPATH`. You'll can get away by adding apps through settings.py in production.
Manoj Govindan
Ok thanks so, for now then, adding through Eclipse is fine for development, and as long as I leave that code in settings.py, when I deploy it, it should also work fine.
JPC