Imports, works from directory but not from file

12 views
Skip to first unread message

When ideas fail

unread,
Nov 10, 2009, 5:12:29 PM11/10/09
to Django users
I have a folder called lib on my path and in there I have a folder
called openid.

If i want to import things from openid i have a problem.

For example if I have these 2 import statements:

from openid.yadis import xri
from openid.association import Association as OIDAssociation

yadis is a directory (in openid) and then there is xri.py inside. This
works fine.
association is a .py file not in a subdirectory, just in lib/openid
this produces the following error:

Traceback (most recent call last):
File "C:\TheAbeonaFile\Apache2.2\MyProject\test.py", line 18, in
<module>
from openid.association import Association as OIDAssociation
ImportError: No module named association

I don't see why one would be a problem and not the other, does anyone
have any ideas?

thanks

Bill Freeman

unread,
Nov 10, 2009, 5:18:27 PM11/10/09
to django...@googlegroups.com
Which all of these directories are on your python path, and which of
them have __init__.py files?

Bill

[Hint to answer first question:

from pprint import pprint as pp
import sys
pp(sys.path)

]

When ideas fail

unread,
Nov 10, 2009, 5:24:53 PM11/10/09
to Django users
I used this in my settings.py to add the open id folder on my path
sys.path.append(os.path.join(FILE_ROOT, 'lib', 'openid'))

and on my path it say:
'C:\\TheAbeonaFile\\Python26\\Lib\\idlelib\\lib\\openid'

And both the yadis file and openid file have an init.py

On 10 Nov, 22:18, Bill Freeman <ke1g...@gmail.com> wrote:
> Which all of these directories are on your python path, and which of
> them have __init__.py files?
>
> Bill
>
> [Hint to answer first question:
>
> from pprint import pprint as pp
> import sys
> pp(sys.path)
>
> ]
>
> On Tue, Nov 10, 2009 at 5:12 PM, When ideas fail
>

Bill Freeman

unread,
Nov 10, 2009, 6:46:32 PM11/10/09
to django...@googlegroups.com
Did you really mean "init.py", rather than the required "__init__.py".
If so, that's wrong.

I presume that the names of the files and directories are all lower
case and contain no spaces or accented characters.

I presume that you've checked and rechecked the spelling of the file
name against the spelling in the import statement.

It is just possible that it's a permission problem. Can you open the
files for reading? In the manage.py shell try (and assuming FILE_ROOT
is the current directory):

open('lib/openid/association.py').readline()

If that doesn't give an exception, but just prints the first line of
the file, good.

If there's an association.pyc remove it and try again (write
permission problem to replace file
with compiled python for the version of the interpreter in use, though
I think it would work fine
anyway).

I'd expect a different exception, but maybe a problem in
association.py is preventing it from importing. Try adding:

import pdb;pdb.set_trace()

to the top of association.py and see if it gets there and stops, then
step along ("n" command) to see that it all completes.

Check that the file doesn't have Apple line endings ('\r' only). I
know that both DOS and *nix line endings work on Windows ('\r\n' and
'\n' respectively).

There could be problems if the file is in a unicode variant as opposed
to simple 7 bit ASCII, though I hope not. That would probably be
clear from the open test above.

Check that the __init__.py files are either empty, or have valid
python contents.

Beyond that, I'm out of ideas. Good luck.

Bill

Bill Freeman

unread,
Nov 11, 2009, 10:15:34 AM11/11/09
to django...@googlegroups.com
I had another couple of thoughts.

In the manage.py shell, try:

from openid.yadis import xri
xri

The output will include from where it got xri. Make sure that it's
coming from your lib directory, and not from some old system wide
installation of openid.


Instead of:

from openid.association import Association as OIDAssociation

try:

from association import Association as OIDAssociation


Also, expecially if the previous worked, in your settings.py, instead of:

sys.path.append(os.path.join(FILE_ROOT, 'lib', 'openid'))

try

sys.path.append(os.path.join(FILE_ROOT, 'lib'))

Bill

andreas schmid

unread,
Nov 11, 2009, 10:34:19 AM11/11/09
to django...@googlegroups.com
im experiencing the same problem.
i started with a simple app and the modules.py was at the root of the
app package, so everything was fine.
now i extended the app and restructured the files in it by making a
subfolder app/models and inside an __init__.py and the mymodel.py

in the urls.py which i have an import like
from app.models.mymodel import myclass

and while the django shell is able to import it without any problem the
django server isnt. im using buildout to build the whole django
environment so the python interpreter im starting the shell and the one
of the server is the same.

i already tried to remove all .pyc and start everything again but i had
no luck.

this is weird and i cant figure out what the problem could be.

Bill Freeman

unread,
Nov 11, 2009, 2:17:29 PM11/11/09
to django...@googlegroups.com
Does your project root (I'm assuming that's the directory containing
the sub directory "app") have an __init__.py file. Does the app
directory? I'm assuming that the one you mention below is in
app/models/ . All three are required, if what you have on your
sys.path is just the project root.

Do make sure that your project root is on your sys.path. You can do
that by temporarily adding:

import foo

to settings.py, and creating a file foo.py in the project root containing, say:

print "Foo!"

and then do:

python manage.py shell

As part of the startup printout you should get a line containing only:

Foo!

By the way, I'm pretty sure that lots of django internal machinery,
like syncdb, for example, depends on there being a file named exactly
"models.py" in each app. So a better choice of directory structure
might be "app/myapp/models.py", with your myclass class in the
models.py file. Pinax uses such a structure, though the upper
directory is called apps instead of app (there may be more than one)
and has been added to the path, so that you can mention it in
INSTALLED_APPS as just 'myapp', rather than 'apps/myapp'. If you go
this route, you will want to insert the apps directory at or near the
beginning of sys.path, rather than appending it, so that when you
mention a possibly installed app, such as django_microblogging, but
which you have customized in a copy in your apps folder, you can say
'microblogging' in your INSTALLED_APPS and it will find yours rather
than the system version. (TN.B.: emplate paths are a separate issue.)

Bill

andreas schmid

unread,
Nov 12, 2009, 2:39:25 AM11/12/09
to django...@googlegroups.com
Bill Freeman wrote:
> Does your project root (I'm assuming that's the directory containing
> the sub directory "app") have an __init__.py file. Does the app
> directory? I'm assuming that the one you mention below is in
> app/models/ . All three are required, if what you have on your
> sys.path is just the project root.
>
>
my app is in the pythonpath of the environment and every directory has
an __init__.py
everything worked fine till i changed the structure from:

app/
__init__.py
models.py
views.py
urls.py

to:

app/
models/
__init__.py
models.py
views.py/
__init__.py
whatever.py
another.py
urls/
__init__.py
myurls.py
anotherurls.py
__init__.py

so it doesnt really make sense that i cant import something in the
second case from my point of view.
> --~--~---------~--~----~------------~-------~--~----~
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com
> To unsubscribe from this group, send email to django-users...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en
> -~----------~----~----~----~------~----~------~--~---
>
>
>

Bill Freeman

unread,
Nov 12, 2009, 10:27:25 AM11/12/09
to django...@googlegroups.com
If that wasn't a typo, I suspect that you're going to have trouble
with having a directory
named "views.py". I don't know how you import anything from it unless
it is directly on
your path.

When you say "my app is in the pythonpath of the environment":
1. Which directory is in the python path?
2. If you mean that you've added it to the PYTHONPATH environment
variable then
please check whether you can find it in sys.path when running in the manage.py
shell. (If you don't know how to do that, please ask.) I don't know
if your on Windows
or something else, but, especially on Windows, I don't take it for
granted that environment
settings will be properly respected/available.

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

andreas schmid

unread,
Nov 12, 2009, 10:33:57 AM11/12/09
to django...@googlegroups.com
Bill Freeman wrote:
> If that wasn't a typo, I suspect that you're going to have trouble
> with having a directory
> named "views.py".
yes it was a typo... my dir is called 'views'
> I don't know how you import anything from it unless
> it is directly on
> your path.
>
> When you say "my app is in the pythonpath of the environment":
> 1. Which directory is in the python path?
>
my pythonpath is called 'site-packages' and this one is in the
pythonpath. every packages in there has an __init__.py in each subdir.
> 2. If you mean that you've added it to the PYTHONPATH environment
> variable then
> please check whether you can find it in sys.path when running in the manage.py
> shell. (If you don't know how to do that, please ask.) I don't know
> if your on Windows
> or something else, but, especially on Windows, I don't take it for
> granted that environment
> settings will be properly respected/available.
>
im on a unix system and i can import it on the django shell but it makes
troubles when running the django server which doesnt make sense

rebus_

unread,
Nov 12, 2009, 11:14:24 AM11/12/09
to django...@googlegroups.com
I am not sure if i got the problem right, but this is what i think you could do.

If you want to keep your views in directory:

app/
views/
__init__.py <- This is where you keep your views so you don't
even need to change your urls.py or anthing
something_else.py <- something else

Also you can keep your models in models/__init__.py and don't need to
change any imports, like:

app/
models/
__init__.py <- this is where your keep your models

On the other hand you can have

app/
models/
other_models.py <- this is where the models are for example
__init__.py <- this is where you import your models so they can
be imported

in __init__.py you say
from other_models import * (or Model1, Model2 etc)

And where every you used your models like
from app.models import Model still works as expected.

Same goes for views.

Bill Freeman

unread,
Nov 12, 2009, 11:38:35 AM11/12/09
to django...@googlegroups.com
On Thu, Nov 12, 2009 at 10:33 AM, andreas schmid <a.sch...@gmail.com> wrote:
>>
> im on a unix system and i can import it on the django shell but it makes
> troubles when running the django server which doesnt make sense


It may be that it won't be recognized as an app because the app
directory doesn't
have a models.py file in it. The app logic decides that an app listed
in settings.py
INSTALLED_APPS has a models.py. I don't know off hand whether the test is made
by looking for that name in the directory, in which case your
configuration would fail,
by doing "import app.models" to see if it gets an exception, in which
case your code
passes, but your models wouldn't be available unless you put stuff in
app/models/__init__.py to import your other models files.

Bill
Reply all
Reply to author
Forward
0 new messages