Help us understand the problem. What is going on with this article?

pythonパッケージを一般ユーザーでローカル環境にインストールする方法

More than 1 year has passed since last update.

管理者権限(root)はないけど、ローカル環境にpythonのパッケージをインストールする。

以下の素環境で確認した。

$ docker run  --rm -i -t docker.io/centos:7.3.1611 /bin/bash
//pythonは入っているが、pipコマンドはない。
$ python -V
Python 2.7.5
$ pip
-bash: pip: command not found

// まずはpipコマンドを準備する
$ curl -O https://bootstrap.pypa.io/get-pip.py
$ python get-pip.py --user

// 上記コマンドを実行すると、.localにインストール+パスが追加される
$ ll ~/.local/bin/pip
-rwxrwxr-x 1 ymko ymko 201 Jul 15 03:58 /home/ymko/.local/bin/pip
$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ymko/.local/bin:/home/ymko/bin

// ログオフすると消えるので追加しておく
$ echo "export PATH=$HOME/.local/bin:\$PATH" >> ~/.bashrc

// そのままインストールしようとするとシステム領域で書き込めないエラー
$ pip install bottle
IOError: [Errno 13] Permission denied: '/usr/lib/python2.7/site-packages/bottle.pyc'

// --userで自分のローカルにインストールする
$ pip install bottle --user
Collecting bottle
Installing collected packages: bottle
Successfully installed bottle-0.12.13

// モジュール検索パスにも.localのものが含まれている
$ python
Python 2.7.5 (default, Nov  6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(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', '/home/ymko/.local/lib/python2.7/site-packages', '/usr/lib64/python2.7/site-packages', '/usr/lib/python2.7/site-packages']

毎回面倒なのでスクリプトにした

pip_user_install.sh
#!/bin/bash

curl -O https://bootstrap.pypa.io/get-pip.py
python get-pip.py --user
grep .local/bin ~/.bashrc
if [ $? != 0 ]; then
    echo "export PATH=$HOME/.local/bin:\$PATH" >> ~/.bashrc
fi

こちらも参考に

オフライン環境にpythonパッケージをインストールする - Qiita

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした