开发者

Installing Django Apps outside of the project folder?

For reasons of utter silliness, I want to completely change the way my Django project's filesystem is laid out. In particular, I want to move the actual apps making up the website into separate folders from the admin/settings files. For example, rather than:

django/
    site/
        __init__.py
        manage.py
        settings.py
        urls.py
        home/
            __init__.py
            views.py
            models.py
            template.html
        page/
            __init__.py
            views.py
            models.py
            template.html
        media/
            __init__.py
            styles.css
            picture1.jpg
            picture2.jpg

I want it to be:

django/
    site/
        __init__.py
        manage.py
        settings.py
        urls.py
    home/
        __init__.py
        views.py
        models.py
        template.html
    page/
        __init__.py
        views.py
        models.py
        template.html
    media/
        __init__.py
        styles.css
        picture1.jpg
        picture2.jpg

because i think the N-ary tree of my filesystem to be as balanced as possible, minimizing its height to guarantee O(lgn) search times for any particular file!!!

I have managed to set up my django.wsgi such that all my "external" folders are part of the system path when python runs, so imports work perfectly. Similarly, i did a os.chdir() to make loading-from-filesystem work perfectly too. The URL mapper also works perfectly, directing the incoming requests to these "external" modules to handle.

However, i did not managed to get manage.py to load these "external" modules, presumably because it doesn't run through django.wsgi and thus the sys.path modifications did not apply to them. Hence, when i put these external folders into INSTALLED_APPS (e.g. "home" rather than "site.home"), it cannot find them, giving me:

Error: No module named home

Whenever I try to python manage.py syncdb. I have tried setting the PYTHONPATH 开发者_如何学Cenvironmental variable to /home/django in the hope that that will cause any and every python script run to check that folder and load my "external" modules, but that failed too. Is there anything else i can do to get manage.py to check these external folders when it loads its stuff?


With a setup like that, you could even completely separate your code from django site configuration:

site/
    yoursitename/
        __init__.py
        manage.py
        settings.py
        urls.py
src/
    your/
        namespaced/
            apps/
                app1/
                    models.py...
                app2/...

And in the settings.py just load the "src" dir into the PYTHONPATH:

SITE_PATH = os.path.abspath(os.path.dirname(__file__))
PROJECT_PATH = os.path.normpath(os.path.join(SITE_PATH, '..', '..'))
SRC_PATH = os.path.join(PROJECT_PATH, 'src')
if SRC_PATH not in sys.path:
    sys.path.insert(0, SRC_PATH)

In the end, all the "reusable" apps you install as eggs or pips work the same way, they just live in the pythonpath.


I can't say that I agree with your motivations, but I believe the problem is still with your PYTHONPATH. In your wsgi script try

import sys

sys.path += ['/home/django']


So many things key off of the settings file and/or are written relative to manage.py that at the very least I'd keep those in the root directory.

django/
    __init__.py
    manage.py
    settings.py

    site/
        __init__.py
        urls.py
        other stuff.py
    home/
        __init__.py
        views.py
        models.py
        template.html
    page/
        __init__.py
        views.py
        models.py
        template.html
    media/
        __init__.py
        styles.css
        picture1.jpg
        picture2.jpg


If you package up your apps with a setup.py file, you can 'install' them into your virtualenv. This will make them available for that environment, but not anywhere else.

I do this, even for non-reusable apps. Part of the deployment process is to run 'pip install ' for each app I have (or have the apps pushed to a private pypi/cheeseshop server). Development uses 'pip install -e ' which means I can edit and run without having to re-setup each time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜