为python添加搜索模块路径
来源:互联网 发布:韩国网络剧奇迹在线 编辑:程序博客网 时间:2024/06/07 18:40
原文出处:https://www.douban.com/note/334738164/
方法一:函数添加
1 import sys
2 查看sys.path
3 添加sys.path.append("c:\\")
方法二:修改环境变量
w用户可以修改系统环境变量PYTHONPATH
方法三:增加.pth文件,推荐!
在site-packages添加一个路径文件,如mypkpath.pth,必须以.pth为后缀,写上你要加入的模块文件所在的目录名称就是了。
1 windows
c:\python27\site-packages
# 我们的学员把pth文件直接放在c:\python27
# (或其他python安装目录)目录下测试也是有效的
# 2015.9.23
2 linux(ubuntu)
/usr/local/lib/python2.7/dist-packages (note by shanql, 我是放在这个目录下有效的,所添加的目录一定要存在,不存在则会不成功)
2 linux(redhat)
/usr/lib/python2.7/site-packages
以下是另一篇文章:http://blog.sina.com.cn/s/blog_59ba44680100z0mt.html
学习python不久,碰到个问题,怎么样把自己写的模块加入到python默认就有的搜索路径中呢?不要每次非得import sys; sys.path.append(‘/home/fightlikekobe/python’)才可。
上网找了很多久未果,都来我自己糊里糊涂找到了解决方法,那就是在dist-packages(即/usr/local/lib/python2.7/dist-packages)添加一个路径文件,如mypkpath.pth,必须以.pth为后缀,写上你要加入的文件名称就是了,如我的:
/home/fightlikekobe/python
保存退出,OK!结果自己验证,重启terminal,再次打开python,
>>> import sys
>>> sys.path
['/home/fightlikekobe', '/usr/bin', '/usr/local/lib/python2.7/dist-packages/pymysql_sa-1.0-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/PyMySQL-0.5-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/hiredis-0.1.0-py2.7-linux-i686.egg', '/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.7.3-py2.7-linux-i686.egg', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/local/lib/python2.7/dist-packages', '/home/fightlikekobe/python', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client', '/usr/lib/python2.7/dist-packages/ubuntuone-client', '/usr/lib/python2.7/dist-packages/ubuntuone-control-panel', '/usr/lib/python2.7/dist-packages/ubuntuone-couch', '/usr/lib/python2.7/dist-packages/ubuntuone-installer', '/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol', '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode']
- 为Python添加默认模块搜索路径
- 为Python添加默认模块搜索路径
- 为Python添加默认模块搜索路径
- 为Python添加默认模块搜索路径
- 为Python添加默认模块搜索路径
- 为Python添加默认模块搜索路径
- 为Python添加默认模块搜索路径
- 为Python添加默认模块搜索路径
- 为python添加搜索模块路径
- 为Python添加默认模块搜索路径[转]
- linux 添加python 模块搜索路径(以thrift为例)
- (转)为Python添加默认模块搜索路径
- python添加模块搜索路径
- 如何添加Python的模块搜索路径
- Python添加默认模块搜索包路径
- Python添加默认模块搜索路径
- 【Python】一劳永逸的为Python添加默认模块搜索路径即工作目录
- Python 模块搜索路径
- 知识图谱基础之RDF,RDFS与OWL
- Mac Jenkins. 安装
- java中初始化顺序
- RocketMQ实战(三):分布式事务
- 给初学者的RxJava2.0教程(二)
- 为python添加搜索模块路径
- 白话C++系列(关键字static)
- PHP7新特性(1)
- #define offsetof(s,m) (size_t)&(((s *)0)->m)
- 分页页码
- Python
- codeforces 266B Queue at the School
- 二叉树遍历、栈、二叉查找树
- JSP_处理访问图片路径问题