import 导入模块学习python
python 你们都知道,模块是这门语言的强大之处,那么常常导入模块去使用也是作为程序员或者是运维工程师必需要作的,那咱们也深刻学习下吧
linux
导入系统已安装的模块程序员
[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 >>>
各位,看清楚了,这个没有问题吧,可是,这些模块是存入在哪里,或者是说路径在哪里呢?app
[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 >>>
这样就可使用,但接下来要注意的一件事情,若是我不在模块的目录下面执行那会不会成功?ssh
[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 >>>
发现问题了吧,提示说没有这个模块,那怎么样才能正常使用本身建立模块呢?由于总不能到模块目录去写程序吧?ide
>>> 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 >>>
这样添加了路径就能够添加了,那接下来现试下,若是我退出后,再从新导入模块会是怎么样的?orm
[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]# 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 >>>
说明sys.path.append也只是一次性能把本身的模块路径给添加,退出后则没有了,这样用起来也不是很方便。用更好的办法
[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添加,这样就永久啦,因此本身之后的开发,模块都放在一块儿,方便使用
在下也是菜鸟,但愿各位有经验的大牛多多指点