Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"import datetime" v.s. "from datetime import datetime"

I have a script that needs to execute the following at different lines in the script:

today_date = datetime.date.today() date_time = datetime.strp(date_time_string, '%Y-%m-%d %H:%M') 

In my import statements I have the following:

from datetime import datetime import datetime 

I get the following error:

AttributeError: 'module' object has no attribute 'strp' 

If I change the order of the import statements to:

import datetime from datetime import datetime 

I get the following error:

AttributeError: 'method_descriptor' object has no attribute 'today' 

If I again change the import statement to:

import datetime 

I get the following error:

AttributeError: 'module' object has no attribute 'strp' 

What is going on here and how do I get both to work?

like image 962
codingknob Avatar asked Mar 29 '13 16:03

codingknob


People also ask

What is the difference between from datetime import datetime and import datetime?

datetime refers to the datetime class within the datetime module. If you do 'from datetime import datetime', you're only importing the datetime class, so when you refer to 'datetime' in your code, it's referring to the datetime class, not the whole datetime module.

Do you need to import datetime in Python?

datetime is a built-in Python module. Developers don't need to install it separately. It's right there. With the datetime module, you get to work with different classes that work perfectly with date and time.

What does import datetime do in Python?

In Python, date and time are not a data type of their own, but a module named datetime can be imported to work with the date as well as time. Python Datetime module comes built into Python, so there is no need to install it externally. Python Datetime module supplies classes to work with date and time.

What is the difference between datetime and time module in Python?

The time module is principally for working with Unix time stamps; expressed as a floating point number taken to be seconds since the Unix epoch. the datetime module can support many of the same operations, but provides a more object oriented set of types, and also has some limited support for time zones.


2 Answers

Your trouble is that you have some code that is expecting datetime to be a reference to the datetime module and other code that is expecting datetime to be a reference to the datetime class. Obviously, it can't be both.

When you do:

from datetime import datetime import datetime 

You are first setting datetime to be a reference to the class, then immediately setting it to be a reference to the module. When you do it the other way around, it's the same thing, but it ends up being a reference to the class.

You need to rename one of these references. For example:

import datetime as dt from datetime import datetime 

Then you can change references in the form datetime.xxxx that refer to the module to dt.xxxx.

Or else just import datetime and change all references to use the module name. In other words, if something just says datetime(...) you need to change that reference to datetime.datetime.

Python has a fair bit of this kind of thing in its library, unfortunately. If they followed their own naming guidelines in PEP 8, the datetime class would be named Datetime and there'd be no problem using both datetime to mean the module and Datetime to mean the class.

like image 147
kindall Avatar answered Sep 21 '22 14:09

kindall


You cannot use both statements; the datetime module contains a datetime type. The local name datetime in your own module can only refer to one or the other.

Use only import datetime, then make sure that you always use datetime.datetime to refer to the contained type:

import datetime  today_date = datetime.date.today() date_time = datetime.datetime.strptime(date_time_string, '%Y-%m-%d %H:%M') 

Now datetime is the module, and you refer to the contained types via that.

Alternatively, import all types you need from the module:

from datetime import date, datetime  today_date = date.today() date_time = datetime.strptime(date_time_string, '%Y-%m-%d %H:%M') 

Here datetime is the type from the module. date is another type, from the same module.

like image 22
Martijn Pieters Avatar answered Sep 19 '22 14:09

Martijn Pieters