helloworld2013
helloworld2013

Reputation: 3164

Django import module: Unable to import module by name

I have sub-directories inside my django app folder, and on each I was trying to call a module. The issue that I am having is that I am able to import a module by using * but not by name which produces an error "Exception Value: cannot import name [my module]"

from foo import Bar # throws error
from foo import * # works

I dont know if I am missing anything on my settings.py but definitely I have included the app directory on my INSTALLED_APPS and also I have init.py on each directory. I also tried to check if my app folder is included on python paths and it was included.

Any help will be appreciated. THanks in advance

Upvotes: 0

Views: 2787

Answers (3)

Jyoti
Jyoti

Reputation: 37

if this is in django, try doing from . import form from . import models from . import views

This should solve the issue. Apart from that, fist make sure you have a init.py file in the directory.

Upvotes: 0

helloworld2013
helloworld2013

Reputation: 3164

After looking over my directories I found out that I have a file that has the same name as my app/Foo. after removing this it started working.

Foo/
 - Bar.py
Process/
 - Foo.py # deleted this
 - views.py

from Foo import Bar #Foo.py was overriding the app/Foo that I was trying to call

Thanks for all your response!

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599450

I expect you are thinking in terms of Java. In Python, you import things by module, not class name. So if a directory foo contains a file bar.py which defines a class Bar, you must do from foo.bar import Bar, not from foo import Bar.

Upvotes: 2

Related Questions