Sunday, June 20, 2010

Re: decoupling project name from import?

On Jun 20, 8:01 am, vcarney <vincecar...@gmail.com> wrote:
> I created a new project with django-admin.py as such:
>
> django-admin.py startproject foo
>
> Under the folder foo it gives me the basic files:
> __init__.py
>     manage.py
>     settings.py
>     urls.py
>
> I then created a new folder under foo named apps and added a blank
> __init__.py. Within the apps folder I created a new app with django-
> admin.py instead of manage.py:
>
> django-admin.py startapp bar
>
> This gives me the basic files within bar like models.py. I also
> created a new managers.py file within this same bar directory:
>
> __init__.py
>     managers.py
>     models.py
>     tests.py
>     views.py
>
> In my foo/bar/models.py file, I need to import a class from
> managers.py. I have tried "from bar.managers import PublicManger"
> which gives me an ImportError stating no module is named bar.managers.
> However, when I import with "from foo.apps.bar.managers" it works.
>
> How do I decouple the project name within my apps so I may distribute
> the source code?

The 'apps' directory is not on your pythonpath, so you won't be able
to import directly from it without referencing its parent, which is.
However from the bar.models file 'from managers import PublicManager'
should work, because the current file's containing directory is always
automatically on the pythonpath.

I usually use a similar layout, with apps in an 'apps' subdirectory,
and what I usually do is slightly hack the manage.py script to put
apps on the path:
sys.path.insert(0,
os.path.join(
os.path.realpath(os.path.dirname(__file__)), 'apps'
)
)
(and a similar version in my .wsgi file for production).
--
DR.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment