LoginSignup
2
4

More than 3 years have passed since last update.

MacにVirtualenvをインストールする

Posted at

Virtualenvをインストールする

Virtualenvとは

  • Sandboxに近いイメージ
  • 隔離された環境を用意して、その中でのみ
  • Pythonのためのツール

Motivation

  • gitなどで試しに使ってみたいだけで恒久的には必要ない可能性のあるもの
  • 他のパッケージのバージョン依存が心配な場合

Install

  • MacOS
  • pipインストール済み

pipを使ってインストールしていく

$ sudo pip install virtualenv
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
WARNING: The directory '/Users/[Username]/Library/Caches/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting virtualenv
  Downloading virtualenv-20.0.20-py2.py3-none-any.whl (4.7 MB)
     |████████████████████████████████| 4.7 MB 8.6 MB/s 
Collecting importlib-resources<2,>=1.0; python_version < "3.7"
  Downloading importlib_resources-1.5.0-py2.py3-none-any.whl (21 kB)
Collecting importlib-metadata<2,>=0.12; python_version < "3.8"
  Downloading importlib_metadata-1.6.0-py2.py3-none-any.whl (30 kB)
Collecting contextlib2<1,>=0.6.0; python_version < "3.3"
  Downloading contextlib2-0.6.0.post1-py2.py3-none-any.whl (9.8 kB)
Collecting pathlib2<3,>=2.3.3; python_version < "3.4" and sys_platform != "win32"
  Downloading pathlib2-2.3.5-py2.py3-none-any.whl (18 kB)
Collecting filelock<4,>=3.0.0
  Downloading filelock-3.0.12.tar.gz (8.5 kB)
Collecting distlib<1,>=0.3.0
  Downloading distlib-0.3.0.zip (571 kB)
     |████████████████████████████████| 571 kB 19.1 MB/s 
Collecting appdirs<2,>=1.4.3
  Downloading appdirs-1.4.3-py2.py3-none-any.whl (12 kB)
Requirement already satisfied: six<2,>=1.9.0 in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from virtualenv) (1.12.0)
Collecting zipp>=0.4; python_version < "3.8"
  Downloading zipp-1.2.0-py2.py3-none-any.whl (4.8 kB)
Collecting typing; python_version < "3.5"
  Downloading typing-3.7.4.1-py2-none-any.whl (26 kB)
Collecting singledispatch; python_version < "3.4"
  Downloading singledispatch-3.4.0.3-py2.py3-none-any.whl (12 kB)
Collecting configparser>=3.5; python_version < "3"
  Downloading configparser-4.0.2-py2.py3-none-any.whl (22 kB)
Collecting scandir; python_version < "3.5"
  Downloading scandir-1.10.0.tar.gz (33 kB)
Building wheels for collected packages: filelock, distlib, scandir
  Building wheel for filelock (setup.py) ... done
  Created wheel for filelock: filename=filelock-3.0.12-py2-none-any.whl size=7574 sha256=626e83e59d82c8c380c7cd32d221f74b6be0dbc746ad5d0b57bdcd1a06e0085d
  Stored in directory: /private/tmp/pip-ephem-wheel-cache-NWPDKz/wheels/b9/91/23/b559c1f4fd55056712b3a71cd9cab1dc0089e2232d502ed72e
  Building wheel for distlib (setup.py) ... done
  Created wheel for distlib: filename=distlib-0.3.0-py2-none-any.whl size=340429 sha256=ab8e5e40964d8f9ae6864ad56013f72c32507ab12adeaf6f5e7eab26cb4ee950
  Stored in directory: /private/tmp/pip-ephem-wheel-cache-NWPDKz/wheels/0c/88/ac/41500883ea902d3409a83a827870a726346b5ebfd0523e91df
  Building wheel for scandir (setup.py) ... done
  Created wheel for scandir: filename=scandir-1.10.0-cp27-cp27m-macosx_10_15_x86_64.whl size=18201 sha256=e97e3e174d2c9c9a2a99af91ffeb3c6b1a4252a5d53fc06f2aa6febc2fe4b23b
  Stored in directory: /private/tmp/pip-ephem-wheel-cache-NWPDKz/wheels/58/2c/26/52406f7d1f19bcc47a6fbd1037a5f293492f5cf1d58c539edb
Successfully built filelock distlib scandir
Installing collected packages: configparser, contextlib2, zipp, scandir, pathlib2, importlib-metadata, typing, singledispatch, importlib-resources, filelock, distlib, appdirs, virtualenv
Successfully installed appdirs-1.4.3 configparser-4.0.2 contextlib2-0.6.0.post1 distlib-0.3.0 filelock-3.0.12 importlib-metadata-1.6.0 importlib-resources-1.5.0 pathlib2-2.3.5 scandir-1.10.0 singledispatch-3.4.0.3 typing-3.7.4.1 virtualenv-20.0.20 zipp-1.2.0

インストールされているか確認するためにVersionを確認する

$ virtualenv --version
virtualenv 20.0.20 from /Library/Python/2.7/site-packages/virtualenv/__init__.pyc

使い方

  1. 実行環境のためのディレクトリをほる
  2. お約束
  3. Activateする
  4. 自由に遊ぶ
  5. Deactivateする

1. 実行環境のためのディレクトリをほる

2.でvirtualenvをここで使います!という宣言のもとに必要なパッケージがそのディレクトリに用意されるので、
プロジェクト名など自由に名前を決めてディレクトリを作成する。

$ mkdir test-project01

2.お約束

上で述べたように事前準備のために以下実行。1,2はどちらもプロジェクトごとに実行必須

$ virtualenv venv

3. Activateする

2までで準備が完了したので、実際にVirtualenvコマンドで仮想的な環境を作り出す

$ source venv/bin/activate

コンソールの先頭に(venv)がついていればOK

4. 自由に遊ぶ

これで隔離された環境が出来上がったので、pipなりgitコマンドなりでいろいろインストールして使ってミス

5. Deactivateする

上記の環境を終了させるためのdeactivate

$ deactivate

参考

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