PythonのPath

提供: Akionux-wiki
Share/Save/Bookmark
移動先: 案内検索

概要

PythonのPathの確認および設定方法。

テスト環境

  • Python 2.7.5
  • Slackware Linux 14.1

Pathの確認

pythonのコマンドラインでimport sysして、sys.pathを表示する。

% python      (git)-[master]
Python 2.7.5 (default, May 29 2013, 02:28:51) 
[GCC 4.8.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/PIL', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib64/python2.7/site-packages/wx-2.8-gtk2-unicode']

Pathの設定

環境変数から

~/.bashrcへ以下の行を追加[1]

export PYTHONPATH="${PYTHONPATH}:/my/other/path1:/my/other/path2"

Pythonから

追加:

>>> import sys
>>> sys.path.append('/my/other/path')
>>> sys.path.append(['/my/other/path1','/my/other/path2'])

削除:

>>> import sys
>>> sys.path.remove('/my/other/path')
>>> sys.path.remove(['/my/other/path1','/my/other/path2'])

References

  1. python - Permanently add a directory to PYTHONPATH - Stack Overflow http://stackoverflow.com/questions/3402168/permanently-add-a-directory-to-pythonpath