python學習第五天-模組
原創作品,允許轉載,轉載時請務必以超連結形式標明文章 、作者資訊和本宣告。否則將追究法律責任。
1 匯入系統已安裝的模組
1 匯入系統已安裝的模組
[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部落格 ” ,連結:http://blog.itpub.net/30129545/viewspace-1549468/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [Python模組學習] glob模組Python
- Python學習之模組Python
- Python學習——xml模組PythonXML
- Python time模組學習Python
- python模組學習:CookiePythonCookie
- Python模組學習:atexitPython
- Python模組學習:fileinputPython
- 天池python學習-task02打卡第五天Python
- 前端學習第五天前端
- Python學習——hashlib模組Python
- Python學習之常用模組Python
- Python學習——shelve模組Python
- python 各種模組學習Python
- python學習之argparse模組Python
- python optparse模組學習薦Python
- Python模組學習:urllibPython
- Python模組學習:datetimePython
- 【python】python 模組學習之--FabricPython
- 【python】python 模組學習之--pexpectPython
- JAVA SE 學習第五天Java
- java學習暑假第五天Java
- Python學習之 datetime模組Python
- Python學習之模組與包Python
- python基礎學習16—-模組Python
- 學習Python的urllib模組Python
- Python學習——configparser模組Python
- python模組學習:anydbm, shelvePython
- Python的shutil zipfile tarfile模組學習Python
- Python學習——logging模組Python
- Python模組學習:hashlib hash加密Python加密
- Python模組學習:copy 物件拷貝Python物件
- Python模組學習:subprocess 建立子程式Python
- Python學習之如何引用Python自定義模組?Python
- 第五天學習Java的筆記Java筆記
- Python學習【第十四篇】shutil模組Python
- Python模組學習:zipfile zip檔案操作Python
- Python模組學習: re 正規表示式Python
- Python模組學習:random 隨機數生成Pythonrandom隨機