Pythonのシステムパス

Pythonが見ているシステムパスの表示の仕方、絶対に忘れるのでメモメモ。ちなみに私が使っているのはWindows版のバージョンは2.5を利用してます。

>>> import sys
>>> sys.path
['', 'C:\\WINDOWS\\system32\\python25.zip', 'C:\\Python25\\DLLs', 'C:\\Python25\
\lib', 'C:\\Python25\\lib\\plat-win', 'C:\\Python25\\lib\\lib-tk', 'C:\\Python25
', 'C:\\Python25\\lib\\site-packages']

で新たにパスを通したい場合は、

>>> sys.path.append('/mymodule/path')

実際試した事ないんでなんとも言えないが、Windowsの場合は「c:\mymodule\path」で良いんだよね?もしかしたら「c:\\mymodule\\path」とすべきなのかも〜今のところ新しくパスを通す必要もないので試さない。まあいいや!

>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__stderr__', '__st
din__', '__stdout__', '_current_frames', '_getframe', 'api_version', 'argv', 'bu
iltin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'dis
playhook', 'dllhandle', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec
_prefix', 'executable', 'exit', 'getcheckinterval', 'getdefaultencoding', 'getfi
lesystemencoding', 'getrecursionlimit', 'getrefcount', 'getwindowsversion', 'hex
version', 'last_traceback', 'last_type', 'last_value', 'maxint', 'maxunicode', '
meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform',
'prefix', 'ps1', 'ps2', 'setcheckinterval', 'setprofile', 'setrecursionlimit', '
settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info',
 'warnoptions', 'winver']

>>> dir(sys.path)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delsli
ce__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__gets
lice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '
__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__r
educe_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__
', '__setslice__', '__str__', 'append', 'count', 'extend', 'index', 'insert', 'p
op', 'remove', 'reverse', 'sort']

最近新しいモジュールを見つけるとやたらと「dir()」をやりたくなる。面白いよね。

>>> sys.version
'2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)]'
>>> sys.subversion
('CPython', 'tags/r254', '67916')
>>>

いやほんと、Pythonは面白いよ。

やっぱりパスを新しく通してみた。

>>> sys.path.append('c:\practice\mymodule')
>>> sys.path
['', 'C:\\WINDOWS\\system32\\python25.zip', 'C:\\Python25\\DLLs', 'C:\\Python25\
\lib', 'C:\\Python25\\lib\\plat-win', 'C:\\Python25\\lib\\lib-tk', 'C:\\Python25
', 'C:\\Python25\\lib\\site-packages', 'c:\\practice\\mymodule']

存在しないパスでもエラーなく通るのね。まあ、そうだよな〜ちなみに「sys.path.remove('c:\practice\mymodule')」で指定したパスを消せます。