import 导入模块学习
python 大家都知道,模块是这门语言的强大之处,那么经常导入模块去使用也是做为程序员或者是运维工程师必须要做的,那我们也深入学习下吧
导入系统已安装的模块
[root@localhost bin]# pythonPython 2.6.6 (r266:84292, Dec 7 2011, 20:48:22)[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import sys>>> import re>>> import os>>>
各位,看清楚了,这个没有问题吧,但是,这些模块是存入在哪里,或者是说路径在哪里呢?
[root@localhost bin]# pythonPython 2.6.6 (r266:84292, Dec 7 2011, 20:48:22)[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2Type "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.pytest='This is my module'print test[root@localhost python]# pythonPython 2.6.6 (r266:84292, Dec 7 2011, 20:48:22)[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import moduleThis is my module>>>
这样就可以使用,但接下来要注意的一件事情,如果我不在模块的目录下面执行那会不会成功?
[root@localhost python]# pwd/opt/python[root@localhost python]# cd ..[root@localhost opt]# pythonPython 2.6.6 (r266:84292, Dec 7 2011, 20:48:22)[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import moduleTraceback (most recent call last): File "", line 1, in 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]# pythonPython 2.6.6 (r266:84292, Dec 7 2011, 20:48:22)[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import sys>>> sys.path.append('/opt/python')>>> import moduleThis is my module>>>
这样添加了路径就可以添加了,那接下来现试下,如果我退出后,再重新导入模块会是怎么样的?
[root@localhost opt]# pwd/opt[root@localhost opt]# pythonPython 2.6.6 (r266:84292, Dec 7 2011, 20:48:22)[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import sys>>> sys.path.append('/opt/python')>>> import moduleThis is my module>>>[root@localhost opt]# pythonPython 2.6.6 (r266:84292, Dec 7 2011, 20:48:22)[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import moduleTraceback (most recent call last): File "", line 1, in ImportError: No module named module>>>
说明sys.path.append也只是一次性能把自己的模块路径给添加,退出后则没有了,这样用起来也不是很方便。用更好的办法
[root@localhost opt]# export PYTHONPATH=/opt/python/[root@localhost opt]# pythonPython 2.6.6 (r266:84292, Dec 7 2011, 20:48:22)[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import moduleThis is my module>>>
使用export添加,这样就永久啦,所以自己以后的开发,模块都放在一起,方便使用
在下也是菜鸟,希望各位有经验的大牛多多指点