LoginSignup
5
9

More than 5 years have passed since last update.

Macで始めるPython環境づくり2019 初心者編

Posted at

はじめに

この記事は最近MacBookProの2018年版TouchBar付きをPayPayで購入した筆者が、
家でPython開発を始めようとしている記事です。
正直初心者なので、深い部分は理解していないものも多いですが、
初心者の方の参考になれば幸いです。

仮想環境選び Pyenv+Pipenv

Pyenvは特に要らないとか、venvが公式だからいいとか色々と記事が書かれていますが、まぁ個人的に困らないものを使えばいいと思っているので、今回はPyenv + Pipenvで環境構築を行ってみます。これがベストかは知りませんが、特に問題なく導入できたので僕は良いと思います。

XCodeのインストール

本体は普通にXCodeをAppStoreからインストールします。
そのあとターミナルで下記を実行して、Command Line Toolsをインストールする。

$ xcode-select --install

Homebrewのインストール

HomebrewはMacでのパッケージ管理ツールみたいなものと理解しています。
pyenvをインストールするためにもまずはインストール。
インストール先にあるコマンドをそのままコピペすればOK。

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

pyenvのインストール

Homebrewさえ入れば、あとはpython系を入れていくだけです。
まずはpyenvを入れます。

$ brew install pyenv

その後、PATHを通すので、home/.bash_profileに下記を書き込みます。viでの編集に慣れている人は、下記を実行して、.bash_profileに書き込みます。

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

慣れていない人は、ファインダーからホームディレクトリの隠しファイルを表示してテキストエディタで.bash_profileの中身を編集しましょう。ちなみに隠しファイルはMacでは下記のショートカットで表示できます。

[command] + [shift] + [.(ドット)]

その後設定したファイルを反映させます。

$ source ~/.bash_profile

一応、動いているかを確認するため、叩いてみましょう。
下記のようなヘルプ表示が出ればOKです。

$ pyenv
pyenv 1.2.9
Usage: pyenv <command> [<args>]

Some useful pyenv commands are:
   commands    List all available pyenv commands
   local       Set or show the local application-specific Python version
   global      Set or show the global Python version
   shell       Set or show the shell-specific Python version
   install     Install a Python version using python-build
   uninstall   Uninstall a specific Python version
   rehash      Rehash pyenv shims (run this after installing executables)
   version     Show the current Python version and its origin
   versions    List all Python versions available to pyenv
   which       Display the full path to an executable
   whence      List all Python versions that contain the given executable

See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme

Pipenvのインストール

次にPipenvをインストールします。
brewで一発です。

$ brew install pipenv

こちらも設定を.bash_profileに追記します。
仮想環境をフォルダに作成するフラグを立てておくことがメインです。
ついでにインストールがうまくいっているか叩いてみましょう。

.bash_profile
export PIPENV_VENV_IN_PROJECT=true
eval "$(pipenv --completion)"
$ source ~/.bash_profile
$ pipenv
Usage: pipenv [OPTIONS] COMMAND [ARGS]...

Options:
  --where             Output project home information.
  --venv              Output virtualenv information.
  --py                Output Python interpreter information.
  --envs              Output Environment Variable options.
  --rm                Remove the virtualenv.
  --bare              Minimal output.
  --completion        Output completion (to be eval'd).
  --man               Display manpage.
  --support           Output diagnostic information for use in GitHub issues.
  --site-packages     Enable site-packages for the virtualenv.  [env var:
                      PIPENV_SITE_PACKAGES]
  --python TEXT       Specify which version of Python virtualenv should use.
  --three / --two     Use Python 3/2 when creating virtualenv.
  --clear             Clears caches (pipenv, pip, and pip-tools).  [env var:
                      PIPENV_CLEAR]
  -v, --verbose       Verbose mode.
  --pypi-mirror TEXT  Specify a PyPI mirror.
  --version           Show the version and exit.
  -h, --help          Show this message and exit.


Usage Examples:
   Create a new project using Python 3.7, specifically:
   $ pipenv --python 3.7

   Remove project virtualenv (inferred from current directory):
   $ pipenv --rm

   Install all dependencies for a project (including dev):
   $ pipenv install --dev

   Create a lockfile containing pre-releases:
   $ pipenv lock --pre

   Show a graph of your installed dependencies:
   $ pipenv graph

   Check your installed dependencies for security vulnerabilities:
   $ pipenv check

   Install a local setup.py into your virtual environment/Pipfile:
   $ pipenv install -e .

   Use a lower-level pip command:
   $ pipenv run pip freeze

Commands:
  check      Checks for security vulnerabilities and against PEP 508 markers
             provided in Pipfile.
  clean      Uninstalls all packages not specified in Pipfile.lock.
  graph      Displays currently-installed dependency graph information.
  install    Installs provided packages and adds them to Pipfile, or (if no
             packages are given), installs all packages from Pipfile.
  lock       Generates Pipfile.lock.
  open       View a given module in your editor.
  run        Spawns a command installed into the virtualenv.
  shell      Spawns a shell within the virtualenv.
  sync       Installs all packages specified in Pipfile.lock.
  uninstall  Un-installs a provided package and removes it from Pipfile.
  update     Runs lock, then sync.

pyenvとpipenvを利用したpython環境構築

ここまででインストレーションは終わりました。
ここから実際にPythonの環境を作っていきます。
今回は例として、python3.5.6の環境を作ってきます。
pipenvはpyenvの仮想環境をラップするようなイメージで動きますので、まずpython3.5系列をpyenvでインストールしてあげてから、pipenvでディレクトリの環境を整えます。

$ pyenv install  3.5.6

これでpipenvを使う準備が整いました。プロジェクトディレクトリを作りましょう。

$ mkdir sample_project
$ cd sample_project
$ pipenv --python 3.5.6

これでsample_project内でpython3.5を使って開発をすることができます。
あとは動くかを試してみましょう。

pipenvで作った環境をactivateするには、shellというコマンドを実行します。
ぬける際にはexitです。
下記例を見るとわかる通り、pipenvの仮想環境をactivateしている状態では、pythonのバージョンが3.5.6になっており、deactivateした状態では2.7.10となっています。ちなみに作った仮想環境は、sample_project/.venvに作られていますので、pycharmなどのinterpreterの設定も簡単に行えます。

$ pipenv shell
$ python --version
Python 3.5.6
$ exit
$ python --version
Python 2.7.10

これでpyenvとpipenvを使った簡単なpython環境構築は完了です。
凝ったことはできないので、今後も初心者向けの投稿をしていくつもりです。

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