Python Forum
Best programming practice for imports
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Best programming practice for imports
#1
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
Reply
#2
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 welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#3
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
Reply
#4
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.
Reply
#5
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
Reply
#6
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.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#7
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?
Reply
#8
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.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#9
ok, Settings > Project > Interpreter = Python 3.6 although "python --version" in terminal returns Python 2.7.17.

curbie
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sudden Extremely Slow / Failed Python Imports bmccollum 1 1,201 Aug-20-2024, 02:09 PM
Last Post: DeaD_EyE
  how can I organise my code : best practice? pythonbum 0 601 May-24-2024, 03:17 PM
Last Post: pythonbum
  Imports that work with Python 3.8 fail with 3.9 and 3.10 4slam 1 3,520 Mar-11-2022, 01:50 PM
Last Post: snippsat
  Imports in my first package cuppajoeman 1 2,507 Jun-28-2021, 09:06 AM
Last Post: snippsat
  script with imports works but pytest gives "ModuleNotFoundError"? Hpao 0 2,138 Jun-27-2021, 08:30 PM
Last Post: Hpao
  Help wanted with python imports petros21 3 3,565 Apr-07-2021, 07:16 PM
Last Post: snippsat
Question How to include Modules not found (conditional imports) in my setup.py when I want to cff 0 4,849 Mar-17-2021, 11:57 AM
Last Post: cff
  threading across imports Nickd12 2 3,059 Nov-09-2020, 01:59 AM
Last Post: Nickd12
  refreshing imports seandepagnier 4 3,857 Sep-20-2020, 11:51 PM
Last Post: seandepagnier
  Multimode imports fine as script but not after compiling into exe chesschaser 0 2,937 Aug-13-2020, 01:28 PM
Last Post: chesschaser

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020