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?

3
  • How are you getting the first error. For me in a script it just exits with no printout Commented Jun 8, 2010 at 12:27
  • 1
    Probably running a script and not interactive mode. exit() is a special interactive mode command. Commented Jun 8, 2010 at 12:28
  • Maybe it exits before you can see the printout. You try running it in IDLE? Commented Jun 8, 2010 at 12:54

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

Comments

6

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']

Comments

0

Ok from your Answers I can remeber that:

import sys from *

would import all members from sys

3 Comments

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.
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
1) The syntax is from sys import * (you have the keywords backwards), and 2) Never ever ever use this feature.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.