我的 sys.path 如下所示:
/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
我注意到有一些目录都是/usr/lib64/python2.7的子目录,怎么会这样呢?当我删除所有子目录并使 sys.path 如下所示:
/homel/ychao/python/python_lib
/usr/lib64/python2.7
/usr/lib/python2.7/site-packages
一些之前可以导入的包修改后会失败。所以这意味着“导入”不知道递归地找到合适的包?
谢谢!
最佳答案
so this means the "import" does not know to find the appropriate packages recursively?
没错。 docs for sys.path
不要在任何地方提到递归。
事实上,如果模块解析是递归的,你会得到令人困惑的行为。假设 /usr/lib/python2.7/site-packages/ham
在搜索路径上,并且您有文件
/usr/lib/python2.7/site-packages/ham
/usr/lib/python2.7/site-packages/ham/ham/util.py
然后你会导入util
。如果直接在搜索路径上没有找到 util.py
或 util/__init__.py
,则递归处理将导致 ham.util
使用错误的名称导入,破坏了 Python 的包/模块命名空间。
关于python - 为什么python系统路径中一个目录是另一个目录的子目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14030935/