LoginSignup
2
5

More than 3 years have passed since last update.

⭐︎初心者でも分かる仮想環境構築

Last updated at Posted at 2020-02-05

目的

  1. 仮想環境を上手く使いこなせれていなかったため、今後のために環境構築を最初からやり直す。
  2. これからデータサイエンスをやりたいのでJupyter Notebookのインストールする。

*追記1 2.2においてインストール後にpythonを使う設定が出来てなかったため追加(2020.2.16)

1.プログラムのアンイストール

友人の勧めでHomebrewのインストールを行なっていたが、入門 Python3を行う中で、仮想環境を使用していなかった。なので、まずは不要なものをアンイストール。

#Homebrewにインストールされているパッケージを確認する。
$ brew list
autoconf    pkg-config  rbenv       redis
openssl@1.1 pyenv       readline    ruby-build

#移動した後にプログラムのアンイストール
$ brew uninstall pyenv
Uninstalling /usr/local/Cellar/pyenv/1.2.15... (663 files, 2.5MB)

$ brew uninstall rbenv
Uninstalling /usr/local/Cellar/rbenv/1.1.2... (36 files, 65KB)

$ brew uninstall redis
Uninstalling /usr/local/Cellar/redis/5.0.7... (13 files, 3.1MB)

$ brew uninstall ruby-build
Uninstalling /usr/local/Cellar/ruby-build/20191111... (461 files, 231.0KB)

#リスト確認
$ brew list
autoconf    openssl@1.1 pkg-config  readline

2. 再インストール

2.1. pyenv(バージョンの切り替え)

$ brew install pyenv

#pyenvに/usr/local/var/pyenvを使うように命令
$ cat << 'EOS' >> ~/.bash_profile
> export PYENV_ROOT=/usr/local/var/pyenv
> if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
> EOS

#~/.bash_profileに書いた内容を有効にするのだが、コマンドがないと怒られた。
#先ほどアンインストールしていたので再度インストールする。
$ source ~/.bash_profile
-bash: rbenv: command not found
$ brew install rbenv
#成功した。
$ source ~/.bash_profile


#インストールできたか確認する。
#成功しているね。
$ brew list | grep pyenv
pyenv
$ pyenv --version
pyenv 1.2.16

2.2. Pythonのインストール

上記のbrew listの結果を見ると今までデフォルトのPythonを使っていたみたい。パーミッションの問題やセキュリティの問題から長く使い続けるには適していないようだ。

#インストールできるPythonの一覧表示する。
$ pyenv install -l

#Python 3.8.0をインストール
$ % pyenv install 3.8.0  
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.8.0.tar.xz...
-> https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tar.xz
Installing Python-3.8.0...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.8.0 to /Users/uemura/.pyenv/versions/3.8.0

#Python 3.8.0を使うように指定。
% pyenv global 3.8.0  

#確認
% pyenv versions
  system
* 3.8.0 (set by /Users/.pyenv/version)

2.3. venv(仮想環境の作成)


#ディレクトリ作成
$mkdir kaggle0205
#ディレクトリへ移動
$ cd ./kaggle0205
#python3.8.1についてるvenvコマンドでkaggle0205 (ユーザー名)/practice1というディレクトリの中にpractice1という仮想環境を構築した。
kaggle0205 $ python -m venv practice1

3. 仮想環境の有効化・無効化

これからPythonを使うときは作成した仮想環境(ここではpractice1)を有効化する


#source 仮想環境名/bin/activateで有効化
kaggle0205 (ユーザー名)$ source practice1/bin/activate

#仮想環境practice1を無効化する
(practice1) $ deactivate

4. Jupiter Notebookのインストール

ググってみるとCondaやAnacondaを利用してインストールするやり方が多く
、Homebrewでのインストール方法は数が少なかった。


#仮想環境有効化
$ source practice1/bin/activate
#jupyterインストール
(practice1) $ pip3 install jupyter
#jupyter notebookを起動する。
$ jupyter notebook

  • 仮想環境内でJupyter Notebookをインストールしたが、これは仮想環境外でも使用可能なのかと思い、起動してみたが使用不可能でした。

$ jupyter notebook
-bash: jupyter: command not found

  • ctl+cを押して5秒以内に「y」キーで終了させる。

感想

  • パッケージ管理のHomebrewを実行した上で、venvやpyenvはPythonのツールであり、これを使って環境構築するんだね。
  • 仮想環境を有効化してPythonを使うことが重要なんだね。
参考文献

「【2019年版】Pythonインストール・Mac編【長く安全に使える環境構築】」
https://basicincome30.com/python-install-mac

「【2019年5月】MacBookにJupyter Notebookをインストールする (macOS 10.14.4/Mojave)」
https://qiita.com/inai/items/e9da22eb336f7f2cd375

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