简体   繁体   English

python如何导入具有多个副本的软件包

[英]how python import packages with multiple copies

I was believing that when importing a package, it would search from sys.path and use the first hit for import. 我相信导入包时,它将从sys.path搜索并使用第一个匹配项进行导入。 However, it seems not true: 但是,这似乎不正确:

import mpl_toolkits
print(mpl_toolkits.__path__)

And it outputs: 它输出:

['/Library/Python/2.7/site-packages/matplotlib-1.5.0-py2.7-macosx-10.11-intel.egg/mpl_toolkits', '/usr/local/lib/python2.7/site-packages/mpl_toolkits']

Can someone please explain to me how exactly python looks for packages if it is installed multiple times in the machine (in different location searchable by sys.path )? 如果有人在机器中多次安装python(在sys.path可以搜索的其他位置),有人可以向我解释python如何精确查找软件包吗? Or a pointer to relevant reference would be good. 或者指向相关参考文献的指针会很好。

when you import a module, python uses PYTHON PATH (system variable that contains a list of folders) and loops to search for importable module. 导入模块时,python使用PYTHON PATH(包含文件夹列表的系统变量)并循环搜索可导入的模块。
Python will test if it is a package (folder containing init .py) or a module (*.py). Python将测试它是一个包(包含init .py的文件夹)还是一个模块(* .py)。 it will stop on first module found if no module is found python raises an import error 如果找不到模块,它将在找到的第一个模块上停止python引发import error

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM