Python Forum

Full Version: How to import entire module ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to import entire module. I dont want to specify each function of that module i plan on using.
For example:
from tkinter import messagebox
How to import whole thing so that messagebox.showinfo("Title", "text") would work ?
you can do from tkinter import * but in general star imports are considered BAD practice and should be avoided at all cost. For example they may cause name collisions. It's also much more difficult to read the code, e.g. one would not know from where messagebox comes from. Still you can see it often used in tkinter tutorials
the best approach would be to use alias, e.g. something like import tkinter as tk and then fully reference the objects and functions you want to use, e.g. tk.messagebox.showinfo("Title", "text")
from tkinter import *
messagebox.showinfo("Title", "Ok")
messagebox.askokcancel("Title","Ok Cancel")
messagebox.askyesnocancel("Title","Yes No Cancel")
This does not work unless i am very specific on what i import.
I tried
from tkinter import *
tkinter.messagebox.showinfo("Title", "Ok")
tkinter.messagebox.askokcancel("Title","Ok Cancel")
tkinter.messagebox.askyesnocancel("Title","Yes No Cancel")
Cant get it to work
messagebox.py don't get imported into global namespace bye using *.
So this file work stand alone and need imported as:
from tkinter import messagebox
Then you don't need to worry about * Evil (that never should be used).
I have 1 problem of using import module that: when i import a module with a few function i've created and i don't know how to insert the definition of that function like others in python's library so that i can see how it works every time when using it.
Its not just mesagebox i want to import, i want to be able to call any function from tkinter library without the need to import each and every one of them i use.
How to import entire library and be able to just call functions instead of importing those functions in order to be able to call them ?
Thanks !
For example:
import tkinter
file = tkinter.filedialog.askopenfile()
print(file)
Why this doesnt work ?