unable to import Modules Present in subdirectores

175 views
Skip to first unread message

srinivasan panneer

unread,
Jun 29, 2011, 3:51:36 AM6/29/11
to django...@googlegroups.com
Hi Guys

I am very new to Python and Django and having problem with importing modules in my project,

I am following the tutorial, which i downloaded from django website. Where I have the following folder hierarchy in my Django project.(using Pycharm IDE)


HelloWorldDjango
HelloWorldDjango/HelloWorldDjangoApp
HelloWorldDjango/HelloWorldDjangoApp/polls

I am able to import modules from my site folder which is HelloWorldDjangoApp, but having problem in importing from the subfolders. 
For example I have Views.py in polls directory and iam not able to import it.

It is unable to find the modules in the subdirectories like  polls.views 

What do i do?   i believe some Env variable has to be modified, Because currently the    sys.path has path upto my HelloWorldDjangoApp directory.. What do i do for subdirectories.

Thank you in advance.. 

 
Regards, 
Srini
 
 
 
 

 

 
 

Tim Shaffer

unread,
Jun 29, 2011, 9:33:00 AM6/29/11
to django...@googlegroups.com
You do have a __init__.py file in each of the sub folders, correct?

Bill Freeman

unread,
Jun 29, 2011, 9:41:01 AM6/29/11
to django...@googlegroups.com
A couple of basic python things for you to check:

1: You must have a file named "__init__.py" (it may be an empty file)
in any directory that is to occur before a "." in an import statement.
The presence of this file makes the directory a "package", just like
having a name ending in ".py" makes a file a module. You can import
modules or sub-packages from packages, but not from directorys that
aren't packages, unless the directory is explicitly on sys.path.

2: Python imported names are case sensitive, even if your filesystem
is not. Thus you cannot import a module named "Views.py" using the
name "views", as in "polls.views". You would have to use, for
example, "polls.View". If the name of the package is "Polls" then you
would have to use "Polls.Views".

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=en.
>

bruno desthuilliers

unread,
Jun 29, 2011, 11:26:30 AM6/29/11
to Django users


On Jun 29, 3:41 pm, Bill Freeman <ke1g...@gmail.com> wrote:
> A couple of basic python things for you to check:
(...)
> 2: Python imported names are case sensitive, even if your filesystem
> is not.  Thus you cannot import a module named "Views.py" using the
> name "views", as in "polls.views".  You would have to use, for
> example, "polls.View".  If the name of the package is "Polls" then you
> would have to use "Polls.Views".

The obvious conclusion being: only use all_lower names for your
packages and modules names.

Andre Terra

unread,
Jun 29, 2011, 12:47:33 PM6/29/11
to django...@googlegroups.com
This can't be stressed enough.

Please read http://www.python.org/dev/peps/pep-0008/ and try to follow it as best as you can.



Sincerely,

André Terra


--

Kenneth Gonsalves

unread,
Jun 29, 2011, 11:15:04 PM6/29/11
to django...@googlegroups.com
On Wed, 2011-06-29 at 03:51 -0400, srinivasan panneer wrote:
> For example I have Views.py

is it Views.py or views.py?
--
regards
KG
http://lawgon.livejournal.com
Coimbatore LUG rox
http://ilugcbe.techstud.org/

Reply all
Reply to author
Forward
0 new messages