简体   繁体   中英

How to use other library be creating your own in python?

I am trying to create my own library in Python with simple functions that I use often use. And sometimes in these functions I use others function from Pandas, Numpy or datetime.

For example:

def tm(time, date):
    hhour = int(time[:2])
    mminute = int(time[3:5])
    ssecond = int(time[6:8])
    return(datetime.datetime(date[2], date[1], date[0], hhour, mminute, ssecond))

The problem is that when I import my library in other files, these functions do not work. Even when I import in this file datetime, I still get an error:

NameError: name 'datetime' is not defined

When I run this function directly (by copy-pasting the code) it is working.

How can i import in my file my library with all functions working?

Thanks a lot for any answers!

You'll have to import the datetime somewhere, either in the other .py file that you're calling it from or in the function itself.

def tm(time, date):
    import datetime
    hhour = int(time[:2])
    mminute = int(time[3:5])
    ssecond = int(time[6:8])
    return(datetime.datetime(date[2], date[1], date[0], hhour, mminute, ssecond))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM