cannot import name current_datetime

168 views
Skip to first unread message

asherakhet06

unread,
Apr 18, 2012, 6:47:12 PM4/18/12
to django...@googlegroups.com
H all!

I am fairly new to programming (went over LPTHW) and now going through the Djangobook on the internet.  Quick beginners question:  In Chapter 3, where I am looking at a dynamic webpage in the 2nd example I am running into some problems with the current_datetime function I am trying to set up. When, in views.py I insert the "datetime.datetime.now()" statement and "import current_datetime" into my urls.py file, I get the following error message when I try to call up the webpage via runserver.  It seems like my pure python script is not being picked up by the server: 

"ImportError cannot import name current_datetime".

I know this is most likely an easy question for most of you, but it's been a real puzzle for me to be honest:/  Anybody have any tips on how to solve this error?  I know it is really important to be careful to copy EXACTLY as mentioned in the programming book(s), so I am pretty sure I am not making any spelling/character errors.  I am using windows btw.  I also have python 2.7 and django 1.4 installed on my computer.  Anybody kind enough to help me out?  Much appreciated!    

Babatunde Akinyanmi

unread,
Apr 18, 2012, 7:49:23 PM4/18/12
to django...@googlegroups.com
I have a feeling you want to do:
Import datetime not import current_datetime

> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/K5CG719gkDoJ.
> 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.
>
>

--
Sent from my mobile device

bruno desthuilliers

unread,
Apr 19, 2012, 10:59:39 AM4/19/12
to Django users
On Apr 18, 8:47 pm, asherakhet06 <francisco.demira...@gmail.com>
wrote:
> H all!
>
> I am fairly new to programming (went over LPTHW) and now going through the
> Djangobook on the internet.  Quick beginners question:  In Chapter 3, where
> I am looking at a dynamic webpage in the 2nd example

Could you please post a link to this resource ?

> I am running into some
> problems with the current_datetime function I am trying to set up. When, in
> views.py I insert the "datetime.datetime.now()" statement and "import
> current_datetime" into my urls.py file

If your import statement is actually "import current_datetime", then
the book is wrong, Python imports dont work that way. You either
import a module then use a qualified name to access the module's
symbols, ie:

from myapp import views
now = views.current_datetime()

or import a symbol from a module and use it directly, ie:

from myapp.views import current_datetime
now = current_datetime()

of course the module (or the package containing the module) must be in
your pythonpath.

for more about the import statement, see

* http://docs.python.org/release/2.7/tutorial/modules.html

and

* http://docs.python.org/release/2.7/reference/simple_stmts.html#the-import-statement

> but it's been
> a real puzzle for me to be honest:/  Anybody have any tips on how to solve
> this error?  I know it is really important to be careful to copy EXACTLY as
> mentioned in the programming book(s),

Books are sometimes wrong, and sometimes a bit outdated.

Timothy Makobu

unread,
Apr 19, 2012, 11:27:25 AM4/19/12
to django...@googlegroups.com
The example works as the book describes  http://www.djangobook.com/en/1.0/chapter03/  

 When, in views.py I insert the "datetime.datetime.now()" statement and "import current_datetime" into my urls.py file, I get the following error message when I try to call up the webpage via runserver.  It seems like my pure python script is not being picked up by the server: 

"ImportError cannot import name current_datetime".



Do it exactly as it says in the book:
from mysite.views import current_datetime

asherakhet06

unread,
Apr 20, 2012, 8:56:15 PM4/20/12
to django...@googlegroups.com
Hey guys,

I had to re-install Python and Django for it to work.  There seemed to have been something wrong with my path/environment settings.  Thank you all for the suggestions and tips though.  Once again, very much appreciated:)    

FYI I had python installed in a wrong path in the and that gave

jacob poke

unread,
Jul 25, 2013, 7:18:33 AM7/25/13
to django...@googlegroups.com
hi - I am having the same problem to this and wondering if anyone found a solution? I dont really want to move on until i solve this. 

I have tried to change the text in the "Hello world" section but the browser continues to show "hello world". I also tried to develop a url similar to "Hello" using my name and this refused to work also. Any ideas?

Mike Dewhirst

unread,
Jul 25, 2013, 10:34:32 PM7/25/13
to django...@googlegroups.com
On 25/07/2013 5:18pm, jacob poke wrote:
> Djangobook on the internet. Quick beginners question: In Chapter 3, where
>>I am looking at a dynamic webpage in the 2nd example I am running into some
>>problems with the current_datetime function I am trying to set up. When, in
>>views.py I insert the "datetime.datetime.now()" statement and "import
>>current_datetime" into my urls.py file, I get the following error message
>>when I try to call up the webpage via runserver. It seems like my pure
>>python script is not being picked up by the server:
>>
>>"ImportError cannot import name current_datetime".

You need to look at a Python tutorial about namespaces to understand
what is happening.

The error is saying you have tried to import something which cannot be
found. There are two solutions:

1. tell Python where to look for current_datetime

2. put the current_datetime function near the top of the file in which
you are calling it.

It is good practice to keep your main program files relatively
uncluttered and put such "utility" functions somewhere they can be
called by lots of different modules. This makes 1. above the preferred
solution.

from .date_utilities import current_datetime

... where the dot indicates that date_utilities.py is in the same
directory as the file within which you make the call. Otherwise, you
need to put date_utilities.py in directory which is on the Python path.
For example:

from utils.date_utilities import current_datetime

hth

Mike


>>
>>

Reply all
Reply to author
Forward
0 new messages