Python Forum

Full Version: Best programming practice for imports
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
It seems to this newbe, like python programs wants to be built-up from the smallest reusable routines as possible? If that is so, I could end up with a bunch of imports, is this best practice, is there a practical limit to the number of imports? Am I totally off-base?

curbie
I generally just import what I need.
Example would be like
Create a gui that can save, edit, delete information.
I would import tkinter and sqlite3
Then create classes using those for my program.
I could put that all in one file or create modules to import into the main program.

This is just a very basic example.
I don't think there is a limit to how many modules you may import.

However, confusion and name clashes may set in.

People recommend not importing all of a big module. Just import the functions you need, such as:

from openpyxl import Workbook

wb = Workbook()
Modules are fun! Make your own modules to do what you want. I am doing this in an Idle shell.

Working in your own (virtual?) environment, try this:

# make this function and save it as add.py
def add(x, y):
    return x + y

# now import your shiny new function
import add

# now try it
add.add(1, 2)
Amazing output!

Output:
3
I'm not sure what your question is. Are you asking about imports or are you asking a question about module size?

Imports question:
I don't know what you mean by "a bunch of imports". Is that 5, 10, 20?

When importing a module I usually import the module. I would do this:
import tkinter

root = tkinter.Tk()
label = tkinter.Label(root, text="I am a label")
Instead of this:
from tkinter import Tk, Label

root = Tk()
label = Label(root, text="I am a label")
I like using the module name to create a namespace. That way I don't have to worry about names defined in one module colliding with similar names defined in another module. It also makes it really obvious where a function or class comes from.

Module Size Question:
Group your code such that a module's contents all relate to each other in an obvious way and have a purpose that can be described in a few sentences. It doesn't matter if the resulting files are big or small.
I think Pedroski55 answer is close to what I was asking, for example, is it better to lump all related stuff into one big import file, then
from inet import http, mime, smtp
or bust them up into smaller more succinct import files?
import http
import mime
import smtp
Are there advantages to one over the other?

curbie
Think it's more of preference and use
Think it's more of a common practice to do as in deanhystead first example.

My preference is somewhat like deanhystead

I usually do
import tkinter as tk

root = tk.Tk()
label = tk.Label(root, text='some text')
label.pack()
root.mainloop()
Open a terminal and depending on os
Output:
python3 import tkinter as tk print(tk)
This will print out everything you have access to in the module using the tk namespace.
menator01, I’m thinking I have something setup incorrectly...

import tkinter as tk                    # import tkinter (GUI interface) as tk

print(tk)
outputs:

Output:
/home/curbie/PycharmProjects/alpha/amimagev2/.venv/bin/python /home/curbie/PycharmProjects/alpha/amimagev2/00 gui-template.py <module 'tkinter' from '/usr/lib/python3.6/tkinter/__init__.py'> Process finished with exit code 0
I’m running Unumtu 18.04.6 LTS,

PyCharm 2024.2.3 (Community Edition)
Build #PC-242.23339.19, built on September 25, 2024
Runtime version: 21.0.4+13-b509.17 amd64 (JCEF 122.1.9)
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Toolkit: sun.awt.X11.XToolkit
Linux 4.15.0-213-generic
GC: G1 Young Generation, G1 Concurrent GC, G1 Old Generation
Memory: 2048M
Cores: 8
Registry:
ide.experimental.ui=true
i18n.locale=
Current Desktop: XFCE

TERMINAL
python --version

returns
Python 2.7.17

But I downloaded and setup Python-3.12.4

Would some Ubuntu application have downloaded Python?
should be print(dir(tk)). I mistyped in my response
Looks like pycharm is not setup for python3. Someone that uses pycharm will need to help set that up.
ok, Settings > Project > Interpreter = Python 3.6 although "python --version" in terminal returns Python 2.7.17.

curbie