1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

virtualenv でプロジェクトごとに異なる Python バージョンを使う

Last updated at Posted at 2021-02-01

はじめに

venv/virtualenv などのような表記を見かけますが、
venv は virtualenv の省略表記ではありません。
それぞれの概要は以下です。

前提

mac で挙動確認を行いました。
https://www.python.org/downloads/ のインストーラーを用いて、
3.8 と 3.9 を install してあります。
python3 は 3.8 のバージョンを向いています。

$ ls /Library/Frameworks/Python.framework/Versions   
3.8/     3.9/     Current@
$ which python3
/Library/Frameworks/Python.framework/Versions/3.8/bin/python3

virtualenv で 3.8 と 3.9 の環境を作成する

virtualenv を install します。
virtualenv は 3.8 のディレクトリに install されました。

$ pip3 install --user virtualenv

$ which virtualenv
/Users/<user_name>/Library/Python/3.8/bin/virtualenv

まずは python3.9 の仮想環境を作ります。
(python3.9 の環境ができたことが分かります。)

$ virtualenv -p python3.9 venv39
$ tree -L 3 venv39
venv39
├── bin
│   ├── activate
│   ├── activate.csh
│   ├── activate.fish
│   ├── activate.ps1
│   ├── activate.xsh
│   ├── activate_this.py
│   ├── pip
│   ├── pip-3.9
│   ├── pip3
│   ├── pip3.9
│   ├── py.test
│   ├── pytest
│   ├── python -> /usr/local/bin/python3.9
│   ├── python3 -> python
│   ├── python3.9 -> python
│   ├── wheel
│   ├── wheel-3.9
│   ├── wheel3
│   └── wheel3.9
├── lib
│   └── python3.9
│       └── site-packages
└── pyvenv.cfg

$ . venv39/bin/activate
$ python3 --version    
Python 3.9.1

続いて python3.8 の仮想環境を作ります。
(python3.8 の環境ができたことが分かります。)

$ virtualenv -p python3.8 venv38

$ tree -L 3 venv38
venv38
├── bin
│   ├── activate
│   ├── activate.csh
│   ├── activate.fish
│   ├── activate.ps1
│   ├── activate.xsh
│   ├── activate_this.py
│   ├── pip
│   ├── pip-3.8
│   ├── pip3
│   ├── pip3.8
│   ├── python -> /Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8
│   ├── python3 -> python
│   ├── python3.8 -> python
│   ├── wheel
│   ├── wheel-3.8
│   ├── wheel3
│   └── wheel3.8
├── lib
│   └── python3.8
│       └── site-packages
└── pyvenv.cfg

$ . venv38/bin/activate
$ python3 --version
Python 3.8.6rc1

以上で異なる Python バージョンの環境を作成することができました。
お疲れさまでした。

参照したドキュメント

1
3
2

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
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?