Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » decoupling project name from import? [RSS Feed]

#1 June 20, 2010 13:19:54

v.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

decoupling project name from import?


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?

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

Offline

#2 June 20, 2010 15:15:20

Sithembewena L.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

decoupling project name from import?


Am a Django noob, and I do not know what your Django proficiency is, but I'd
think you have compromised the strict directory structure that Django
created. I would:

- Check that your new "apps" folder is on your PATH,
- Consider having the apps folder outside of your foo project,
- Seehttp://docs.djangoproject.com/en/1.2/intro/tutorial03/for a word on
decoupling apps.

On Sun, Jun 20, 2010 at 9: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?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
>http://groups.google.com/group/django-users?hl=en.
>
>


--
Regards,
Sithembewena Lloyd Dubehttp://www.lloyddube.com--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 June 20, 2010 17:26:49

Daniel R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

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-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 June 21, 2010 04:10:39

v.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

decoupling project name from import?


I attempted to add my apps folder to the wsgi script file, but still
no luck (same module import error). Here is what I have in my wsgi
script file:

import os
import sys

os.environ = 'example.settings'
sys.path.append('/apps/django')
sys.path.append('/apps/django/example/apps')
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Any thoughts?

On Jun 20, 11:26 am, Daniel Roseman <dan...@roseman.org.uk> wrote:
> 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-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 June 21, 2010 04:23:32

v.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

decoupling project name from import?


I've now got this working by also adding the following to my projects
settings.py file:

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

manage.py now knows about my apps folder. See Greg Allard's post for
more info if you run into this -http://codespatter.com/2009/04/10/how-to-add-locations-to-python-path-for-reusable-django-apps/.

On Jun 20, 10:10 pm, vcarney <vincecar...@gmail.com> wrote:
> I attempted to add my apps folder to the wsgi script file, but still
> no luck (same module import error). Here is what I have in my wsgi
> script file:
>
> import os
> import sys
>
> os.environ = 'example.settings'
> sys.path.append('/apps/django')
> sys.path.append('/apps/django/example/apps')
> import django.core.handlers.wsgi
> application = django.core.handlers.wsgi.WSGIHandler()
>
> Any thoughts?
>
> On Jun 20, 11:26 am, Daniel Roseman <dan...@roseman.org.uk> wrote:
>
> > 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-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#6 June 21, 2010 07:55:28

Sithembewena L.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

decoupling project name from import?


Thanks everyone, I could use that link myself.

On Mon, Jun 21, 2010 at 5:23 AM, vcarney <vincecar...@gmail.com> wrote:

> I've now got this working by also adding the following to my projects
> settings.py file:
>
> import os
> import sys
> PROJECT_ROOT = os.path.dirname(__file__)
> sys.path.insert(0, os.path.join(PROJECT_ROOT, "apps"))
>
> manage.py now knows about my apps folder. See Greg Allard's post for
> more info if you run into this -
>
>http://codespatter.com/2009/04/10/how-to-add-locations-to-python-path-for-reusable-django-apps/> .
>
> On Jun 20, 10:10 pm, vcarney <vincecar...@gmail.com> wrote:
> > I attempted to add my apps folder to the wsgi script file, but still
> > no luck (same module import error). Here is what I have in my wsgi
> > script file:
> >
> > import os
> > import sys
> >
> > os.environ = 'example.settings'
> > sys.path.append('/apps/django')
> > sys.path.append('/apps/django/example/apps')
> > import django.core.handlers.wsgi
> > application = django.core.handlers.wsgi.WSGIHandler()
> >
> > Any thoughts?
> >
> > On Jun 20, 11:26 am, Daniel Roseman <dan...@roseman.org.uk> wrote:
> >
> > > 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-us...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
>http://groups.google.com/group/django-users?hl=en.
>
>


--
Regards,
Sithembewena Lloyd Dubehttp://www.lloyddube.com--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » decoupling project name from import? [RSS Feed]

Board footer

Moderator control

Enjoy the 15th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support