The python sys module sys.path USES method examples

  • 2020-04-02 13:14:42
  • OfStack

The python sys module contains functions related to the python interpreter and its environment, which you can view with dir(sys) for methods and member properties


import sys
print dir(sys)

The 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']

There's a sys.path property in there. It's a list. When python imports a file or module, it looks for the module's path in sys.path. If not, the program will report an error.
So we usually write our own programs. It is best to add your module path to the current module scan path,eg: sys.path.append(' name of your module '), so that the program will not
An error was reported because the module could not be found.


Related articles: