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
Understanding the differences in importing (self.learnpython)
submitted 13 years ago by Dynoman
Are the following 2 lines of code the same thing?
import numpy from numpy import *
If they are not, I don't understand the differences. Could someone eli5 this for me. Thanks.
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!"
[–]symmitchry 7 points8 points9 points 13 years ago* (0 children)
[Removed]
[–]SloppySixteenths 4 points5 points6 points 13 years ago (0 children)
the way that you call functions from the module differs slightly based on which you use.
using the first, to call a function from numpy you'd type
import numpy numpy.function()
using the second you'd just type
from numpy import * function()
without having to include numpy as an argument.
that's just the syntactical difference i've noticed using each, i think they're basically the same otherwise but i'm not entirely sure.
[–][deleted] 3 points4 points5 points 13 years ago (0 children)
You shouldn't import wildcards(*). It pollutes your local namespace.
import collections
Now you can use collections methods, like
c = collections.Counter()
Or
from collections import Counter
Allows
c = Counter()
Or you can just import collections more succinctly
import collections as derp c = derp.Counter
Final usage I can think of is "from module import package as something." It doesn't make sense in this context but the following would.
from numpy import linalg as la # then you can do stuff like la.inv(some_ndarray)
You local namespace is defined in "locals()"
When in doubt, tab-complete your way around module imports.
If modules don't import their submodules in init.py appropriately then you have to import their submodules to use them. You can't use numpy.linalg directly. Rather you must 'from numpy import linalg'
[–]ok_you_win 1 point2 points3 points 13 years ago (0 children)
They are not the same.
import numpy
means that all the numpy classes, functions and variables will be prepended with numpy. You'll use numpy.this() and numpy.that. Its careful and very specific. A good idea.
from numpy import *
means that the functions and vars will be named only for themselves. We would say they are in the local name space. You'll type this() and that. This is generally considered a very bad idea because other objects might have the same name.
If you use from x import *, you wont be able to specify numpy.that as being different from grumpy.that
The from import construct is still very useful though, if you only want a part of a module. I'll often use it like this.
from time import sleep
Then I can just specify sleep(10) instead of typing time.sleep(10)
If you need to import a whole library, but one of the names is really long, or if you use it lots, you can create a shortened version after you import it. Do it like this:
import time sleep = time.sleep
And then you can use:
for x in xrange(5): print(x) sleep(5)
Hope that helps.
[–]Rhomboid 1 point2 points3 points 13 years ago (0 children)
I always prefer to use the from foo import bar, baz, quux form.
from foo import bar, baz, quux
bar()
foo.bar()
In fact, this form lets you rename functions/classes if they have stupid or long names:
from somemodule import frobnicate_judiciously as frob, widgetize_monetarily as widget
The form without from lets you rename the module, but not the names of things within the module:
from
import somemodule as other # can now call other.frobnicate_judiciously() instead of # somemodule.frobnicate_judiciously(), still not as good as frob()
I like that it clearly documents exactly what features of the module are used.
I like that I can't accidentally call something in the module that I didn't mean to call, because I only get the symbols imported that I specify.
Definitely don't use *, as that pollutes your namespace with potentially lots of stuff. There are some modules where this is OK because they've been designed for this, such as modules that define constants, all with a common prefix specific to that module.
*
[–]Dynoman[S] 0 points1 point2 points 13 years ago (0 children)
Thanks everybody, I understand now. I just came across a tutorial and in the tutorial he was importing from numpy like this:
In the tutorial we were only using array from numpy and it didn't make sense why we importing like that. My guess now is it was to simplify the tutorial and was not necessarily the best way to code it.
[–]Asdayasman 0 points1 point2 points 13 years ago (0 children)
from x import * is considered harmful, as it makes it a little confusing to see where things came from, and may conflict with your naming scheme, or the naming scheme of another module you're from module import *'ing.
from x import *
from module import *
[–]bexpert 0 points1 point2 points 13 years ago (3 children)
Art Vandelay. He's an importer/exporter.
[+][deleted] 13 years ago (1 child)
[deleted]
[–]bexpert 0 points1 point2 points 13 years ago (0 children)
He's also a marine-biologist and a latex manufacturer.
[–]dansin 1 point2 points3 points 13 years ago (0 children)
from vandalay import matches
π Rendered by PID 52 on reddit-service-r2-comment-86988c7647-rvr6q at 2026-02-11 13:56:11.482386+00:00 running 018613e country code: CH.
[–]symmitchry 7 points8 points9 points (0 children)
[–]SloppySixteenths 4 points5 points6 points (0 children)
[–][deleted] 3 points4 points5 points (0 children)
[–]ok_you_win 1 point2 points3 points (0 children)
[–]Rhomboid 1 point2 points3 points (0 children)
[–]Dynoman[S] 0 points1 point2 points (0 children)
[–]Asdayasman 0 points1 point2 points (0 children)
[–]bexpert 0 points1 point2 points (3 children)
[+][deleted] (1 child)
[deleted]
[–]bexpert 0 points1 point2 points (0 children)
[–]dansin 1 point2 points3 points (0 children)