8
6

More than 3 years have passed since last update.

macOSでpythonを使えるようにした

Last updated at Posted at 2021-01-01

背景

macOS BigSurでpythonをつかおうとおもった。
(CuteRというコマンドを使いたかった:CuteRでQRコード画像をつくった

ところが、pipをつかうと

$ pip install
dyld: Library not loaded: /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation
  Referenced from: /Library/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python
  Reason: image not found
Abort trap: 6

となってしまう。

$ python3でもおなじ。

基本的な方向性

macOS環境のPython:https://www.python.jp/install/macos/index.html

によると

  • macOSに入っているpythonを使おうとするな
  • Homebrewでインストールせよ
  • pip3でなくてpython3.9 -m pip xxxとせよ
  • venvなど仮想環境で動かせ

ということだそう。ここでは、上の3つまでやってみた。

作業ログ

$ brew updateすると、まずはgit - C ...をせよとのお達し。

$ brew update
Error: homebrew-core is a shallow clone. To `brew update` first run:
  git -C "/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core" fetch --unshallow

まずはfetchして、$ brew update

$ git -C "/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core" fetch --unshallow
remote: Enumerating objects: 690418, done.
remote: Counting objects: 100% (690359/690359), done.
remote: Compressing objects: 100% (233146/233146), done.
remote: Total 680792 (delta 450632), reused 674814 (delta 444788), pack-reused 
Receiving objects: 100% (680792/680792), 271.20 MiB | 14.34 MiB/s, done.
Resolving deltas: 100% (450632/450632), completed with 7510 local objects.
From https://github.com/Homebrew/homebrew-core
   baf6aab591..b293a33dd8 master     -> origin/master

$ brew update

pythonのインストール。

$ brew install python
Warning: python@3.9 3.9.1_3 is already installed, it's just not linked
You can use `brew link python@3.9` to link this version.

入っているらしい。リンクすればいいのね。

$ brew link python@3.9
Linking /usr/local/Cellar/python@3.9/3.9.1_3... 
Error: Could not symlink bin/2to3
Target /usr/local/bin/2to3
already exists. You may want to remove it:
  rm '/usr/local/bin/2to3'

To force the link and overwrite all conflicting files:
  brew link --overwrite python@3.9

To list all files that would be deleted:
  brew link --overwrite --dry-run python@3.9

ふむ、rmしてbrew linkね。

$ rm '/usr/local/bin/2to3'
$ brew link --overwrite python@3.9
Linking /usr/local/Cellar/python@3.9/3.9.1_3... 25 symlinks created

これをしたけど、

$ pip install 
$ python3

とすると、また冒頭と同じエラー。そうではなくて

$ python3.9
Python 3.9.1 (default, Dec 28 2020, 11:25:16) 
[Clang 12.0.0 (clang-1200.0.32.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

いけたー! ということで、下記サイトを参考にしてpipを試す。
https://www.python.jp/install/macos/pip.html

$ python3.9 -m pip install pillow
Collecting pillow
  Downloading Pillow-8.0.1-cp39-cp39-macosx_10_10_x86_64.whl (2.2 MB)
     |████████████████████████████████| 2.2 MB 3.7 MB/s 
Installing collected packages: pillow
Successfully installed pillow-8.0.1
WARNING: You are using pip version 20.3.1; however, version 20.3.3 is available.
You should consider upgrading via the '/usr/local/opt/python@3.9/bin/python3.9 -m pip install --upgrade pip' command.

といわれるので、御意。

$ python3.9 -m pip install --upgrade pip
Requirement already satisfied: pip in /usr/local/lib/python3.9/site-packages (20.3.1)
Collecting pip
  Downloading pip-20.3.3-py2.py3-none-any.whl (1.5 MB)
     |████████████████████████████████| 1.5 MB 6.2 MB/s 
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 20.3.1
    Uninstalling pip-20.3.1:
      Successfully uninstalled pip-20.3.1
Successfully installed pip-20.3.3

上記サイトに従って、いったんpillowはアンインストール

$ python3.9 -m pip uninstall pillow
Found existing installation: Pillow 8.0.1
Uninstalling Pillow-8.0.1:
  Would remove:
    /usr/local/lib/python3.9/site-packages/PIL/*
    /usr/local/lib/python3.9/site-packages/Pillow-8.0.1.dist-info/*
Proceed (y/n)? y
  Successfully uninstalled Pillow-8.0.1

これでよいのだな。

感想

  • 「macOSに入っているpythonを使わない」というのがめちゃくちゃシンプルでスッキリした
  • ここでは書かなかったけれど、「venvを使う」というのもシンプルで、すごくスッキリした

リソース

macOS環境のPython:https://www.python.jp/install/macos/index.html
macOS環境のPython - pip :https://www.python.jp/install/macos/pip.html

ほとんど↑に書いてある。この記事はいわば「つくれぽ」。

環境

Python 3.9.1 (Dec 28 2020, 11:25:16)
macOS BigSur バージョン 11.1
MacBook Pro (13-inch, 2020, Four Thunderbolt 3 ports)

8
6
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
8
6