python 教程 第六章、 模組
第六章、 模組
sys模組
位元組編譯的.pyc檔案,優化編譯後生成pyo檔案
import sys
print 'The command line arguments are:'
for i in sys.argv:
print i
print '\n\nThe PYTHONPATH is', sys.path, '\n'
只想在程式本身被使用的時候執行主塊,而在它被別的模組輸入的時候不執行主塊
from sys import *
print 'The command line arguments are:'
for i in argv:
print i
print '\n\nThe PYTHONPATH is', path, '\n'
#!/usr/bin/python
# Filename: mymodule.py
def sayhi():
print 'Hi, this is mymodule speaking.'
version = '0.1'
# End of mymodule.py
#!/usr/bin/python
# Filename: mymodule_demo.py
import mymodule
mymodule.sayhi()
print 'Version', mymodule.version
>>> import changer #匯入模組changer.py檔案
>>> changer.printer() # No effect: uses loaded module
First version
##修改changer.py的程式碼##
>>> from imp import reload
>>> reload(changer) # Forces new code to load/run
<module 'changer' from 'changer.py'>
>>> changer.printer() # Runs the new version now
reloaded: After editing
列出模組定義的識別符號。識別符號有函式、類和變數。
>>> import sys
>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright'…]
>>>
如果不提供引數,它返回當前模組中定義的名稱列表。
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'sys']
>>> a = 1
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'a', 'sys']
>>> del a
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'sys']
>>>
列出資料型別的可使用的函式,help函式的簡體版
>>> dir(dict)
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues']
import dir1.dir2.mod #from dir1.dir2.mod import x
dir1在PYTHONPATH路徑中
dir1目錄/dir2目錄/mod.py檔案
dir1目錄和dir2目錄下都必須含有__init__.py
sys.path.append('c:\\lp4e\\examples')
相關文章
- Python教程-6模組Python
- 簡明Python 教程 --模組Python
- python教程 - 檢視python的可用模組Python
- Python中yaml模組的使用教程PythonYAML
- Python模組、第三方模組安裝、模組匯入教程Python
- Python爬蟲教程-09-error 模組Python爬蟲Error
- Python 快速教程(進階篇03):模組Python
- Python中常用模組有哪些?Python基礎教程Python
- Python開發常用的庫及模組!Python學習教程Python
- 《Python 簡明教程》讀書筆記系列三 —— 模組Python筆記
- python 模組:itsdangerous 模組Python
- Python模組:time模組Python
- urlparse模組(python模組)Python
- python模組-re模組Python
- python模組 - functools模組Python
- Python培訓教程分享:Python模組如何匯入__all__屬性?Python
- python模組之collections模組Python
- Python 模組Python
- python模組Python
- Python 內建模組:os模組Python
- Python模組之urllib模組Python
- [Python模組學習] glob模組Python
- Python中模組是什麼?Python有哪些模組?Python
- Python的defaultdict模組和namedtuple模組Python
- Python functools 模組Python
- Python Execl模組Python
- Python webargs 模組PythonWeb
- python collections模組Python
- Python mongoHelper模組PythonGo
- Python pymsql模組PythonSQL
- Python模組(module)Python
- Python-模組Python
- python 模組fnmatchPython
- Python:requests模組Python
- python random模組Pythonrandom
- Python Re模組Python
- python:time模組Python
- python:模組0Python