import 导入模块学习
2014-03-12 11:11
465 查看
import 导入模块学习 python 大家都知道,模块是这门语言的强大之处,那么经常导入模块去使用也是做为程序员或者是运维工程师必须要做的,那我们也深入学习下吧
导入系统已安装的模块
这样添加了路径就可以添加了,那接下来现试下,如果我退出后,再重新导入模块会是怎么样的?
本文出自 “游造技术博客” 博客,请务必保留此出处http://youzao.blog.51cto.com/3946111/1373839
导入系统已安装的模块
[root@localhost bin]# python Python 2.6.6 (r266:84292, Dec 7 2011, 20:48:22) [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> import re >>> import os >>>各位,看清楚了,这个没有问题吧,但是,这些模块是存入在哪里,或者是说路径在哪里呢?
[root@localhost bin]# python Python 2.6.6 (r266:84292, Dec 7 2011, 20:48:22) [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.path ['', '/usr/lib64/python26.zip', '/usr/lib64/python2.6', '/usr/lib64/python2.6/plat-linux2', '/usr/lib64/python2.6/lib-tk', '/usr/lib64/python2.6/lib-old', '/usr/lib64/python2.6/lib-dynload', '/usr/lib64/python2.6/site-packages', '/usr/lib64/python2.6/site-packages/gtk-2.0', '/usr/lib/python2.6/site-packages'] >>>以后大家使用的时候也可以这样查询你安装了啥模块,接下来就是,安装了模块是可以直接使用 的,那如果是我自己写的模块,我也想直接使用,那怎么样操作
[root@localhost python]# cat module.py test='This is my module' print test [root@localhost python]# python Python 2.6.6 (r266:84292, Dec 7 2011, 20:48:22) [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import module This is my module >>>这样就可以使用,但接下来要注意的一件事情,如果我不在模块的目录下面执行那会不会成功?
[root@localhost python]# pwd /opt/python [root@localhost python]# cd .. [root@localhost opt]# python Python 2.6.6 (r266:84292, Dec 7 2011, 20:48:22) [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import module Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named module >>>发现问题了吧,提示说没有这个模块,那怎么样才能正常使用自己创建模块呢?因为总不能到模块目录去写程序吧?
>>> dir(sys.path) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']这个查找sys.path的方法,当我们不知道这个模块怎么样使用的话,这是最基础的查找和使用看这里是不是有一个append?,这个就是可以添加模块的路径的,那么我们来试一
[root@localhost opt]# pwd /opt [root@localhost opt]# python Python 2.6.6 (r266:84292, Dec 7 2011, 20:48:22) [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.path.append('/opt/python') >>> import module This is my module >>>
这样添加了路径就可以添加了,那接下来现试下,如果我退出后,再重新导入模块会是怎么样的?
[root@localhost opt]# pwd /opt [root@localhost opt]# python Python 2.6.6 (r266:84292, Dec 7 2011, 20:48:22) [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.path.append('/opt/python') >>> import module This is my module >>>说明sys.path.append也只是一次性能把自己的模块路径给添加,退出后则没有了,这样用起来也不是很方便。用更好的办法
[root@localhost opt]# python
Python 2.6.6 (r266:84292, Dec 7 2011, 20:48:22)
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import module
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named module
>>>
[root@localhost opt]# export PYTHONPATH=/opt/python/ [root@localhost opt]# python Python 2.6.6 (r266:84292, Dec 7 2011, 20:48:22) [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import module This is my module >>>使用export添加,这样就永久啦,所以自己以后的开发,模块都放在一起,方便使用在下也是菜鸟,希望各位有经验的大牛多多指点
本文出自 “游造技术博客” 博客,请务必保留此出处http://youzao.blog.51cto.com/3946111/1373839
相关文章推荐
- 静态链接库LIB和动态链接库DLL的区别 创建和示例
- mysql-connector-odbc-5.2.6-winx64 安装 ERROR
- Java基础知识总结
- 连载:面向对象葵花宝典:思想、技巧与实践(17) - 需求分析518方法
- fm1188简介
- 精确到时分秒的日历控件
- Zookeeper集群安装详解
- [转]理解Linux的处理器负载均值
- App Store 评分方法
- 总结
- 25岁研究生拿高薪互联网的offer,意气风发的走出学校
- 总结java面试题
- C++中的set(STL的应用部分)
- 惊群现象
- ASP.NET MVC + Jquery 实现Ajax下拉框数据三级联动
- 获取网络图片
- java 树结构
- C# DateTime格式化
- 转:人生成功必读的经典语录200条
- Q4.3