![](/img/trans.png)
[英]Import files in specified directory into separate pandas dataframes (and name them correspondingly)
[英]How to create separate files for functions, then import them?
我有这个工作代码:
from telegram import Update
from telegram.ext import Updater, CommandHandler, CallbackContext
updater = Updater('token')
def hello(update: Update, context: CallbackContext) -> None:
update.message.reply_text('Hello!')
updater.dispatcher.add_handler(CommandHandler('hello', hello))
updater.start_polling()
updater.idle()
我希望为每个 function 有一个单独的文件,并有一个main.py用于导入它们。
所以我开始用这个创建一个f1.py :
def hello(update: Update, context: CallbackContext) -> None:
update.message.reply_text('Hello!')
然后将其导入到main.py中
from f1 import hello
hello()
显然,这是行不通的(缺少论据)。 我该如何正确地做到这一点?
有关更多信息,这里是导入自己的模块的好来源:
您的main.py
和f1.py
很好,但是在main.py
的updater.dispatcher
上缺少一些东西。 尝试这个:
from telegram import Update
from telegram.ext import Updater, CommandHandler, CallbackContext
#On my bots I prefer to import the entire module than a function from a module,
#because I usually have users.py (all functions for users), clients.py (all
#functions for clients), devs.py (all functions for developer's team) and so on
import f1
updater = Updater('token')
#Calling a function from a module must have a callback, as shown bellow
updater.dispatcher.add_handler(CommandHandler('hello', callback = f1.hello))
updater.start_polling()
updater.idle()
试试这个,让我知道它是否对你有用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.