1
1

More than 3 years have passed since last update.

python仮想環境を使う【venv】

Posted at

はじめに

pythonの仮想環境について教わったメモです。

なぜpython仮想環境が必要なの?

  • プロジェクトごとに必要なライブラリを管理するため
  • ライブラリ同士の競合を防ぐため

環境

2つの環境で試しました。

  1. Windows 10 + Python 3.7.3
  2. Linux(Debian 10.2) + Python 3.7.3

venvでpython仮想環境を使う

仮想環境を作成する

Windows
>python -m venv testvenv
Linux
$ python3 -m venv testvenv

testvenvは仮想環境の名前です。任意の名前でOKです。
実行すると、カレントディレクトリに、新規ディレクトリtestvenvが作成されます。

仮想環境を起動する

Windows
>testvenv\Scipts\activate

(testvenv) >
Linux
$ source testenv/bin/activate

(testvenv) $

仮想環境を起動した状態で、通常のコマンドプロンプト・ターミナルと同様のコマンドが使えます。
pipコマンドでライブラリをインストールすると、仮想環境にインストールされます。仮想環境を抜けた状態でこのライブラリを使用しようとしても、使えません。
pip freezeすると、仮想環境にインストールされているライブラリを確認できます。

仮想環境を終了する

Windows
(testvenv) >deactivate
>
Linux
(testvenv) $ deactivate
$
1
1
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
1
1