[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" formore information.
>>> import sys
>>> import re
>>> import os
>>>
2 创建模块的使用
[root@localhost test]# cat mymodule.py
def hello():
print "mymodule,hello!!"
moduleversion='1.0'
[root@localhost test]# python
Python 2.6.6 (r266:84292, Nov 22 2013, 12:16:22)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" formore information.
>>> import mymodule
>>> from mymodule import hello,moduleversion
>>> mymodule.hello()
mymodule,hello!!
>>> mymodule.moduleversion
'1.0'
3 不在模块的目录下面执行那会不会成功?
[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" formore information.
>>> import module
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named 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" formore information.
>>> import sys
>>> sys.path.append('/opt/python')
>>> import module
This is my 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" formore information.
>>> import module
This is my module
>>>
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30129545/viewspace-1549468/
,如需转载,请注明出处,否则将追究法律责任。