python 仮想環境 Pipenv

# 前提環境

* Windows10 64bit
* python3.8.2

# Pipenv

* [公式サイト](https://github.com/pypa/pipenv)
* [公式ドキュメント](https://pipenv.pypa.io/en/latest/basics/#specifying-versions-of-python)

### インストール

```console
D:\work> pip install pipenv
Collecting pipenv
  Using cached pipenv-2020.8.13-py2.py3-none-any.whl (3.9 MB)
Requirement already satisfied: pip>=18.0 in c:\users\username\appdata\local\programs\python\python38\lib\site-packages (from pipenv) (20.2.3)
Requirement already satisfied: certifi in c:\users\username\appdata\local\programs\python\python38\lib\site-packages (from pipenv) (2020.6.20)
Requirement already satisfied: setuptools>=36.2.1 in c:\users\username\appdata\local\programs\python\python38\lib\site-packages (from pipenv) (41.2.0)
Requirement already satisfied: virtualenv in c:\users\username\appdata\local\programs\python\python38\lib\site-packages (from pipenv) (20.0.33)
Requirement already satisfied: virtualenv-clone>=0.2.5 in c:\users\username\appdata\local\programs\python\python38\lib\site-packages (from pipenv) (0.5.4)
Requirement already satisfied: filelock<4,>=3.0.0 in c:\users\username\appdata\local\programs\python\python38\lib\site-packages (from virtualenv->pipenv) (3.0.12)
Requirement already satisfied: distlib<1,>=0.3.1 in c:\users\username\appdata\local\programs\python\python38\lib\site-packages (from virtualenv->pipenv) (0.3.1)
Requirement already satisfied: appdirs<2,>=1.4.3 in c:\users\username\appdata\local\programs\python\python38\lib\site-packages (from virtualenv->pipenv) (1.4.4)
Requirement already satisfied: six<2,>=1.9.0 in c:\users\username\appdata\local\programs\python\python38\lib\site-packages (from virtualenv->pipenv) (1.15.0)
Installing collected packages: pipenv
Successfully installed pipenv-2020.8.13
```

インストール確認

```console
D:\work> 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 executed by the
                                  shell).

  --man                           Display manpage.
  --support                       Output diagnostic information for use in
                                  GitHub issues.

  --site-packages / --no-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 PyUp Safety 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  Uninstalls a provided package and removes it from Pipfile.
  update     Runs lock, then sync.
```

### 仮想環境作成

#### 仮想環境用のフォルダを用意

```console
D:\work> mkdir mytestenv


    ディレクトリ: D:\work


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2020/10/07     18:55                mytestenv
```

#### 仮想環境作成

```console
D:\work> cd mytestenv
D:\work\mytestenv> pipenv install
Creating a virtualenv for this project…
Pipfile: D:\work\mytestenv\Pipfile
Using C:/Users/username/AppData/Local/Programs/Python/Python38/python.exe (3.8.2) to create virtualenv…
[=== ] Creating virtual environment...created virtual environment CPython3.8.2.final.0-64 in 4929ms
  creator CPython3Windows(dest=C:\Users\username\.virtualenvs\mytestenv-1lNwFUaT, clear=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\username\AppData\Local\pypa\virtualenv)
    added seed packages: pip==20.2.3, setuptools==50.3.0, wheel==0.35.1
  activators BashActivator,BatchActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

Successfully created virtual environment!
Virtualenv location: C:\Users\username\.virtualenvs\mytestenv-1lNwFUaT
Creating a Pipfile for this project…
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Updated Pipfile.lock (db4242)!
Installing dependencies from Pipfile.lock (db4242)…
  ================================ 0/0 - 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
```

### 仮想環境に切り替え

```console
PS D:\work\mytestenv> pipenv shell
```

```console:切り替わった状態
Launching subshell in virtual environment…
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

新しいクロスプラットフォームの PowerShell をお試しください https://aka.ms/pscore6
PS D:\work\mytestenv> 
```

```console:試しにライブラリ追加
D:\work\mytestenv> pip list
Package    Version
---------- -------
pip        20.2.3
setuptools 50.3.0
wheel      0.35.1

D:\work\mytestenv> pip install numpy
Collecting numpy
  Using cached numpy-1.19.2-cp38-cp38-win_amd64.whl (13.0 MB)
Installing collected packages: numpy
Successfully installed numpy-1.19.2

D:\work\mytestenv> pip list
Package    Version
---------- -------
numpy      1.19.2
pip        20.2.3
setuptools 50.3.0
wheel      0.35.1
```

```console:仮想環境から戻る
D:\work\mytestenv> exit・・・これで戻れる
D:\work\mytestenv> pip list・・・本当に戻ったか確認
Package                          Version
-------------------------------- ---------
appdirs                          1.4.4
asgiref                          3.2.7
blinker                          1.4
(省略)
```

# おわり

pythonのバージョン切り替えはまだマシンに別バージョンのpythonをインストールしたいといけないぽく見えるので、
また今度。

### pythonのバージョン指定

こうやるらしい。

```
pipenv --python 3.6
```

```
pipenv --python 2.7.14
```