tags:

views:

75

answers:

3

Just wondering why

import sys
exit(0)

gives me this error:

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in ?
    exit(0)
TypeError: 'str' object is not callable

but

from sys import exit
exit(0)

works fine?

+8  A: 

Python imports only the chosen names into the namespace.

Your equivalent first solution should be

sys.exit(0)

since import sys imports only the sys keyword into the current namespace.

Lakshman Prasad
+6  A: 

See http://effbot.org/zone/import-confusion.htm for all the different ways to use import in Python.

import sys

This imports the sys module and binds it to the name "sys" in your namespace. "exit", and other members of the sys module are not brought into the namespace directly but can be accessed like so:

sys.exit(0)

from sys import exit

This imports specific members of the sys module into your namespace. Specifically this binds the name "exit" to the sys.exit function.

exit(0)

To see what's in your namespace, use the dir function.

>>> import sys
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'sys']
>>>
>>> from sys import exit
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'exit', 'sys']

You can even see what all is in the sys module itself:

>>> dir(sys)
['__displayhook__', '__doc__', '__egginsert', '__excepthook__', '__name__', '__package__', '__plen', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'getwindowsversion', 'hexversion', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'py3kwarning', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions', 'winver']
FogleBird
A: 

Ok from your Answers I can remeber that:

import sys from *

would import all members from sys

Richard
This is, of course, discouraged because it produces namespace pollution. The script `print len(locals()); from sys import *; print len(locals())` produces 4 and then 47 as the local variable count. This is undesirable for several reasons, the main being increased likelihood that your name shadows an imported name. Though for one-off scripts and testing it's not *horrible*, but as a general rule it should be avoided.
Wayne Werner
Yeah I agree with what you are saying Wayne. It could lead to unexpected results. This is why I named Lakshman Prasad answer as number one. Maybe I should not have included this answer in case it confuses others. Thanks for clarifying
Richard
1) The syntax is `from sys import *` (you have the keywords backwards), and 2) Never ever ever use this feature.
Mike Graham