Opensourcetechブログ

OpensourcetechによるNGINX/Kubernetes/Zabbix/Neo4j/Linuxなどオープンソース技術に関するブログです。

Pythonの仮想環境について


LinuCエヴァンジェリストの鯨井貴博@opensourcetechです。

Pythonの仮想環境に関するメモです。

ローカルPCなどにおいて、様々なプロジェクトに対応するため異なるライブラリバージョンを用いた複数の環境を用意する場合などに利用します。


操作は、Windows Powershellで実施しています。

すでにPython3がインストールされている状態です。

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

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

PS C:\Users\pyuser> python -v
import _frozen_importlib # frozen
import _imp # builtin
import '_thread' # <class '_frozen_importlib.BuiltinImporter'>
import '_warnings' # <class '_frozen_importlib.BuiltinImporter'>
import '_weakref' # <class '_frozen_importlib.BuiltinImporter'>
import '_frozen_importlib_external' # <class '_frozen_importlib.FrozenImporter'>
import '_io' # <class '_frozen_importlib.BuiltinImporter'>
import 'marshal' # <class '_frozen_importlib.BuiltinImporter'>
import 'nt' # <class '_frozen_importlib.BuiltinImporter'>
import _thread # previously loaded ('_thread')
import '_thread' # <class '_frozen_importlib.BuiltinImporter'>
import _weakref # previously loaded ('_weakref')
import '_weakref' # <class '_frozen_importlib.BuiltinImporter'>
import 'winreg' # <class '_frozen_importlib.BuiltinImporter'>
# installing zipimport hook
import 'time' # <class '_frozen_importlib.BuiltinImporter'>
import 'zipimport' # <class '_frozen_importlib.FrozenImporter'>
# installed zipimport hook
# C:\Python38\lib\encodings\__pycache__\__init__.cpython-38.pyc matches C:\Python38\lib\encodings\__init__.py
# code object from 'C:\\Python38\\lib\\encodings\\__pycache__\\__init__.cpython-38.pyc'
# C:\Python38\lib\__pycache__\codecs.cpython-38.pyc matches C:\Python38\lib\codecs.py
# code object from 'C:\\Python38\\lib\\__pycache__\\codecs.cpython-38.pyc'
import '_codecs' # <class '_frozen_importlib.BuiltinImporter'>
import 'codecs' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E680A00>
# C:\Python38\lib\encodings\__pycache__\aliases.cpython-38.pyc matches C:\Python38\lib\encodings\aliases.py
# code object from 'C:\\Python38\\lib\\encodings\\__pycache__\\aliases.cpython-38.pyc'
import 'encodings.aliases' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E77FFD0>
import 'encodings' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E6807F0>
# C:\Python38\lib\encodings\__pycache__\utf_8.cpython-38.pyc matches C:\Python38\lib\encodings\utf_8.py
# code object from 'C:\\Python38\\lib\\encodings\\__pycache__\\utf_8.cpython-38.pyc'
import 'encodings.utf_8' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E680880>
# C:\Python38\lib\encodings\__pycache__\cp932.cpython-38.pyc matches C:\Python38\lib\encodings\cp932.py
# code object from 'C:\\Python38\\lib\\encodings\\__pycache__\\cp932.cpython-38.pyc'
import '_codecs_jp' # <class '_frozen_importlib.BuiltinImporter'>
import '_multibytecodec' # <class '_frozen_importlib.BuiltinImporter'>
import 'encodings.cp932' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E783100>
import '_signal' # <class '_frozen_importlib.BuiltinImporter'>
# C:\Python38\lib\encodings\__pycache__\latin_1.cpython-38.pyc matches C:\Python38\lib\encodings\latin_1.py
# code object from 'C:\\Python38\\lib\\encodings\\__pycache__\\latin_1.cpython-38.pyc'
import 'encodings.latin_1' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E7831C0>
# C:\Python38\lib\__pycache__\io.cpython-38.pyc matches C:\Python38\lib\io.py
# code object from 'C:\\Python38\\lib\\__pycache__\\io.cpython-38.pyc'
# C:\Python38\lib\__pycache__\abc.cpython-38.pyc matches C:\Python38\lib\abc.py
# code object from 'C:\\Python38\\lib\\__pycache__\\abc.cpython-38.pyc'
import '_abc' # <class '_frozen_importlib.BuiltinImporter'>
import 'abc' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E7839D0>
import 'io' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E7836D0>
# C:\Python38\lib\__pycache__\site.cpython-38.pyc matches C:\Python38\lib\site.py
# code object from 'C:\\Python38\\lib\\__pycache__\\site.cpython-38.pyc'
# C:\Python38\lib\__pycache__\os.cpython-38.pyc matches C:\Python38\lib\os.py
# code object from 'C:\\Python38\\lib\\__pycache__\\os.cpython-38.pyc'
# C:\Python38\lib\__pycache__\stat.cpython-38.pyc matches C:\Python38\lib\stat.py
# code object from 'C:\\Python38\\lib\\__pycache__\\stat.cpython-38.pyc'
import '_stat' # <class '_frozen_importlib.BuiltinImporter'>
import 'stat' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E7BE430>
# C:\Python38\lib\__pycache__\ntpath.cpython-38.pyc matches C:\Python38\lib\ntpath.py
# code object from 'C:\\Python38\\lib\\__pycache__\\ntpath.cpython-38.pyc'
# C:\Python38\lib\__pycache__\genericpath.cpython-38.pyc matches C:\Python38\lib\genericpath.py
# code object from 'C:\\Python38\\lib\\__pycache__\\genericpath.cpython-38.pyc'
import 'genericpath' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E7CB190>
import 'ntpath' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E7BE4C0>
# C:\Python38\lib\__pycache__\_collections_abc.cpython-38.pyc matches C:\Python38\lib\_collections_abc.py
# code object from 'C:\\Python38\\lib\\__pycache__\\_collections_abc.cpython-38.pyc'
import '_collections_abc' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E7BEFA0>
import 'os' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E7A3AF0>
# C:\Python38\lib\__pycache__\_sitebuiltins.cpython-38.pyc matches C:\Python38\lib\_sitebuiltins.py
# code object from 'C:\\Python38\\lib\\__pycache__\\_sitebuiltins.cpython-38.pyc'
import '_sitebuiltins' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E7B5640>
# C:\Python38\lib\__pycache__\_bootlocale.cpython-38.pyc matches C:\Python38\lib\_bootlocale.py
# code object from 'C:\\Python38\\lib\\__pycache__\\_bootlocale.cpython-38.pyc'
import '_locale' # <class '_frozen_importlib.BuiltinImporter'>
import '_bootlocale' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E7CBCD0>
# C:\Python38\lib\__pycache__\types.cpython-38.pyc matches C:\Python38\lib\types.py
# code object from 'C:\\Python38\\lib\\__pycache__\\types.cpython-38.pyc'
import 'types' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E7E33D0>
# C:\Python38\lib\importlib\__pycache__\__init__.cpython-38.pyc matches C:\Python38\lib\importlib\__init__.py
# code object from 'C:\\Python38\\lib\\importlib\\__pycache__\\__init__.cpython-38.pyc'
# C:\Python38\lib\__pycache__\warnings.cpython-38.pyc matches C:\Python38\lib\warnings.py
# code object from 'C:\\Python38\\lib\\__pycache__\\warnings.cpython-38.pyc'
import 'warnings' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E8AB100>
import 'importlib' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E892D90>
# C:\Python38\lib\importlib\__pycache__\util.cpython-38.pyc matches C:\Python38\lib\importlib\util.py
# code object from 'C:\\Python38\\lib\\importlib\\__pycache__\\util.cpython-38.pyc'
# C:\Python38\lib\importlib\__pycache__\abc.cpython-38.pyc matches C:\Python38\lib\importlib\abc.py
# code object from 'C:\\Python38\\lib\\importlib\\__pycache__\\abc.cpython-38.pyc'
# C:\Python38\lib\importlib\__pycache__\machinery.cpython-38.pyc matches C:\Python38\lib\importlib\machinery.py
# code object from 'C:\\Python38\\lib\\importlib\\__pycache__\\machinery.cpython-38.pyc'
import 'importlib.machinery' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E8BF940>
import 'importlib.abc' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E8ABC40>
# C:\Python38\lib\__pycache__\contextlib.cpython-38.pyc matches C:\Python38\lib\contextlib.py
# code object from 'C:\\Python38\\lib\\__pycache__\\contextlib.cpython-38.pyc'
# C:\Python38\lib\collections\__pycache__\__init__.cpython-38.pyc matches C:\Python38\lib\collections\__init__.py
# code object from 'C:\\Python38\\lib\\collections\\__pycache__\\__init__.cpython-38.pyc'
# C:\Python38\lib\__pycache__\operator.cpython-38.pyc matches C:\Python38\lib\operator.py
# code object from 'C:\\Python38\\lib\\__pycache__\\operator.cpython-38.pyc'
import '_operator' # <class '_frozen_importlib.BuiltinImporter'>
import 'operator' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E8ECE50>
# C:\Python38\lib\__pycache__\keyword.cpython-38.pyc matches C:\Python38\lib\keyword.py
# code object from 'C:\\Python38\\lib\\__pycache__\\keyword.cpython-38.pyc'
import 'keyword' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E8ECF40>
# C:\Python38\lib\__pycache__\heapq.cpython-38.pyc matches C:\Python38\lib\heapq.py
# code object from 'C:\\Python38\\lib\\__pycache__\\heapq.cpython-38.pyc'
import '_heapq' # <class '_frozen_importlib.BuiltinImporter'>
import 'heapq' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E8ECFD0>
import 'itertools' # <class '_frozen_importlib.BuiltinImporter'>
# C:\Python38\lib\__pycache__\reprlib.cpython-38.pyc matches C:\Python38\lib\reprlib.py
# code object from 'C:\\Python38\\lib\\__pycache__\\reprlib.cpython-38.pyc'
import 'reprlib' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E8F5490>
import '_collections' # <class '_frozen_importlib.BuiltinImporter'>
import 'collections' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E8CDFD0>
# C:\Python38\lib\__pycache__\functools.cpython-38.pyc matches C:\Python38\lib\functools.py
# code object from 'C:\\Python38\\lib\\__pycache__\\functools.cpython-38.pyc'
import '_functools' # <class '_frozen_importlib.BuiltinImporter'>
import 'functools' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E8D9C10>
import 'contextlib' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E8ABFA0>
import 'importlib.util' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E892F40>
# possible namespace for C:\Python38\lib\site-packages\pywinusb
# possible namespace for C:\Python38\lib\site-packages\sphinxcontrib
# possible namespace for C:\Python38\lib\site-packages\sphinxcontrib
# destroy sphinxcontrib
# possible namespace for C:\Python38\lib\site-packages\sphinxcontrib
# destroy sphinxcontrib
# possible namespace for C:\Python38\lib\site-packages\sphinxcontrib
# destroy sphinxcontrib
# possible namespace for C:\Python38\lib\site-packages\sphinxcontrib
# destroy sphinxcontrib
# possible namespace for C:\Python38\lib\site-packages\sphinxcontrib
# destroy sphinxcontrib
# possible namespace for C:\Python38\lib\site-packages\sphinxcontrib
# destroy sphinxcontrib
import 'site' # <_frozen_importlib_external.SourceFileLoader object at 0x0000013B4E7A33D0>
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
import 'atexit' # <class '_frozen_importlib.BuiltinImporter'>
>>> exit
Use exit() or Ctrl-Z plus Return to exit
>>> exit()



まず、実行するPythonスクリプトに実行権限を与えるため、Set-Executionpolicy RemoteSigned -Scope CurrentUserを実施します。
その後、python -m venv 仮想環境名とすると、カレントディレクトリ配下に仮想環境ディレクトリが作成されます。

PS C:\Users\pyuser> Set-Executionpolicy RemoteSigned -Scope CurrentUser

実行ポリシーの変更
実行ポリシーは、信頼されていないスクリプトからの保護に役立ちます。実行ポリシーを変更すると、about_Execution_Policies
のヘルプ トピック (https://go.microsoft.com/fwlink/?LinkID=135170)
で説明されているセキュリティ上の危険にさらされる可能性があります。実行ポリシーを変更しますか?
[Y] はい(Y)  [A] すべて続行(A)  [N] いいえ(N)  [L] すべて無視(L)  [S] 中断(S)  [?] ヘルプ (既定値は "N"): y
PS C:\Users\pyuser> python -m venv venv1
PS C:\Users\pyuser> dir

    ディレクトリ: C:\Users\pyuser

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2020/11/14      4:21                venv1

PS C:\Users\pyuser> dir venv1

    ディレクトリ: C:\Users\pyuser\venv1

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2020/11/14      4:21                Include
d-----       2020/11/14      4:21                Lib
d-----       2020/11/14      4:21                Scripts
-a----       2020/11/14      4:21             75 pyvenv.cfg



作成した仮想環境に入るには、作成されたディレクトリ(Scripts)内にあるActivate.ps1を実行します。

PS C:\Users\pyuser> cd .\venv1\
PS C:\Users\pyuser\venv1> cd .\Scripts\
PS C:\Users\pyuser\venv1\Scripts> dir


    ディレクトリ: C:\Users\pyuser\venv1\Scripts


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2020/11/14      4:21           2282 activate
-a----       2020/11/14      4:21            958 activate.bat
-a----       2020/11/14      4:21          18147 Activate.ps1
-a----       2020/11/14      4:21            368 deactivate.bat
-a----       2020/11/14      4:21         103290 easy_install-3.8.exe
-a----       2020/11/14      4:21         103290 easy_install.exe
-a----       2020/11/14      4:21         103272 pip.exe
-a----       2020/11/14      4:21         103272 pip3.8.exe
-a----       2020/11/14      4:21         103272 pip3.exe
-a----       2020/11/14      4:21         524872 python.exe
-a----       2020/11/14      4:21         523848 pythonw.exe


PS C:\Users\pyuser\venv1\Scripts> .\Activate.ps1
(venv1) PS C:\Users\pyuser\venv1\Scripts> python -v
import _frozen_importlib # frozen
import _imp # builtin
import '_thread' # <class '_frozen_importlib.BuiltinImporter'>
import '_warnings' # <class '_frozen_importlib.BuiltinImporter'>
import '_weakref' # <class '_frozen_importlib.BuiltinImporter'>
import '_frozen_importlib_external' # <class '_frozen_importlib.FrozenImporter'>
import '_io' # <class '_frozen_importlib.BuiltinImporter'>
import 'marshal' # <class '_frozen_importlib.BuiltinImporter'>
import 'nt' # <class '_frozen_importlib.BuiltinImporter'>
import _thread # previously loaded ('_thread')
import '_thread' # <class '_frozen_importlib.BuiltinImporter'>
import _weakref # previously loaded ('_weakref')
import '_weakref' # <class '_frozen_importlib.BuiltinImporter'>
import 'winreg' # <class '_frozen_importlib.BuiltinImporter'>
# installing zipimport hook
import 'time' # <class '_frozen_importlib.BuiltinImporter'>
import 'zipimport' # <class '_frozen_importlib.FrozenImporter'>
# installed zipimport hook
# C:\Python38\lib\encodings\__pycache__\__init__.cpython-38.pyc matches C:\Python38\lib\encodings\__init__.py
# code object from 'C:\\Python38\\lib\\encodings\\__pycache__\\__init__.cpython-38.pyc'
# C:\Python38\lib\__pycache__\codecs.cpython-38.pyc matches C:\Python38\lib\codecs.py
# code object from 'C:\\Python38\\lib\\__pycache__\\codecs.cpython-38.pyc'
import '_codecs' # <class '_frozen_importlib.BuiltinImporter'>
import 'codecs' # <_frozen_importlib_external.SourceFileLoader object at 0x000001B9C5BC0A00>
# C:\Python38\lib\encodings\__pycache__\aliases.cpython-38.pyc matches C:\Python38\lib\encodings\aliases.py
# code object from 'C:\\Python38\\lib\\encodings\\__pycache__\\aliases.cpython-38.pyc'
import 'encodings.aliases' # <_frozen_importlib_external.SourceFileLoader object at 0x000001B9C5CBFFD0>
import 'encodings' # <_frozen_importlib_external.SourceFileLoader object at 0x000001B9C5BC07F0>
# C:\Python38\lib\encodings\__pycache__\utf_8.cpython-38.pyc matches C:\Python38\lib\encodings\utf_8.py
# code object from 'C:\\Python38\\lib\\encodings\\__pycache__\\utf_8.cpython-38.pyc'
import 'encodings.utf_8' # <_frozen_importlib_external.SourceFileLoader object at 0x000001B9C5BC0880>
# C:\Python38\lib\encodings\__pycache__\cp932.cpython-38.pyc matches C:\Python38\lib\encodings\cp932.py
# code object from 'C:\\Python38\\lib\\encodings\\__pycache__\\cp932.cpython-38.pyc'
import '_codecs_jp' # <class '_frozen_importlib.BuiltinImporter'>
import '_multibytecodec' # <class '_frozen_importlib.BuiltinImporter'>
import 'encodings.cp932' # <_frozen_importlib_external.SourceFileLoader object at 0x000001B9C5CC3100>
import '_signal' # <class '_frozen_importlib.BuiltinImporter'>
# C:\Python38\lib\encodings\__pycache__\latin_1.cpython-38.pyc matches C:\Python38\lib\encodings\latin_1.py
# code object from 'C:\\Python38\\lib\\encodings\\__pycache__\\latin_1.cpython-38.pyc'
import 'encodings.latin_1' # <_frozen_importlib_external.SourceFileLoader object at 0x000001B9C5CC31C0>
# C:\Python38\lib\__pycache__\io.cpython-38.pyc matches C:\Python38\lib\io.py
# code object from 'C:\\Python38\\lib\\__pycache__\\io.cpython-38.pyc'
# C:\Python38\lib\__pycache__\abc.cpython-38.pyc matches C:\Python38\lib\abc.py
# code object from 'C:\\Python38\\lib\\__pycache__\\abc.cpython-38.pyc'
import '_abc' # <class '_frozen_importlib.BuiltinImporter'>
import 'abc' # <_frozen_importlib_external.SourceFileLoader object at 0x000001B9C5CC39D0>
import 'io' # <_frozen_importlib_external.SourceFileLoader object at 0x000001B9C5CC36D0>
# C:\Python38\lib\__pycache__\site.cpython-38.pyc matches C:\Python38\lib\site.py
# code object from 'C:\\Python38\\lib\\__pycache__\\site.cpython-38.pyc'
# C:\Python38\lib\__pycache__\os.cpython-38.pyc matches C:\Python38\lib\os.py
# code object from 'C:\\Python38\\lib\\__pycache__\\os.cpython-38.pyc'
# C:\Python38\lib\__pycache__\stat.cpython-38.pyc matches C:\Python38\lib\stat.py
# code object from 'C:\\Python38\\lib\\__pycache__\\stat.cpython-38.pyc'
import '_stat' # <class '_frozen_importlib.BuiltinImporter'>
import 'stat' # <_frozen_importlib_external.SourceFileLoader object at 0x000001B9C5CFE430>
# C:\Python38\lib\__pycache__\ntpath.cpython-38.pyc matches C:\Python38\lib\ntpath.py
# code object from 'C:\\Python38\\lib\\__pycache__\\ntpath.cpython-38.pyc'
# C:\Python38\lib\__pycache__\genericpath.cpython-38.pyc matches C:\Python38\lib\genericpath.py
# code object from 'C:\\Python38\\lib\\__pycache__\\genericpath.cpython-38.pyc'
import 'genericpath' # <_frozen_importlib_external.SourceFileLoader object at 0x000001B9C5D0C190>
import 'ntpath' # <_frozen_importlib_external.SourceFileLoader object at 0x000001B9C5CFE4C0>
# C:\Python38\lib\__pycache__\_collections_abc.cpython-38.pyc matches C:\Python38\lib\_collections_abc.py
# code object from 'C:\\Python38\\lib\\__pycache__\\_collections_abc.cpython-38.pyc'
import '_collections_abc' # <_frozen_importlib_external.SourceFileLoader object at 0x000001B9C5CFEFA0>
import 'os' # <_frozen_importlib_external.SourceFileLoader object at 0x000001B9C5CE3AF0>
# C:\Python38\lib\__pycache__\_sitebuiltins.cpython-38.pyc matches C:\Python38\lib\_sitebuiltins.py
# code object from 'C:\\Python38\\lib\\__pycache__\\_sitebuiltins.cpython-38.pyc'
import '_sitebuiltins' # <_frozen_importlib_external.SourceFileLoader object at 0x000001B9C5CF5640>
import 'site' # <_frozen_importlib_external.SourceFileLoader object at 0x000001B9C5CE33D0>
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
import 'atexit' # <class '_frozen_importlib.BuiltinImporter'>
>>> exit()



仮想環境内では、通常通りPythonを使うことが出来ます。
以下では、numpy・pandas・matplotlib・scikit-learnをpipでインストールしています。

(venv1) PS C:\Users\pyuser\venv1\Scripts> pip list
Package    Version
---------- -------
pip        19.2.3
setuptools 41.2.0
WARNING: You are using pip version 19.2.3, however version 20.2.4 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
(venv1) PS C:\Users\pyuser\venv1\Scripts> pip install numpy
Collecting numpy
  Downloading https://files.pythonhosted.org/packages/40/db/5060f18b0116f00ee73f8365efc9c95bd5496946290b0e7c97b6ee89dffe/numpy-1.19.4-cp38-cp38-win_amd64.whl (13.0MB)
     |████████████████████████████████| 13.0MB 6.4MB/s
Installing collected packages: numpy
Successfully installed numpy-1.19.4
WARNING: You are using pip version 19.2.3, however version 20.2.4 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
(venv1) PS C:\Users\pyuser\venv1\Scripts> pip install pandas
Collecting pandas
  Downloading https://files.pythonhosted.org/packages/07/c5/b331415af20d49098250d923440e710e31e42d84ca0d071d5f7f29e95b2c/pandas-1.1.4-cp38-cp38-win_amd64.whl (8.9MB)
     |████████████████████████████████| 8.9MB ...
Collecting python-dateutil>=2.7.3 (from pandas)
  Using cached https://files.pythonhosted.org/packages/d4/70/d60450c3dd48ef87586924207ae8907090de0b306af2bce5d134d78615cb/python_dateutil-2.8.1-py2.py3-none-any.whl
Requirement already satisfied: numpy>=1.15.4 in c:\users\pyuser\venv1\lib\site-packages (from pandas) (1.19.4)
Collecting pytz>=2017.2 (from pandas)
  Downloading https://files.pythonhosted.org/packages/12/f8/ff09af6ff61a3efaad5f61ba5facdf17e7722c4393f7d8a66674d2dbd29f/pytz-2020.4-py2.py3-none-any.whl (509kB)
     |████████████████████████████████| 512kB ...
Collecting six>=1.5 (from python-dateutil>=2.7.3->pandas)
  Downloading https://files.pythonhosted.org/packages/ee/ff/48bde5c0f013094d729fe4b0316ba2a24774b3ff1c52d924a8a4cb04078a/six-1.15.0-py2.py3-none-any.whl
Installing collected packages: six, python-dateutil, pytz, pandas
Successfully installed pandas-1.1.4 python-dateutil-2.8.1 pytz-2020.4 six-1.15.0
WARNING: You are using pip version 19.2.3, however version 20.2.4 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
(venv1) PS C:\Users\pyuser\venv1\Scripts> pip install matplotlib
Collecting matplotlib
  Downloading https://files.pythonhosted.org/packages/42/7b/7c05e601af729d8289876aa00f530b27aea9b11768a2846bc941f9d058ef/matplotlib-3.3.3-cp38-cp38-win_amd64.whl (8.5MB)
     |████████████████████████████████| 8.5MB 6.4MB/s
Collecting pillow>=6.2.0 (from matplotlib)
  Downloading https://files.pythonhosted.org/packages/d2/2c/9ac71068585b3d528349be6617ed963068bfcba3da21018cdb0d2b19f07c/Pillow-8.0.1-cp38-cp38-win_amd64.whl (2.1MB)
     |████████████████████████████████| 2.1MB 6.4MB/s
Collecting cycler>=0.10 (from matplotlib)
  Downloading https://files.pythonhosted.org/packages/f7/d2/e07d3ebb2bd7af696440ce7e754c59dd546ffe1bbe732c8ab68b9c834e61/cycler-0.10.0-py2.py3-none-any.whl
Collecting kiwisolver>=1.0.1 (from matplotlib)
  Downloading https://files.pythonhosted.org/packages/a8/be/9a8f1a59c94f99544366fd3680373ccd6cf6dcd504e02d504486e9f0943b/kiwisolver-1.3.1-cp38-cp38-win_amd64.whl (51kB)
     |████████████████████████████████| 61kB 3.8MB/s
Requirement already satisfied: numpy>=1.15 in c:\users\pyuser\venv1\lib\site-packages (from matplotlib) (1.19.4)
Requirement already satisfied: python-dateutil>=2.1 in c:\users\pyuser\venv1\lib\site-packages (from matplotlib) (2.8.1)
Collecting pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.3 (from matplotlib)
  Downloading https://files.pythonhosted.org/packages/8a/bb/488841f56197b13700afd5658fc279a2025a39e22449b7cf29864669b15d/pyparsing-2.4.7-py2.py3-none-any.whl (67kB)
     |████████████████████████████████| 71kB ...
Requirement already satisfied: six in c:\users\pyuser\venv1\lib\site-packages (from cycler>=0.10->matplotlib) (1.15.0)
Installing collected packages: pillow, cycler, kiwisolver, pyparsing, matplotlib
Successfully installed cycler-0.10.0 kiwisolver-1.3.1 matplotlib-3.3.3 pillow-8.0.1 pyparsing-2.4.7
WARNING: You are using pip version 19.2.3, however version 20.2.4 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
(venv1) PS C:\Users\pyuser\venv1\Scripts> pip install scikit-learn
Collecting scikit-learn
  Downloading https://files.pythonhosted.org/packages/d6/04/ea17391926a03db3dd7c41d2090b7f4394b3be4be55fef49d6a5a6f3d796/scikit_learn-0.23.2-cp38-cp38-win_amd64.whl (6.8MB)
     |████████████████████████████████| 6.8MB 3.3MB/s
Collecting scipy>=0.19.1 (from scikit-learn)
  Downloading https://files.pythonhosted.org/packages/d1/79/3d00d6d7f386741b6f4e1cf1fadc2d0b50bd11564ac4f25163f87bfd3810/scipy-1.5.4-cp38-cp38-win_amd64.whl (31.4MB)
     |████████████████████████████████| 31.4MB 6.4MB/s
Requirement already satisfied: numpy>=1.13.3 in c:\users\pyuser\venv1\lib\site-packages (from scikit-learn) (1.19.4)
Collecting threadpoolctl>=2.0.0 (from scikit-learn)
  Downloading https://files.pythonhosted.org/packages/f7/12/ec3f2e203afa394a149911729357aa48affc59c20e2c1c8297a60f33f133/threadpoolctl-2.1.0-py3-none-any.whl
Collecting joblib>=0.11 (from scikit-learn)
  Downloading https://files.pythonhosted.org/packages/fc/c9/f58220ac44a1592f79a343caba12f6837f9e0c04c196176a3d66338e1ea8/joblib-0.17.0-py3-none-any.whl (301kB)
     |████████████████████████████████| 307kB ...
Installing collected packages: scipy, threadpoolctl, joblib, scikit-learn
Successfully installed joblib-0.17.0 scikit-learn-0.23.2 scipy-1.5.4 threadpoolctl-2.1.0
WARNING: You are using pip version 19.2.3, however version 20.2.4 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
(venv1) PS C:\Users\pyuser\venv1\Scripts> pip list
Package         Version
--------------- -------
cycler          0.10.0
joblib          0.17.0
kiwisolver      1.3.1
matplotlib      3.3.3
numpy           1.19.4
pandas          1.1.4
Pillow          8.0.1
pip             19.2.3
pyparsing       2.4.7
python-dateutil 2.8.1
pytz            2020.4
scikit-learn    0.23.2
scipy           1.5.4
setuptools      41.2.0
six             1.15.0
threadpoolctl   2.1.0
WARNING: You are using pip version 19.2.3, however version 20.2.4 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
(venv1) PS C:\Users\pyuser\venv1\Scripts> deactivate



また、pipとstuptoolsはより最新バージョンがあるので、アップデートします。

(venv1) PS C:\Users\pyuser\venv1\Scripts> pip list -o
Package    Version Latest Type
---------- ------- ------ -----
pip        19.2.3  20.2.4 wheel
setuptools 41.2.0  50.3.2 wheel
WARNING: You are using pip version 19.2.3, however version 20.2.4 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
(venv1) PS C:\Users\pyuser\venv1\Scripts>
(venv1) PS C:\Users\pyuser\venv1\Scripts> pip install -U pip setuptools
Collecting pip
  Downloading https://files.pythonhosted.org/packages/cb/28/91f26bd088ce8e22169032100d4260614fc3da435025ff389ef1d396a433/pip-20.2.4-py2.py3-none-any.whl (1.5MB)
     |████████████████████████████████| 1.5MB 3.2MB/s
Collecting setuptools
  Downloading https://files.pythonhosted.org/packages/6d/38/c21ef5034684ffc0412deefbb07d66678332290c14bb5269c85145fbd55e/setuptools-50.3.2-py3-none-any.whl (785kB)
     |████████████████████████████████| 788kB 6.4MB/s
Installing collected packages: pip, setuptools
  Found existing installation: pip 19.2.3
    Uninstalling pip-19.2.3:
      Successfully uninstalled pip-19.2.3
Collecting setuptools
  Using cached setuptools-50.3.2-py3-none-any.whl (785 kB)
Installing collected packages: setuptools
  Attempting uninstall: setuptools
    Found existing installation: setuptools 41.2.0
    Uninstalling setuptools-41.2.0:
      Successfully uninstalled setuptools-41.2.0
Successfully installed setuptools-50.3.2
(venv1) PS C:\Users\pyuser\venv1\Scripts> pip list
Package         Version
--------------- -------
cycler          0.10.0
joblib          0.17.0
kiwisolver      1.3.1
matplotlib      3.3.3
numpy           1.19.4
pandas          1.1.4
Pillow          8.0.1
pip             20.2.4
pyparsing       2.4.7
python-dateutil 2.8.1
pytz            2020.4
scikit-learn    0.23.2
scipy           1.5.4
setuptools      50.3.2
six             1.15.0
threadpoolctl   2.1.0



このように仮想環境を操作することが出来ますが、
さらに便利なのはこの環境で利用されているパッケージ、およびそのバージョン情報を共有して、簡単に仮想環境の複製が可能となります。

これは、業務で環境構築する際などには大変重宝しそうですね!

pip freezeでパッケージ一覧が出力されるので、それをテキストファイル(ここでは、requirements.txt)に保存します。
そして、現在の仮想環境をdeactivateで抜けます。

(venv1) PS C:\Users\pyuser> pip freeze
cycler==0.10.0
joblib==0.17.0
kiwisolver==1.3.1
matplotlib==3.3.3
numpy==1.19.4
pandas==1.1.4
Pillow==8.0.1
pyparsing==2.4.7
python-dateutil==2.8.1
pytz==2020.4
scikit-learn==0.23.2
scipy==1.5.4
six==1.15.0
threadpoolctl==2.1.0
(venv1) PS C:\Users\pyuser\venv1\Scripts> pip freeze > requirements.txt
(venv1) PS C:\Users\pyuser\venv1\Scripts> cat .\requirements.txt
cycler==0.10.0
joblib==0.17.0
kiwisolver==1.3.1
matplotlib==3.3.3
numpy==1.19.4
pandas==1.1.4
Pillow==8.0.1
pyparsing==2.4.7
python-dateutil==2.8.1
pytz==2020.4
scikit-learn==0.23.2
scipy==1.5.4
six==1.15.0
threadpoolctl==2.1.0
(venv1) PS C:\Users\pyuser\venv1\Scripts> deactivate



続いて、別の仮想環境に入り、pip installに-rオプションを付け、作成したテキストファイル(requirements.txt)を指定すればOK。

PS C:\Users\pyuser\venv1\Scripts> cd ../..
PS C:\Users\pyuser> python -m venv venv2
PS C:\Users\pyuser> .\venv2\Scripts\Activate.ps1
(venv2) PS C:\Users\pyuser> pip freeze
(venv2) PS C:\Users\pyuser> pip list
Package    Version
---------- -------
pip        19.2.3
setuptools 41.2.0
WARNING: You are using pip version 19.2.3, however version 20.2.4 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
(venv2) PS C:\Users\pyuser> pip install -r .\venv1\Scripts\requirements.txt
Collecting cycler==0.10.0 (from -r .\venv1\Scripts\requirements.txt (line 1))
  Using cached https://files.pythonhosted.org/packages/f7/d2/e07d3ebb2bd7af696440ce7e754c59dd546ffe1bbe732c8ab68b9c834e61/cycler-0.10.0-py2.py3-none-any.whl
Collecting joblib==0.17.0 (from -r .\venv1\Scripts\requirements.txt (line 2))
  Using cached https://files.pythonhosted.org/packages/fc/c9/f58220ac44a1592f79a343caba12f6837f9e0c04c196176a3d66338e1ea8/joblib-0.17.0-py3-none-any.whl
Collecting kiwisolver==1.3.1 (from -r .\venv1\Scripts\requirements.txt (line 3))
  Using cached https://files.pythonhosted.org/packages/a8/be/9a8f1a59c94f99544366fd3680373ccd6cf6dcd504e02d504486e9f0943b/kiwisolver-1.3.1-cp38-cp38-win_amd64.whl
Collecting matplotlib==3.3.3 (from -r .\venv1\Scripts\requirements.txt (line 4))
  Using cached https://files.pythonhosted.org/packages/42/7b/7c05e601af729d8289876aa00f530b27aea9b11768a2846bc941f9d058ef/matplotlib-3.3.3-cp38-cp38-win_amd64.whl
Collecting numpy==1.19.4 (from -r .\venv1\Scripts\requirements.txt (line 5))
  Using cached https://files.pythonhosted.org/packages/40/db/5060f18b0116f00ee73f8365efc9c95bd5496946290b0e7c97b6ee89dffe/numpy-1.19.4-cp38-cp38-win_amd64.whl
Collecting pandas==1.1.4 (from -r .\venv1\Scripts\requirements.txt (line 6))
  Using cached https://files.pythonhosted.org/packages/07/c5/b331415af20d49098250d923440e710e31e42d84ca0d071d5f7f29e95b2c/pandas-1.1.4-cp38-cp38-win_amd64.whl
Collecting Pillow==8.0.1 (from -r .\venv1\Scripts\requirements.txt (line 7))
  Using cached https://files.pythonhosted.org/packages/d2/2c/9ac71068585b3d528349be6617ed963068bfcba3da21018cdb0d2b19f07c/Pillow-8.0.1-cp38-cp38-win_amd64.whl
Collecting pyparsing==2.4.7 (from -r .\venv1\Scripts\requirements.txt (line 8))
  Using cached https://files.pythonhosted.org/packages/8a/bb/488841f56197b13700afd5658fc279a2025a39e22449b7cf29864669b15d/pyparsing-2.4.7-py2.py3-none-any.whl
Collecting python-dateutil==2.8.1 (from -r .\venv1\Scripts\requirements.txt (line 9))
  Using cached https://files.pythonhosted.org/packages/d4/70/d60450c3dd48ef87586924207ae8907090de0b306af2bce5d134d78615cb/python_dateutil-2.8.1-py2.py3-none-any.whl
Collecting pytz==2020.4 (from -r .\venv1\Scripts\requirements.txt (line 10))
  Using cached https://files.pythonhosted.org/packages/12/f8/ff09af6ff61a3efaad5f61ba5facdf17e7722c4393f7d8a66674d2dbd29f/pytz-2020.4-py2.py3-none-any.whl
Collecting scikit-learn==0.23.2 (from -r .\venv1\Scripts\requirements.txt (line 11))
  Using cached https://files.pythonhosted.org/packages/d6/04/ea17391926a03db3dd7c41d2090b7f4394b3be4be55fef49d6a5a6f3d796/scikit_learn-0.23.2-cp38-cp38-win_amd64.whl
Collecting scipy==1.5.4 (from -r .\venv1\Scripts\requirements.txt (line 12))
  Using cached https://files.pythonhosted.org/packages/d1/79/3d00d6d7f386741b6f4e1cf1fadc2d0b50bd11564ac4f25163f87bfd3810/scipy-1.5.4-cp38-cp38-win_amd64.whl
Collecting six==1.15.0 (from -r .\venv1\Scripts\requirements.txt (line 13))
  Using cached https://files.pythonhosted.org/packages/ee/ff/48bde5c0f013094d729fe4b0316ba2a24774b3ff1c52d924a8a4cb04078a/six-1.15.0-py2.py3-none-any.whl
Collecting threadpoolctl==2.1.0 (from -r .\venv1\Scripts\requirements.txt (line 14))
  Using cached https://files.pythonhosted.org/packages/f7/12/ec3f2e203afa394a149911729357aa48affc59c20e2c1c8297a60f33f133/threadpoolctl-2.1.0-py3-none-any.whl
Installing collected packages: six, cycler, joblib, kiwisolver, pyparsing, numpy, python-dateutil, Pillow, matplotlib, pytz, pandas, threadpoolctl, scipy, scikit-learn
Successfully installed Pillow-8.0.1 cycler-0.10.0 joblib-0.17.0 kiwisolver-1.3.1 matplotlib-3.3.3 numpy-1.19.4 pandas-1.1.4 pyparsing-2.4.7 python-dateutil-2.8.1 pytz-2020.4 scikit-learn-0.23.2 scipy-1.5.4 six-1.15.0 threadpoolctl-2.1.0
WARNING: You are using pip version 19.2.3, however version 20.2.4 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
(venv2) PS C:\Users\pyuser> pip list
Package         Version
--------------- -------
cycler          0.10.0
joblib          0.17.0
kiwisolver      1.3.1
matplotlib      3.3.3
numpy           1.19.4
pandas          1.1.4
Pillow          8.0.1
pip             19.2.3
pyparsing       2.4.7
python-dateutil 2.8.1
pytz            2020.4
scikit-learn    0.23.2
scipy           1.5.4
setuptools      41.2.0
six             1.15.0
threadpoolctl   2.1.0
WARNING: You are using pip version 19.2.3, however version 20.2.4 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.



これは、便利ですね!!

なお、作成した仮想環境が不要になった場合、rmコマンドを使って仮想環境のディレクトリごと削除してやります。

(venv2) PS C:\Users\pyuser> deactivate
PS C:\Users\pyuser> rm -r -fo venv2


他のOSでも、Powershellの部分だけ省けば、ほぼそのままでよいかと思います。※未検証です。

Opensourcetech by Takahiro Kujirai