Custom Search

Friday, April 2, 2010

How are modules found by ’import’?

How are modules found by ’import’ in python?

• The ’import’ statement searches the directories named in sys.path
• The first file found ’xxx.py’ (where xxx is the module name) is used
• There are ways to change your sys.path according to your needs

import sys
for dirname in sys.path:
print dirname

output:
C:\Python23
C:\Python23\DLLs
C:\Python23\lib
C:\Python23\lib\platwin
C:\Python23\lib\libtk
C:\Python23\lib\sitepackages

No comments:

Post a Comment