Drkcore

30 03 2011 Python Tweet

Pythonのsiteモジュールはサイト固有のパスをモジュール検索パスに追加するわけだが

addpackage関数を読んでいたら

for line in f:
    if line.startswith("#"):
        continue
    if line.startswith(("import ", "import\t")):
        exec line
        continue
    line = line.rstrip()
    dir, dircase = makepath(sitedir, line)
    if not dircase in known_paths and os.path.exists(dir):
        sys.path.append(dir)
        known_paths.add(dircase)

最初のifはコメント対応で次はpthでimportではじまる場合にはimportするってことだ。最後は行のパスがまだモジュール検索パスに含まれていなかったら追加するようになっている。

またmain関数は設定が出力されるようになっている。

$ python /opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site.py
sys.path = [
    '/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6',
    ...
    '/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PyObjC',
    '/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/wx-2.8-mac-unicode',
]
USER_BASE: '/Users/kzfm/.local' (exists)
USER_SITE: '/Users/kzfm/.local/lib/python2.6/site-packages' (doesn't exist)
ENABLE_USER_SITE: True

あれ、USER_SITEってことはローカルにライブラリインストールして使えるってことか?

easy_installのヘルプを見たら--userオプション入れればローカルにインストールすることが出来るって書いてあった。いつもvirtualenv使っているから知らなかった。

ProductName エキスパートPythonプログラミング
Tarek Ziade
アスキー・メディアワークス / 3780円 ( 2010-05-28 )


About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021