help on "from deen import *" vs. "import deen"

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Nov 16 23:06:04 EST 2016


On Thursday 17 November 2016 12:54, jfong at ms4.hinet.net wrote:

> Steve D'Aprano at 2016/11/16 8:33:23AM wrote:
>> `import foo` imports the module foo, that is all. (To be pedantic: it is
>> *nominally* a module. By design, it could be any object at all.)
>> 
>> `from foo import *` imports all the visible public attributes of foo.
>> 
>> They do completely different things, equivalent to something similar to:
>> 
>> five = int('5')
>> 
>> 
>> versus:
>> 
>> 
>> _tmp = int('5')
>> for name in dir(_tmp):
>>     if not name.startswith('_'):
>>         locals()[name] = getattr(_tmp, name)
>> del _tmp
> 
> This is far beyond my comprehension:-(

That's okay, I was talking mostly to Eric.

You understand how this works?

five = int('5')


creates a new variable "five", and gives it the value 5.

py> five
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'five' is not defined
py> five = int("5")  # creates the new variable
py> five
5

That is similar to how import works: "import os" reads the "os.py" file, 
compiles it to a module object, creates the variable "os", and sets collections 
to that module object:

py> os
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
py> import os
py> os
<module 'os' from '/usr/lib/python2.5/os.pyc'>



But that's not what "from os import *" does. It is more complicated. It looks 
inside the "os" module, extracts all the public functions, classes and global 
variables, and creates new variables in your own module for them.

The os module has close to 200 or more public functions and globals, so I'll 
pick a simpler example. Create this two line file:


# library.py
apple = 1
pear = 2



Now let's check that "import" works the way we expect. Inside the Python 
interactive interpreter, run this code:


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


Do you get the same output?

Look inside the module:

py> print(library.apple)
1



Check that there's no "apple" variable, then import it:

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


Finally try this:

py> apple = 999
py> from library import *
py> apple
1
py> pear
2




Remember that the "apple" variable is a different variable from the 
"library.apple" variable. Think of:

# in the USA
president = "Barack Obama"

# in Russia
president = "Vladimir Putin"


Two variables, with the same name, but their values are independent:

usa.president = "Donald Trump"  # will happen soon
print(russia.president)  # still prints "Vladimir Putin"


The same happens when you are working inside the current active module:

president = "Xi Jinping"

This doesn't affect either usa.president or russia.president -- not even if we 
first take the value from the other module:


from russia import president
assert president == 'Vladimir Putin'  # this is true, for now
president = 'Francois Hollande'  # new president, here


Changing the president in the current module doesn't change the 
russia.president.



Now:

    from russia import president

is almost the same as:


    import russia as _tmp
    president = _tmp.president
    del _tmp



But this is more complicated:

    from russia import *


The star import has to work out all the visible public names, and create new 
variables for all of them. You don't need to understand how it does that, the 
details are not important. What is important is that the variables it creates 
are different from the variables living inside the russia module.




-- 
Steven
299792.458 km/s — not just a good idea, it’s the law!




More information about the Python-list mailing list