简体   繁体   English

如何使用其他库在python中创建自己的库?

[英]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. 我试图用我经常使用的简单函数在Python中创建自己的库。 And sometimes in these functions I use others function from Pandas, Numpy or datetime. 有时在这些函数中,我使用Pandas,Numpy或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. 您必须将datetime导入到您要调用的其他.py文件中或函数本身中的某个位置。

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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