LoginSignup
1
2

More than 1 year has passed since last update.

【macOS 12.3.1 intelchip】python環境構築まとめ

Last updated at Posted at 2022-04-05

備忘録です.

python環境がごちゃごちゃしてたので、色々消して以下にまとめた.(2022.4.5)
環境:MacBookPro macOS 12.3.1(intelchip)

要約

Homebrew + pyenvでpythonバージョン管理 + venvでプロジェクトごとにパッケージ管理

1.Homebrewインストール

ここからインストール

2.pyenvインストール+環境設定

pyenvのインストール

zsh
$ brew install pyenv

PATHを通す.
この3行目がないと、Macにデフォルトで入ってるPythonが呼び出されうまくいかなかった.
これらの意味は後程調べて記載予定.

~/.zshrc
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"

~/.zshrcは”ホームディレクトリ下”にあるzshシェルのログインコマンドを記載している.
iterm2を再起動か、以下のコマンド実行

zsh
source ~/.zshrc

pythonのインストール

zsh
pyenv install -l

まずはインストールできるバージョン確認.

zsh
pyenv install 3.10.1

この時、バージョンによっては、以下のエラーが出てインストールできず、、謎.

zsh
checking for --with-cxx-main=<compiler>... no
checking for clang++... no
configure:

  By default, distutils will build C++ extension modules with "clang++".
  If this is not intended, then set CXX on the configure command line.

checking for the platform triplet based on compiler characteristics... darwin
configure: error: internal configure error for the platform triplet, please file a bug report
:...skipping...
BUILD FAILED (OS X 12.3.1 using python-build 20180424)

Inspect or clean up the working tree at /var/folders/s3/dnk78f2s5pzg502lv6dhblkw0000gn/T/python-build.20220405163529.14330
Results logged to /var/folders/s3/dnk78f2s5pzg502lv6dhblkw0000gn/T/python-build.20220405163529.14330.log

とりあえず、インストールできたら、グローバルに設定.そして確認.

zsh
pyenv global 3.10.1
pyenv versions
  system
* 3.10.1 (set by /Users/takasukaoru/.pyenv/version)
  3.9.11

無事3.10.1が設定されていることを確認.
一応,pyenvのpythonがpath通ってることも確認.(システムデフォルトではなくなっているはず)

zsh
python -V
Python 3.10.1

OK.

3.venvで仮想環境構築

以下はvscodeのzshターミナルで実行.
プロジェクトフォルダ下で、以下を実行.

zsh
python -m venv .venv

.venvは仮想環境名なので、任意.
カレントディレクトリ名の前に(.venv)と表示されればOK.

zsh
(.venv) 
[takasukaoru@*****:~/python_project/****][master]
$ 

表示されていない場合、以下のコマンドを実行して、venv仮想環境に入る.

zsh
source .venv/bin/activate

ここで、pipインストールすることで、ローカル環境を汚さずにパッケージインストール可能.
仮想環境から抜け出るときは,deactivateのみでOK.

zsh
deactivate

また入る時は、

zsh
source .venv/bin/activate
1
2
1

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
1
2