Saturday, January 12, 2019

How To Avoid Wildcard Imports

How To Avoid Using Star Imports

python-wildcard import-title image

I was trying to help a fellow Python beginner on Reddit and noticed he was using star imports.

I advised him that sooner or later he would have to learn how to avoid using wildcard imports.  The same happened to myself, but I finally got around it, with help from a Reddit guy.

At the time I wrote an article about it on this blog, so I referred my new friend here to read up on it, but I had forgotten that I had deleted the post a few months back when I re-hashed this site to only show projects.

How dumb was that?  Nearly every beginner comes across this problem sooner or later, this is valuable information to noobs like us.

So here we are, I decided to rewrite the article.  What I nice guy I am eh?  There’s really only me, Jesus and Gandhi to be honest (I’m joking of course.  I’m a lot nicer then them two!)

What Is A Wildcard Import?

A star import, or a wildcard import are the same thing, it looks like this:

from tkinter import *

It basically means load in all the modules related to tkinter.

This used to be OK years ago apparently, and there are still thousands of old tutorials, books, and articles about that encourages its use.

But it has come to light that in some cases it can cause clashes with other modules and or variables in the namespace.  It’s not worth getting into the fine technical detail here, I am happy to take the word of the experts on this.

If you show your code to a expert while trying to get help from them, s\he is likely to pull you up on star imports, as it is not too hard to learn how to get around this problem.

Another reason, for me personally, is that I use Pylint regularly to check my code, and it always gives me negative points for “Unused wildcard import”.

The second full Python program that I ever wrote, S-Py, the code was rated ny Pylint at -11 (minus eleven), but after changing the wildcard imports it was then rated +8.36, so this has helped to get my code closer to what others need to read it and help.

Note: I am going to write a Pylint for beginners article soon, so I won’t explain what it is here as well.  You can always Google it for now if you are desperate I guess.

How I Got Star Help

I used the “Ask anything Monday” forum on Reddit learn python, to ask this question:

“How do I avoid wildcard imports?”

And I got this answer:

  • Remove the * line, then run the program, Python will say something like ‘Tk’ is not defined
  • Add “from tkinter import Tk”
  • Run again, it will  maybe say something like ‘Checkbutton’ not found
  • You add it to the same line, comma separated
  • from tkinter import Tk, Checkbutton
  • repeat.

And thanks to johnnyjordaan‘s answer, it works, though I had some work on to catch them all.

Note: If you are a beginner you would be crazy not to make r/learnpython/ your home and read it every day.

reddit-learn-python

This can create a new problem

Once I had all the single imports listed it was one very long line, and as far as I was concerned not very readable.  Pylint hates long lines, and punished me further for it.

from tkinter import Tk, Menu, filedialog, messagebox, LabelFrame, RIDGE, Frame, Entry, W, END, Button, E, Label, Listbox, Scrollbar, RIGHT, Y, X, BOTTOM, LEFT

I tried using a “/” to break the line up, but I got an error.  Damn it.

The Marvels Of a Quick Python Snooze

I had my afternoon snooze (don’t laugh, I’m at that age!) and I came back realising it was a back slash, not a forward slash, that I should of used, doh!  So I split the long line up with some  “\”s, and it worked, hurrah.

Here’s how the import lines in my S-Py program looks now.

from tkinter import Tk, Menu, filedialog, messagebox, LabelFrame, RIDGE, \
Frame, Entry, W, END, Button, E, Label, Listbox, Scrollbar, \
RIGHT, Y, X, BOTTOM, LEFT

Now Pylint loves me, and hopefully I may get a little bit of respect when I show code to the pros.  Well, you got to try, innit!

Import Geeky Update

Later on I got really geeky on this, and arranged all the imports in alphabetical order. I also shortened the lines a bit more so that in an editor you will see all the lines fully without scrolling.

This will make it easier to spot if any given module is listed, and genuinely readable in my opinion.

Python-s-py-wildcard import solved

My Pylint score for S-Py now stands at an incredible +9.96, (once I added the doc string on line 1).  Cool.

I was putting the finishing touches to this article, when I decided I needed a screenshot of Pylint showing bad imports, so I typed in, from tkinter import *,  saved it, and ran it through Pylint.  This is what I got:

Python-pylint bad import

Yup rated -1360, ha-ha.  I think Pylint has made its point, don’t you.

 

 

By the way, I have started off another free blog to publish my personal memoirs if that interests anyone.

Using Python V3.6.5(32 bit) on Windows 7, (64 bit).

Previous post:  Python URL Shortener

Advertisements


from Python Coder
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...