LoginSignup
7
1

command not found: pip / command not found: python を解決する

Last updated at Posted at 2024-04-28

問題

以下の環境。

  • 2024年4月28日時点
  • python latest stable 3.12
  • MacBook M2 Pro

Python でpip installができない。

$ pip install openai
zsh: command not found: pip

Pythonがインストールされていないか、Pythonのパスが正しく設定されていない。

$ python --version
zsh: command not found: python

解決策

自分の場合、以下の1と2の両方の手順を踏む必要があった。

1. python と pip を入れる

Python がインストールされていないので、Homebrewを使用してインストールする。

$ brew install python
...
==> Installing python@3.12
==> Pouring python@3.12--3.12.3.arm64_ventura.bottle.tar.gz
Warning: These files were overwritten during the `brew link` step:
/opt/homebrew/bin/2to3
/opt/homebrew/bin/idle3
/opt/homebrew/bin/pydoc3
/opt/homebrew/bin/python3
/opt/homebrew/bin/python3-config
/opt/homebrew/share/man/man1/python3.1
/opt/homebrew/lib/pkgconfig/python3-embed.pc
/opt/homebrew/lib/pkgconfig/python3.pc
/opt/homebrew/Frameworks/Python.framework/Headers
/opt/homebrew/Frameworks/Python.framework/Python
/opt/homebrew/Frameworks/Python.framework/Resources
/opt/homebrew/Frameworks/Python.framework/Versions/Current

They have been backed up to: /Users/toshihiro-yokota/Library/Caches/Homebrew/Backup
==> /opt/homebrew/Cellar/python@3.12/3.12.3/bin/python3.12 -Im ensurepip
==> /opt/homebrew/Cellar/python@3.12/3.12.3/bin/python3.12 -Im pip install -v --
==> Caveats
Python has been installed as
  /opt/homebrew/bin/python3

Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
  /opt/homebrew/opt/python@3.12/libexec/bin

See: https://docs.brew.sh/Homebrew-and-Python
...

python3 や pip3 などがインストールされる
ただし、 この時点では python や pip コマンドは使用できない

$ which python3
/opt/homebrew/bin/python3

$ which pip3
/opt/homebrew/bin/pip3

$ which python
python not found

$ which pip
pip not found

python や pip コマンドを使用したい場合は、.zshrc に以下を追加する。

.zshrc
export PATH="$(brew --prefix python)/libexec/bin:$PATH"
$ source ~/.zshrc

python と pip へのパスが通りました。2に続く。

$ which python
/opt/homebrew/opt/python@3.12/libexec/bin/python
$ which pip
/opt/homebrew/opt/python@3.12/libexec/bin/pip

2. pip を使用できるようにする

まだ pip コマンドが使えない。

$ pip install openapi
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try brew install
    xyz, where xyz is the package you are trying to
    install.

    If you wish to install a Python library that isn't in Homebrew,
    use a virtual environment:

    python3 -m venv path/to/venv
    source path/to/venv/bin/activate
    python3 -m pip install xyz

    If you wish to install a Python application that isn't in Homebrew,
    it may be easiest to use 'pipx install xyz', which will manage a
    virtual environment for you. You can install pipx with

    brew install pipx

    You may restore the old behavior of pip by passing
    the '--break-system-packages' flag to pip, or by adding
    'break-system-packages = true' to your pip.conf file. The latter
    will permanently disable this error.

    If you disable this error, we STRONGLY recommend that you additionally
    pass the '--user' flag to pip, or set 'user = true' in your pip.conf
    file. Failure to do this can result in a broken Homebrew installation.

    Read more about this behavior here: <https://peps.python.org/pep-0668/>

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.

エラーメッセージにもあるとおり、
PEP668 に準拠してパッケージのインストールを行う必要がある模様。

以下のいずれかの方法で対応できるようだが、今回は1の方法で対応。

1.venv (仮想環境)を作成&使用する ※推奨

python の venvモジュールを使用し、指定のパス(.python/venv)に仮想環境を作成する。

指定のパスはプロジェクトの配下(~/my-project/my-app/venv)に .gitignore に指定した上で作成されることが多いようだが、
今回はシステム全体で使用できればよいと思ったので、一旦 .python/venv に作成している。

$ python -m venv .python/venv

.python/venv 配下に、いくつかディレクトリが作成される。

$ ls .python/venv
bin		include		lib		pyvenv.cfg	share

bin/activate を実行することで、仮想環境のアクティベート(有効化)を行う。

$ source .python/venv/bin/activate

pip コマンドが使用できるようになりました :raised_hands:

$ pip install openapi
Collecting openai
...
Successfully Installed...

2.pipxを使用する方法

試してない

3.break-system-packages = true を付与する方法

試してない

7
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
1