python sys模塊包含了與python解釋器和它的環境有關的函數,這個你可以通過dir(sys)來查看他里面的方法和成員屬性
代碼如下:
import sys
print dir(sys)
result:
代碼如下:
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', '_mercurial', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'getwindowsversion', 'hexversion', 'long_info', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'py3kwarning', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions', 'winver']
代碼如下:
import sys
print sys.path
result:
['C://Documents and Settings//username//My Documents//Aptana Studio 3 Workspace//Python_Test_Project//src', 'C://Documents and Settings//username//My Documents//Aptana Studio 3 Workspace//Python_Test_Project//src', 'C://Python27', 'C://Python27//DLLs', 'C://Python27//lib', 'C://Python27//lib//lib-tk', 'C://Python27//lib//plat-win', 'C://Python27//lib//site-packages', 'C://Python27//lib//site-packages//wx-2.8-msw-unicode', 'C://WINDOWS//system32//python27.zip']
里面有個 sys.path屬性。他是一個list.默然情況下python導入文件或者模塊的話,他會先在sys.path里找模塊的路徑。如果沒有的話,程序就會報錯。
所以我們一般自己寫程序的話。最好把自己的模塊路徑給加到當前模塊掃描的路徑里,eg: sys.path.append('你的模塊的名稱'),這樣程序就不會
因為找不到模塊而報錯。。
新聞熱點
疑難解答