0

I'm learning Python and the imports that have accumulated whilst developing my app seem like I'm repeating imports. Please can someone advise me on development practices.

from pathlib import Path
from urllib.request import urlopen
from gi.repository import Gtk, Gio
from gi.repository import GLib as glib
from gtk_assistant import AssistantApp

import urllib.request
import urllib.error
import xml.etree.ElementTree as ET
import json
import gi
import sys
import os
import hashlib
2
  • EDIT: Please consult PEP8 first. That being said, you could compress your imports a little bit while maintaining the same code afterwards. e.g. gi.repository imports could be grouped, but I am not sure this is considered best practice. Ultimately they should work best for the readability of the following code. You could try if e.g. PyCharm's Optimize import feature produces something to your liking. I like to use import ... as much as possible, so that I know everywhere in my code, where something is coming from. Commented Jul 26, 2019 at 19:15
  • When concerned about coding guidelines and practices, the first place I look is PEP 8 (python.org/dev/peps/pep-0008/#imports). TLDR: builtin imports first, third party imports second, and finally your own local imports last with a blank line between each group. Commented Jul 26, 2019 at 19:18

1 Answer 1

1

You can group imports like this if you want:

from urllib import request, error
from xml.etree import ElementTree as ET
import json, gi, sys, os, hashlib

However, the pep8 style guide says you should have them on seperate lines so the way you did it is fine

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.