367

I'm wondering if there's any difference between the code fragment

from urllib import request

and the fragment

import urllib.request

or if they are interchangeable. If they are interchangeable, which is the "standard"/"preferred" syntax (if there is one)?

wchargin
  • 15,589
  • 12
  • 71
  • 110
  • 1
    I'm not an expert on import so I wont leave an answer, but there is a difference about how things are going into `sys.modules`: take a look at this [answer](http://stackoverflow.com/a/8992850/1132524) (at the end). *(Maybe there's someone who can explain it better than me)* – Rik Poggi Feb 25 '12 at 00:25

6 Answers6

318

It depends on how you want to access the import when you refer to it.

from urllib import request
# access request directly.
mine = request()

import urllib.request
# used as urllib.request
mine = urllib.request()

You can also alias things yourself when you import for simplicity or to avoid masking built ins:

from os import open as open_
# lets you use os.open without destroying the 
# built in open() which returns file handles.
g.d.d.c
  • 46,865
  • 9
  • 101
  • 111
  • 4
    I just tried import urllib.request and it doesn't work at all (python 2.6.5 Ubuntu). – tkone Feb 24 '12 at 23:33
  • 9
    You should use a trailing underscore (rather than leading) to prevent clashing with built-ins. – deadly Aug 28 '12 at 10:36
  • @deadly - bad habit - it prevents an IDE warning in Eclipse to use a leading underscore at times. Thanks. – g.d.d.c Jan 29 '14 at 06:40
  • 26
    @g.d.d.c: The “single trailing underscore” convention is explicitly specified by PEP 8 (twice). If Eclipse produces annoying warnings about correct code, then we have a bad IDE, not a bad habit. – wchargin Feb 16 '18 at 09:11
  • 1
    @tkone In Python 3, urllib2 was moved to urllib.request. – Arcanum Jul 08 '18 at 02:45
245

Many people have already explained about import vs from, so I want to try to explain a bit more under the hood, where the actual difference lies.

First of all, let me explain exactly what the basic import statements do.

import X

Imports the module X, and creates a reference to that module in the current namespace. Then you need to define completed module path to access a particular attribute or method from inside the module (e.g.: X.name or X.attribute)

from X import *

Imports the module X, and creates references to all public objects defined by that module in the current namespace (that is, everything that doesn’t have a name starting with _) or whatever name you mentioned.

Or, in other words, after you've run this statement, you can simply use a plain (unqualified) name to refer to things defined in module X. But X itself is not defined, so X.name doesn't work. And if name was already defined, it is replaced by the new version. And if name in X is changed to point to some other object, your module won’t notice.

This makes all names from the module available in the local namespace.

Now let's see what happens when we do import X.Y:

>>> import sys
>>> import os.path

Check sys.modules with name os and os.path:

>>> sys.modules['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> sys.modules['os.path']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>

Check globals() and locals() namespace dict with name os and os.path:

 >>> globals()['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> locals()['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> globals()['os.path']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'os.path'
>>>    

From the above example, we found that only os is added to the local and global namespaces. So, we should be able to use os:

 >>> os
 <module 'os' from     
  '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
 >>> os.path
 <module 'posixpath' from      
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
 >>>

…but not path:

>>> path
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'path' is not defined 
>>>

Once you delete the os from locals() namespace, you won't be able to access either os or os.path, even though they do exist in sys.modules:

>>> del locals()['os']
>>> os
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>> os.path
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>>

Now let's look at from.

from

>>> import sys
>>> from os import path

Check sys.modules with name os and os.path:

>>> sys.modules['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> sys.modules['os.path']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>

So sys.modules looks the same as it did when we imported using import name.

Okay. Let's check what it the locals() and globals() namespace dicts look like:

>>> globals()['path']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> locals()['path']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> globals()['os']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'os'
>>>

You can access by using path, but not by os.path:

>>> path
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> os.path
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>>
 

Let's delete 'path' from locals():

>>> del locals()['path']
>>> path
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'path' is not defined
>>>

One final example using aliasing:

>>> from os import path as HELL_BOY
>>> locals()['HELL_BOY']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> globals()['HELL_BOY']
<module 'posixpath' from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>>

And no path defined:

>>> globals()['path']
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
KeyError: 'path'
>>>

One pitfall about using from

When you import the same name from two different modules:

>>> import sys
>>> from os import stat
>>> locals()['stat']
<built-in function stat>
>>>
>>> stat
<built-in function stat>

Import stat from shutil again:

>>>
>>> from shutil import stat
>>> locals()['stat']
<module 'stat' from 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/stat.pyc'>
>>> stat
<module 'stat' from 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/stat.pyc'>
>>>

THE LAST IMPORT WILL WIN

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
James Sapam
  • 16,036
  • 12
  • 50
  • 73
  • 8
    Indeed it is, but for me is a little confusing with all these namespaces and so on. So i prefer just to use import x and then x.what_is_necessary. In my humble opinion i think is more readable and easier to avoid confusion, and by this way u sure avoid name clashes ambiguity and all that sort of stuff...right? – udarH3 Sep 26 '15 at 13:13
  • 20
    I like this answer as it teaches how to catch fish instead of providing fish. – Dana Apr 17 '19 at 22:09
  • 4
    What does `from . import ' mean? Not sure what the dot is referring to. – Azurespot Dec 12 '19 at 04:02
  • 6
    The dot denotes that it is a relative import, one means the current directory, two means the parent directory, and so on up the tree. – dragon788 May 12 '20 at 14:35
29

There is a difference. In some cases, one of those will work and the other won't. Here is an example: say we have the following structure:

foo.py
mylib\
    a.py
    b.py

Now, I want to import b.py into a.py. And I want to import a.py to foo. How do I do this? Two statements, in a I write:

import b

In foo.py I write:

import mylib.a

Well, this will generate an ImportError when trying to run foo.py. The interpreter will complain about the import statement in a.py (import b) saying there is no module b. So how can one fix this? In such a situation, changing the import statement in a to import mylib.b will not work since a and b are both in mylib. The solution here (or at least one solution) is to use absolute import:

from mylib import b

Source: Python: importing a module that imports a module

Girrafish
  • 2,320
  • 1
  • 19
  • 32
Anas Elghafari
  • 1,062
  • 1
  • 10
  • 20
4

You are using Python3 were urllib in the package. Both forms are acceptable and no one form of import is preferred over the other. Sometimes when there are multiple package directories involved you may to use the former from x.y.z.a import s

In this particular case with urllib package, the second way import urllib.request and use of urllib.request is how standard library uniformly uses it.

Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131
2

In python 2.x at least you cannot do import urllib2.urlopen

You have to do from urllib2 import urlopen

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib2.urlopen
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named urlopen
>>> import urllib.request
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named request
>>>
tkone
  • 22,092
  • 5
  • 54
  • 78
  • 2
    Well, that's because `urlopen` is a function, right? It'd be like trying to `import os.chdir`—like the error message rightly states, "No module named `chdir`"…because it's not a module! – wchargin Oct 06 '14 at 14:27
1

My main complaint with import urllib.request is that you can still reference urllib.parse even though it isn't imported.

>>> import urllib3.request
>>> urllib3.logging
<module 'logging' from '/usr/lib/python2.7/logging/__init__.pyc'>

Also request for me is under urllib3. Python 2.7.4 ubuntu

Anonymous
  • 11
  • 1