无名商城论坛

搜索
查看: 356|回复: 0

[其他技术] 【HC】python中的库及module的 路径

[复制链接]

1万

主题

1万

帖子

3万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
32464
发表于 2022-5-8 18:37:05 | 显示全部楼层 |阅读模式
本期带来了
python中的库及module的 路径
在python执行时,python调用的库或者模块文件,应该从哪里来加载呢?

包括了python系统的库目录、设置的环境变量PYTHONPATH;还可以在python代码中设置sys.path,在其中添加自己的模块路径。

查看一下python中的sys.path
>>> import sys
>>> sys.path
['', '/Users/david/Desktop/python_c', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Python/2.7/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC']
>>> quit()
如果自己有了新的module和package,可以放置到一个自己的固定目录下,然后设置PYTHONPATH环境变量,这样比较好。也可以在代码中如下设置:

import sys
sys.path.insert(0, "./path/to/your/modules/")
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表