In python, if you need a module from a different package you have to import it. Coming from a Java background, that makes sense.
import foo.bar
What doesn’t make sense though, is why do I need to use the full name whenever I want to use bar? If I wanted to use the full name, why do I need to import? Doesn’t using the full name immediately describe which module I’m addressing?
It just seems a little redundant to have from foo import bar when that’s what import foo.bar should be doing. Also a little vague why I had to import when I was going to use the full name.
The thing is, even though Python’s
importstatement is designed to look similar to Java’s, they do completely different things under the hood. As you know, in Java animportstatement is really little more than a hint to the compiler. It basically sets up an alias for a fully qualified class name. For example, when you writeit tells the compiler that throughout that file, when you write
Set, you meanjava.util.Set. And if you writes.add(o)wheresis an object of typeSet, the compiler (or rather, linker) goes out and finds theaddmethod inSet.classand puts in a reference to it.But in Python,
(that is a made-up module, by the way) does something completely different. See, in Python, packages and modules are not just names, they’re actual objects, and when you write
util.setin your code, that instructs Python to access an object namedutiland look for an attribute on it namedset. The job of Python’simportstatement is to create that object and attribute. The way it works is that the interpreter looks for a file namedutil/__init__.py, uses the code in it to define properties of an object, and binds that object to the nameutil. Similarly, the code inutil/set.pyis used to initialize an object which is bound toutil.set. There’s a function called__import__which takes care of all of this, and in fact the statementimport util.setis basically equivalent toThe point is, when you import a Python module, what you get is an object corresponding to the top-level package,
util. In order to get access toutil.setyou need to go through that, and that’s why it seems like you need to use fully qualified names in Python.There are ways to get around this, of course. Since all these things are objects, one simple approach is to just bind
util.setto a simpler name, i.e. after theimportstatement, you can haveand from that point on you can just use
setwhere you otherwise would have writtenutil.set. (Of course this obscures the built-insetclass, so I don’t recommend actually using the nameset.) Or, as mentioned in at least one other answer, you could writeor
This still imports the package
utilwith the modulesetin it, but instead of creating a variableutilin the current scope, it creates a variablesetthat refers toutil.set. Behind the scenes, this works kind of likein the former case, or
in the latter (although both ways do essentially the same thing). This form is semantically more like what Java’s
importstatement does: it defines an alias (set) to something that would ordinarily only be accessible by a fully qualified name (util.set).