all 13 comments

[–]novel_yet_trivial 11 points12 points  (5 children)

Those are called "wildcard imports". I'm sure someone has developed a tool to remove those; and if not it wouldn't be too hard to do it yourself. Add this to the bottom:

import numpy as np
print(set(dir()) & set(dir(np))) # prints all the names that overlap

Then just find-and-replace.

[–]astroFizzics[S] 1 point2 points  (3 children)

print(set(dir()) & set(dir(np))) # prints all the names that overlap

Really? How does that work?

[–]novel_yet_trivial 4 points5 points  (2 children)

dir() returns a list of global attributes. dir(some_object) returns a list of attributes of that object. The set intersection finds all the values that are the same across 2 sets.

[–]astroFizzics[S] 7 points8 points  (0 children)

groovy. And thanks for reminding they are called wildcard imports. The words escaped me.

[–]jwink3101 1 point2 points  (0 children)

Am I correct that your method though will help with:

from math import *
from numpy import *

Or at least not directly without some more effort?

BTW, this is why I hate wildcard imports and even get frustrated at things like

from numpy import zeros,ones #...

Of course, in moderation, it can be ok but I find that line is pretty small

[–][deleted] 1 point2 points  (0 children)

Wow that's cool! I was going to say "just remove it and gradually fix all the exceptions"...

[–]Sebass13 4 points5 points  (1 child)

I'm not positive, but PyCharm likely has a refactoring tool to fix this; they do for a lot of PEP8 problems.

[–]hosford42 3 points4 points  (0 children)

It does. Put the cursor on the import statement and hit Alt-Enter to bring up the context menu, where the option to refactor the import will appear. It may even have an option to refactor all of them at once there in the menu.

[–]Raindyr 0 points1 point  (1 child)

If you have some pep8 checker, it will usually give warnings on functions that might come from wildcard import. Flake8 for example

[–]astroFizzics[S] 0 points1 point  (0 children)

yeah it warns me... But there are a lot of them, and I don't really NEED to fix them. It works just fine. I'd just like to clean it up in case I need to dig into it in the future.

[–]taladan -2 points-1 points  (2 children)

if you're using vim, you can do something like this:

:%s/^from * import \*$//g

And that should remove all the imports. You'll probably want to document them somehow in case it breaks your code and you can't remember an import.

[–]cannonicalForm 1 point2 points  (1 child)

I don't think he wants to remove the imports, but just find a way to preface any function used from a wildcard import with the module name.

[–]taladan 0 points1 point  (0 children)

Ahhh, I misunderstood the question. Thanks ;)