Question

my sys.path is like below:

/homel/ychao/python/python_lib
/usr/lib64/python27.zip
/usr/lib64/python2.7
/usr/lib64/python2.7/plat-linux2
/usr/lib64/python2.7/lib-tk
/usr/lib64/python2.7/lib-old
/usr/lib64/python2.7/lib-dynload
/usr/lib64/python2.7/site-packages
/usr/lib64/python2.7/site-packages/PIL
/usr/lib64/python2.7/site-packages/gst-0.10
/usr/lib64/python2.7/site-packages/gtk-2.0
/usr/lib64/python2.7/site-packages/wx-2.8-gtk2-unicode
/usr/lib/python2.7/site-packages
/usr/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info
/usr/lib/python2.7/site-packages/IPython/extensions

I noticed there some some directories which are all subdirectories /usr/lib64/python2.7, how could it be like this? when I remove all the subdirectories and make the sys.path like this:

/homel/ychao/python/python_lib
/usr/lib64/python2.7
/usr/lib/python2.7/site-packages

some packages that I can import before will fail after the modification. so this means the "import" does not know to find the appropriate packages recursively?

thanks!

Was it helpful?

Solution

so this means the "import" does not know to find the appropriate packages recursively?

That's right. The docs for sys.path don't mention recursion anywhere.

In fact, if module resolution were recursive, you'd get confusing behavior. Suppose /usr/lib/python2.7/site-packages/ham were on the search path, and you'd have files

/usr/lib/python2.7/site-packages/ham
/usr/lib/python2.7/site-packages/ham/ham/util.py

and you'd do import util. If no util.py or util/__init__.py were found on the search path directly, recursive processing would then cause ham.util to be imported with the wrong name, defeating Python's package/module namespaces.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top