[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

【Python】仮想環境を作って管理しよう(venv)

どうも、システムソリューション部のinusukiです。

今回はPythonのvenvを利用して仮想環境を作成していきます。

venvって何?

venv はバージョン3.3で追加されたベースPython上に軽量のPython仮想環境を作成するためのモジュールです。

仮想環境を用いることにより、Pythonパッケージのインストールをシステム全体ではなく、プロジェクト単位で行うことができます。

そのため、プロジェクトごとに異なるバージョンやパッケージを使用したり、依存関係による影響を考慮する必要なく、他のプロジェクトを立ち上げたりすることが可能です。

バージョン3.5以降の仮想環境の作成には、 venv の使用が推奨されています。(2023/08 時点)

venv --- 仮想環境の作成

実際に作ってみる

それでは実際に試してみましょう。

バージョン3.3以降であれば、標準機能としてあるので、追加のインストールは不要です。

今回はWindowsでPython 3.11.0を利用しています。

 
python -V
Python 3.11.0

仮想環境の作成

便宜上、test_dirという名前でディレクトリを作成し、その中に仮想環境を作成します。

なお、venv作成時のコマンド(python -m venv)はLinuxでも同じです。

仮想環境の管理を円滑にするためにmyenv1という名前で作成します。

 
mkdir test_dir
cd test_dir
python -m venv myenv1

見やすくするために一部の列を非表示にしていますが、venvの中身は以下の通りです。
今回は利用方法に留まるため、詳細を割愛しますが、仮想環境を構成する上で重要なディレクトリ/ファイルですので、安易に変更を加えないようにしましょう。

 
ls myenv1

ディレクトリ: C:\Users\ichik\test_dir\myenv1

Mode LastWriteTime  Name
---- -------------  ----
d----- 2023/08/28 13:30 Include
d----- 2023/08/28 13:30 Lib
d----- 2023/08/28 13:30 Scripts
-a---- 2023/08/28 13:30 pyvenv.cfg

仮想環境の有効化

以下コマンドで仮想環境を有効化します。

実行後、プロンプトの先頭に環境名が表示されていれば、正常に有効化できています。

.\myenv1\Scripts\Activate.ps1
(myenv1) PS C:\Users\ichik\test_dir>

※実行環境によってコマンドが異なりますので、以下の表を参照してください。

プラットフォーム シェル 仮想環境を有効化するためのコマンド
POSIX bash/zsh $ source <venv>/bin/activate
fish $ source <venv>/bin/activate.fish
csh/tcsh $ source <venv>/bin/activate.csh
PowerShell $ <venv>/bin/Activate.ps1
Windows cmd.exe C:\> <venv>\Scripts\activate.bat
PowerShell PS C:\> <venv>\Scripts\Activate.ps1

環境名が表示されている間はベースのPythonではなく、仮想環境のPythonが実行されます。

なお、標準でvenvを作成した場合の仮想環境のPythonバージョンとベースのPythonバージョンは同じです。

 python -V 
Python 3.11.0 

先述の通り、仮想環境上のパッケージは独立したものとなりますので、あとは好きにインストールしていくだけで利用できます。

仮想環境の非有効化

以下コマンドで仮想環境を非有効化します。

(myenv1) PS C:\Users\ichik\test_dir> deactivate

仮想環境自体が不要になった場合はディレクトリごと削除しても問題ありません。

ex ) 仮想環境をコピーして使いまわしたい

例えば、違うPCで利用したり、他の人に共有して開発したりというような場合、venv自体をコピーして使いまわすといったことはしません。

パッケージの一覧を作成して、ソースコードともにgitなどで共有するようにしましょう。

以下のコマンドでパッケージの一覧を出力できます。

(myenv1) PS C:\Users\ichik\test_dir> python -m pip freeze > requirements.txt

あとは新たに立ち上げた仮想環境で以下のコマンドを実行し、パッケージを一括でインストールしていきます。

(myenv1) PS C:\Users\ichik\test_dir> python -m pip install -r requirements.txt

ex2 ) 仮想環境を異なるバージョンで起動したい(windows)

任意のバージョンをインストールすることで別のバージョンで仮想環境を立ち上げることも可能です。

まずはインストール済みのバージョンを確認します。

py --list
-V:3.11 * Python 3.11 (64-bit)
-V:3.9 Python 3.9 (64-bit)

今回はPython 3.9を利用して新たに立ち上げます。

py -3.9 -m venv pyenv39
.\pyenv39\Scripts\Activate.ps1

ベースとは異なるバージョンで仮想環境が立ち上がりました!

(pyenv39) PS C:\Users\ichik\test_dir> python -V
Python 3.9.13

あとはここにソースコードとrequirements.txtを参照してpip installを行うことで別のバージョンでプログラムを実行することができますね。

まとめ

  • venvはバージョン3.3で追加されたベースPython上に軽量のPython仮想環境を作成するためのモジュール
  • プロジェクトごとに異なるバージョンやパッケージを使用することができる
  • 依存関係による影響を考慮する必要なく、他のプロジェクトを立ち上げたりすることが可能

ご閲覧ありがとうございました。

この記事がお役に立てば【 いいね 】のご協力をお願いいたします!
4
読み込み中...
4 票, 平均: 1.00 / 14
2,862
X facebook はてなブックマーク pocket
[2024.6.30 CentOS support ended] CentOS server migration solution

[2024.6.30 CentOS support ended] CentOS server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

The person who wrote this article

About the author

inusuki

Joined Beyond as a new graduate in April 2021.
Maybe it's because I have a problem with my output, but sometimes I say strange things.