LoginSignup
0
1

【Anacondaアンインストール => pyenv】Pythonの環境構築

Last updated at Posted at 2024-04-25

はじめに

なにもわからずとりあえずAnacondaを入れていたため、パッケージマネージャの勉強も兼ねて調べながら、環境を整理しました。
自分のためのメモです。

目次

  • Anacondaのアンインストール
  • HomebrewのPythonの削除
  • pyenvでのpytnonインストール

動作環境

  • Mac OS Sonoma 14.4.1

アンインストール


手順

  1. anaconda-cleanのインストール
  2. anaconda-cleanコマンドの実行
  3. 「~/.bash_profile」と「~/.zshrc」の内容削除
  4. anacondaディレクトリの削除
  5. Anaconda-Navigatorアプリの削除
  6. 確認

1. anaconda-cleanのインストール

terminal
% conda install anaconda-clean

2. anaconda-cleanコマンドの実行

terminal
% anaconda-clean

このとき複数のディレクトリの削除が促されるため、yキーを押しすべて削除する

3. 「~/.bash_profile」と「~/.zshrc」の内容削除

「~/.bash_profile」

まずエディタで.bash_profileをひらく

terminal
% code ~/.bash_profile

.bash_profileの>>> conda initialize >>>から<<< conda initialize <<<まで削除して保存する

.bash_profile
# >>> conda initialize >>> # ここから
#...
省略

# <<< conda initialize <<< # ここまで
「~/.zshrc」

同様にエディタで.zshrcをひらき>>> conda initialize >>>から<<< conda initialize <<<まで削除して保存する


4. anacondaディレクトリの削除

envコマンドでPATHを確認する

terminal
% env
# ...
PATH=/Users/[ユーザ名]/opt/anaconda3/bin:...

# ...

CONDA_PYTHON_EXE=/Users/[ユーザ名]/opt/anaconda3/bin/python
CONDA_SHLVL=1
CONDA_PREFIX=/Users/[ユーザ名]/opt/anaconda3
CONDA_DEFAULT_ENV=base
CONDA_PROMPT_MODIFIER=(base)

rmコマンドでAnacondaディレクトリを削除する
(perission deniedなどが表示された場合、sudo rmで実行する)

terminal
% rm -fr /Users/[ユーザ名]/opt/anaconda3
% ls -l /Users/[ユーザ名]/opt

5. Anaconda-Navigatorアプリの削除

Finderでアプリケーションフォルダを開きAnaconda-Navigatorアプリを削除する

6. 確認

PATHを確認する

terminal
% env

4. anacondaディレクトリの削除envコマンドで確認したときにあったPATH=/Users/[ユーザ名]/opt/anaconda3/bin:...などが消えていることを確認

HomebrewのPythonの削除

確認したところbrewでPythonがいくつかインストールされていたためアンインストール

terminal
% brew list | grep python
python-distlib python-filelock python-platformdirs python@3.10 python@3.11 python@3.12

アンインストールしてみる

terminal
% brew uninstall python-distlib python-filelock python-platformdirs python@3.10 python@3.11 python@3.12
Error: Refusing to uninstall /opt/homebrew/Cellar/python@3.12/3.12.2_1, /opt/homebrew/Cellar/python-distlib/0.3.8, /opt/homebrew/Cellar/python-filelock/3.13.1 and /opt/homebrew/Cellar/python-platformdirs/4.2.0
because they are required by glib, harfbuzz, pillow and virtualenv, which are currently installed.
You can override this and force removal with:
  brew uninstall --ignore-dependencies python-distlib python-filelock python-platformdirs python@3.10 python@3.11 python@3.12

アンインストールしようとしたところ依存関係のエラーがでたため、エラー分に従い次のコマンドを打った

--ignore-dependenciesオプションを使用すると、依存関係を無視してパッケージをアンインストールすることができる

terminal
% brew uninstall --ignore-dependencies python-distlib python-filelock python-platformdirs python@3.10 python@3.11 python@3.12

無事、HomebrewのPythonをアンインストールできた

pyenvでのpytnonインストール

1.brew updateを行った

terminal
% brew update

2.pyenvのインストール

terminal
% brew install pyenv

3.PATHの指定

terminal
% echo 'eval "$(pyenv init --path)"' >> ~/.zprofile
% source ~/.zprofile
terminal
% echo 'eval "$(pyenv init -)"' >> ~/.zshrc
% source ~/.zshrc

4.バージョンの確認

terminal
% pyenv -v
pyenv 2.4.0

5.インストール可能なPythonのバージョン一覧を表示

terminal
% pyenv install -l
Available versions:
  2.1.3
  2.2.3
  ...

  ...
  3.12.2
  3.13.0a5
  3.13-dev
  ...

6. 最新バージョンの3.12.2をインストール

terminal
% pyenv install 3.12.2

7. グローバルにバージョンを設定

terminal
pyenv global 3.12.2

ローカル(ディレクトリごとにバージョンを設定する場合は`pyenv local [Pythonのバージョン])

8. Pythonのバージョンを確認

terminal
% python -V
Python 3.12.2

おわりに

はじめてQiitaで記事を書いたので至らない点や間違っている箇所がありましたら、コメントなど頂けると助かります。

後々virtualenvやpyenvの使い方についても理解が深まったら書こうと思います。

参考

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