Importing external modules/classes

620 views
Skip to first unread message

cla...@gmail.com

unread,
Apr 13, 2006, 6:20:21 PM4/13/06
to Django users
Hello,

I'm fairly new to Django, so apologize if this question is fairly
basic. (I did search for related topics but didn't find anything that
seemed to answer my exact question.)

I've developed a library of code in python that I reuse across multiple
projects, and I wanted to be able to use some of these classes/modules
with Django but I've had some problems. I have defined the PYTHONPATH
env variable (in my .bash_profile on OS X and Linux), but Django
doesn't seem to recognize/acknowledge it as far as I can tell. What do
I need to do to be able to import my own classes not located under the
current Django project?

Thanks,
Cary

P.S. Django looks really cool, keep up the good work!

Joseph Kocherhans

unread,
Apr 13, 2006, 6:29:27 PM4/13/06
to django...@googlegroups.com
On 4/13/06, cla...@gmail.com <cla...@gmail.com> wrote:
>
> I've developed a library of code in python that I reuse across multiple
> projects, and I wanted to be able to use some of these classes/modules
> with Django but I've had some problems. I have defined the PYTHONPATH
> env variable (in my .bash_profile on OS X and Linux), but Django
> doesn't seem to recognize/acknowledge it as far as I can tell. What do
> I need to do to be able to import my own classes not located under the
> current Django project?

A couple of things that might bite you. If you have 2 modules with the
same name on your PYTHONPATH, you won't always get what you expect
(this one always takes me forever to figure out, the module imports
fine but it doesn' have the right classes, etc.) Also, you might want
to try this in python interpreter:

import sys
sys.path

Make sure the proper directory is in the list of paths.

Also, are you running django's builtin server, or running django under apache?

Joseph

arthur debert

unread,
Apr 13, 2006, 6:31:25 PM4/13/06
to Django users
Hi Cary.

that depends if you're using magic-removal or trunk. On the m-r branch
there are no "magic" modules, whatever is on your path can be imported
as usual.

if you're using trunk, you must import inside your models methods, or
user module_constants (
http://www.djangoproject.com/documentation/model_api/#meta-options )

see also this thread:
http://groups.google.com/group/django-users/browse_frm/thread/c3c4a1ff076ae357/a6a38c1e23f52ff6?q=imports+magic&rnum=1#a6a38c1e23f52ff6

DavidA

unread,
Apr 14, 2006, 8:15:58 AM4/14/06
to Django users
Hi Cary,

What I did to avoid all the PYTHONPATH stuff was to just 'install' my
python library using distutils. I set up a trivial setup.py script and
then I just run 'python setup.py install' for my library whenever I
make changes.

from distutils.core import setup

setup(name='pf',
version='0.1',
packages=['pf', 'pf.utils', 'pf.task'],
)

While forgetting to install changes has tripped me up once or twice,
I've done this enought that its "wired" into my fingers now. While this
might seem a bit of overkill, using distutils is so simple that I don't
mind it.
-Dave

Reply all
Reply to author
Forward
0 new messages