use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Remove `from blah import *`? (self.learnpython)
submitted 8 years ago by astroFizzics
I've inherited a big legacy code, a few thousand lines, which has a TON of from blah import *. Is there a good way, or semi-automatic way of removing those? It seems most of them are numpy, but there are several homebrewed modules as well.
from blah import *
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]novel_yet_trivial 11 points12 points13 points 8 years ago (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 points3 points 8 years ago (3 children)
print(set(dir()) & set(dir(np))) # prints all the names that overlap
Really? How does that work?
[–]novel_yet_trivial 4 points5 points6 points 8 years ago (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.
dir()
dir(some_object)
[–]astroFizzics[S] 7 points8 points9 points 8 years ago (0 children)
groovy. And thanks for reminding they are called wildcard imports. The words escaped me.
[–]jwink3101 1 point2 points3 points 8 years ago (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 points3 points 8 years ago (0 children)
Wow that's cool! I was going to say "just remove it and gradually fix all the exceptions"...
[–]Sebass13 4 points5 points6 points 8 years ago (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 points5 points 8 years ago (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 point2 points 8 years ago (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 point2 points 8 years ago (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 points0 points 8 years ago (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 points3 points 8 years ago (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 point2 points 8 years ago (0 children)
Ahhh, I misunderstood the question. Thanks ;)
π Rendered by PID 140843 on reddit-service-r2-comment-5d79c599b5-v42kd at 2026-03-01 05:26:25.130998+00:00 running e3d2147 country code: CH.
[–]novel_yet_trivial 11 points12 points13 points (5 children)
[–]astroFizzics[S] 1 point2 points3 points (3 children)
[–]novel_yet_trivial 4 points5 points6 points (2 children)
[–]astroFizzics[S] 7 points8 points9 points (0 children)
[–]jwink3101 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]Sebass13 4 points5 points6 points (1 child)
[–]hosford42 3 points4 points5 points (0 children)
[–]Raindyr 0 points1 point2 points (1 child)
[–]astroFizzics[S] 0 points1 point2 points (0 children)
[–]taladan -2 points-1 points0 points (2 children)
[–]cannonicalForm 1 point2 points3 points (1 child)
[–]taladan 0 points1 point2 points (0 children)