LoginSignup
8
8

More than 3 years have passed since last update.

多分これが一番簡単だと思います。 WindowsでPython仮想環境構築 2019年

Last updated at Posted at 2019-09-08

この記事を読んで出来る事

Python2.7とPython3.7の仮想環境を構築してVS Codeで実行環境を切り替える。(※Pythonのバージョンは任意)

必要な環境

  • Windows 10
  • Visual Studio Code
  • Python Extension Pack (VS Code拡張機能)
  • Powershell

新規にインストールされるパッケージ

  • Python2.7
  • Python3.7
  • pipenv (pythonモジュール)

手順

Python3.7をインストール

Python3.7
https://www.python.org/downloads/release/python-374/
※ PC環境に応じてどちらかを選択
32bit: Windows x86 executable installer
64bit: Windows x86-64 executable installer

※ 注意:「Add Path」はデフォルトで使いたいPythonのバージョンのみ設定。(ここではPython 3.7)

Python2.7をインストール

Python2.7
https://www.python.org/downloads/release/python-2716/
※ PC環境に応じてどちらかを選択
32bit: Windows x86 MSI installer
64bit: Windows x86-64 MSI installer

※「Install just for me」を選択してPython3系のインストール先にあわせるのがオススメ
py2-install-screenshot.jpg

pipenvをインストール

Powershellを開いて、pythonがインストールされているか確認

PS> python --version
Python 3.7.3

pipenvをインストールする

PS> pip install pipenv

pipenv用環境変数を設定

上の手順までで仮想環境は利用できますが、プロジェクト毎(フォルダ毎)に仮想環境を切り替えられたほうが取り回しがシンプルになるのでpipenv用の環境変数を設定します。(Win + s環境変数を編集)

名前 設定値
変数名 PIPENV_VENV_IN_PROJECT
変数値 True

仮想環境を作成

PS> mkdir sample
PS> cd sample
PS> pipenv install --python 3.7

※ここでは仮想環境にpython 3.7を利用

VS Codeで仮想環境の切り替え

  1. VS Codeを起動して、上で作成したsampleフォルダーを開く。
  2. 左下ステータスバーのPythonの文字をクリックして仮想環境を選択する vscode-switch-python-screenshot.jpg

補足

仮想環境で使えるPythonはPCにインストールされている必要があります。
例えば、仮想環境でPython3.6.8を使いたい場合 

pipenv install --python 3.6.8

としてもPython3.6.8は使えません。Python3.6.8をインストール後pipenv installする必要があります。

8
8
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
8
8