How to create separate files for functions, then import them?

privm

I have this working code:

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

I wish to have a separate file for each function and have a main.py where I import them.

So I head to create a f1.py with just this:

def hello(update: Update, context: CallbackContext) -> None:
    update.message.reply_text('Hello!')

Then import it into a main.py with

from f1 import hello
hello()

Obviously, this is not working (missing arguments). How do I do it correctly?

Rafael Colombo

Your main.py and f1.py are fine, but on updater.dispatcher of your main.py something is missing. Try this:

from telegram import Update
from telegram.ext import Updater, CommandHandler, CallbackContext

#On my bots I preffer 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()

Try this and let me know if it worked for you!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python / Tkinter - how to import files or functions without executing them first?

From Dev

How to create separate log files?

From Dev

Shiny - How to separate the Rendering functions into different files?

From Dev

how to create functions in jquery and reusing them

From Dev

How to call RequireJS functions in other files? How to separate RequireJS files?

From Java

How to create separate AngularJS controller files?

From Dev

How to create separate resource files for debugging and releasing?

From Dev

How to properly store React components in separate files and import React?

From Dev

React Native: How to split a file up into multiple files and import them?

From Dev

".c" files called by fiona. How to import them?

From Dev

How to import files that are called the same as the folder that contains them?

From Dev

How can i separate php code with html to have them both clean and separate in different files?

From Dev

How can I create a sequence of functions and loop them in Swift?

From Dev

How to create a file with functions in C and access them from my application?

From Dev

Separate files for functions outside classes

From Dev

how to create separate files for validations in ionic2?

From Java

How to create multiple files with different names and write to them in Python

From Dev

How to configure YAML to create fresh log files instead of appending them?

From Dev

Text files embedded into a DLL: how to dynamically create a dictionary from them

From Dev

How do I use CMD to merge multiple text files and separate them with line breaks?

From Dev

How to create separate Pandas DataFrames for each CSV file and give them meaningful names?

From Dev

How can I create separate rooms and allow users to freely move between them?

From Dev

Import list variable from separate files in python

From Dev

How to separate gitignore files?

From Dev

Keep functions in the same helper file or separate files?

From Dev

Declaring and Defining Variables and Functions in Separate Files

From Dev

c++ put functions in separate source files

From Dev

Organising TableViewController custom functions into separate files

From Dev

want to count and separate each key in the array to a variable to assign them functions

Related Related

  1. 1

    Python / Tkinter - how to import files or functions without executing them first?

  2. 2

    How to create separate log files?

  3. 3

    Shiny - How to separate the Rendering functions into different files?

  4. 4

    how to create functions in jquery and reusing them

  5. 5

    How to call RequireJS functions in other files? How to separate RequireJS files?

  6. 6

    How to create separate AngularJS controller files?

  7. 7

    How to create separate resource files for debugging and releasing?

  8. 8

    How to properly store React components in separate files and import React?

  9. 9

    React Native: How to split a file up into multiple files and import them?

  10. 10

    ".c" files called by fiona. How to import them?

  11. 11

    How to import files that are called the same as the folder that contains them?

  12. 12

    How can i separate php code with html to have them both clean and separate in different files?

  13. 13

    How can I create a sequence of functions and loop them in Swift?

  14. 14

    How to create a file with functions in C and access them from my application?

  15. 15

    Separate files for functions outside classes

  16. 16

    how to create separate files for validations in ionic2?

  17. 17

    How to create multiple files with different names and write to them in Python

  18. 18

    How to configure YAML to create fresh log files instead of appending them?

  19. 19

    Text files embedded into a DLL: how to dynamically create a dictionary from them

  20. 20

    How do I use CMD to merge multiple text files and separate them with line breaks?

  21. 21

    How to create separate Pandas DataFrames for each CSV file and give them meaningful names?

  22. 22

    How can I create separate rooms and allow users to freely move between them?

  23. 23

    Import list variable from separate files in python

  24. 24

    How to separate gitignore files?

  25. 25

    Keep functions in the same helper file or separate files?

  26. 26

    Declaring and Defining Variables and Functions in Separate Files

  27. 27

    c++ put functions in separate source files

  28. 28

    Organising TableViewController custom functions into separate files

  29. 29

    want to count and separate each key in the array to a variable to assign them functions

HotTag

Archive