Up 仮想環境 : venv (Python) 作成: 2021-04-17
更新: 2021-04-18


    Python 仮想環境を使用して、パッケージのインストール先をシステムから分離する。
      仮想環境) は,ホストシステムのセットアップに影響を及ぼさないための仕掛け。
      必要パッケージをこの中にインストールする。

  • システム要件
    • Python 3.5–3.8
    • pip 19.0 以降


  1. python, pip3 のバージョンを確認
      $ python3 --version Python 3.7.3
      $ pip -V
       または,
      $ pip3 --version pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)

  2. Python インタプリタを保管するディレクトリ venv ("virtual environment") を,カーレントディレクトリに作成:
      $ python -m venv --system-site-packages ./venv $ ls -la venv drwxr-xr-x 2 pi pi 4096 Apr 17 11:24 bin drwxr-xr-x 2 pi pi 4096 Apr 17 11:24 include drwxr-xr-x 3 pi pi 4096 Apr 17 11:24 lib -rw-r--r-- 1 pi pi 68 Apr 17 11:24 pyvenv.cfg drwxr-xr-x 3 pi pi 4096 Apr 17 11:24 share $ ls -la venv/bin -rw-r--r-- 1 pi pi 2189 Apr 17 11:24 activate -rw-r--r-- 1 pi pi 1245 Apr 17 11:24 activate.csh -rw-r--r-- 1 pi pi 2397 Apr 17 11:24 activate.fish -rwxr-xr-x 1 pi pi 241 Apr 17 11:24 easy_install -rwxr-xr-x 1 pi pi 241 Apr 17 11:24 easy_install-3.7 -rwxr-xr-x 1 pi pi 223 Apr 17 11:24 pip -rwxr-xr-x 1 pi pi 223 Apr 17 11:24 pip3 -rwxr-xr-x 1 pi pi 223 Apr 17 11:24 pip3.7 lrwxrwxrwx 1 pi pi 15 Apr 17 11:24 python -> /usr/bin/python lrwxrwxrwx 1 pi pi 6 Apr 17 11:24 python3 -> python $ ls -la venv/lib drwxr-xr-x 3 pi pi 4096 Apr 17 11:24 python3.7

  3. venv を「仮想環境」として有効化
    (→ プロンプトの先頭に "(venv)" が付くようになる)
      $ source ./venv/bin/activate (venv) $

  4. pip のアップデート
      (venv) $ pip --version pip 18.1 from /home/pi/venv/lib/python3.7/site-packages/pip (python 3.7) (venv) $ pip install --upgrade pip Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple Collecting pip Using cached https://files.pythonhosted.org/packages/fe/ef/ 60d7ba03b5c442309ef42e7d69959f73aacccd0d86008362a681c4698e83/ pip-21.0.1-py3-none-any.whl Installing collected packages: pip Found existing installation: pip 18.1 Uninstalling pip-18.1: Successfully uninstalled pip-18.1 Successfully installed pip-21.0.1 (venv) $ pip --version pip 21.0.1 from /home/pi/venv/lib/python3.7/site-packages/pip (python 3.7)

  5. numpy のアップデート
      (venv) $ pip show numpy Name: numpy Version: 1.16.2  ‥‥ (venv) $ pip install --upgrade numpy Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple Requirement already satisfied: numpy in /usr/lib/python3/dist-packages (1.16.2) Collecting numpy Downloading https://www.piwheels.org/simple/numpy/numpy-1.20.2-cp37-cp37m-linux_armv7l.whl (11.6 MB) |████████████████████████████████| 11.6 MB 16 kB/s Installing collected packages: numpy Attempting uninstall: numpy Found existing installation: numpy 1.16.2 Not uninstalling numpy at /usr/lib/python3/dist-packages, outside environment /home/pi/venv Can't uninstall 'numpy'. No files were found to uninstall. Successfully installed numpy-1.20.2 (venv) $ pip show numpy Name: numpy Version: 1.20.2  ‥‥


      $ pip list Package Version ---------------------------------- ----------- appdirs 1.4.3 asn1crypto 0.24.0 astroid 2.1.0 asttokens 1.1.13 automationhat 0.2.0 beautifulsoup4 4.7.1 blessings 1.6 blinker 1.4 blinkt 0.1.2 buttonshim 0.0.2 Cap1xxx 0.1.3 certifi 2018.8.24 cffi 1.14.5 chardet 3.0.4 Click 7.0 colorama 0.3.7 colorzero 1.1 cookies 2.2.1 cryptography 2.6.1 cupshelpers 1.0 curtsies 0.2.12 cycler 0.10.0 decorator 4.3.0 Dexter-AutoDetection-and-I2C-Mutex 0.0.0 DI-Sensors 1.0.0 docutils 0.14 drumhat 0.1.0 entrypoints 0.3 envirophat 1.0.0 ExplorerHAT 0.4.2 Flask 1.0.2 fourletterphat 0.1.0 future 0.18.2 GoPiGo 0.0.0 gopigo3 1.2.0 gpiozero 1.5.1 guizero 0.6.0 html5lib 1.0.1 idna 2.6 ipykernel 4.9.0 ipython 5.8.0 ipython-genutils 0.2.0 isort 4.3.4 itsdangerous 0.24 jedi 0.13.2 Jinja2 2.10 jupyter-client 5.2.3 jupyter-core 4.4.0 keyring 17.1.1 keyrings.alt 3.1.1 kiwisolver 1.0.1 lazy-object-proxy 1.3.1 Line-Follower 1.0.0 logilab-common 1.4.2 lxml 4.3.2 MarkupSafe 1.1.0 matplotlib 3.0.2 mccabe 0.6.1 microdotphat 0.2.1 mote 0.0.4 motephat 0.0.3 mypy 0.670 mypy-extensions 0.4.1 nudatus 0.0.4 numpy 1.16.2 oauthlib 2.1.0 olefile 0.46 opencv-python 4.5.1.48 pantilthat 0.0.7 parso 0.3.1 pexpect 4.6.0 pgzero 1.2 phatbeat 0.1.1 pianohat 0.1.0 picamera 1.13 pickleshare 0.7.5 picraft 1.0 piglow 1.2.5 pigpio 1.78 Pillow 5.4.1 pip 21.0.1 pkg-resources 0.0.0 prompt-toolkit 1.0.15 psutil 5.5.1 pycairo 1.16.2 pycodestyle 2.4.0 pycparser 2.20 pycrypto 2.6.1 pycups 1.9.73 pyflakes 2.0.0 pygame 1.9.4.post1 Pygments 2.3.1 PyGObject 3.30.4 pyinotify 0.9.6 PyJWT 1.7.0 pylint 2.2.2 pyOpenSSL 19.0.0 pyparsing 2.2.0 pyserial 3.4 pysmbc 1.0.15.6 python-apt 1.8.4.3 python-dateutil 2.7.3 python-espeak 0.5 python-periphery 1.1.0 pyusb 1.1.1 pyxdg 0.25 pyzmq 17.1.2 qtconsole 4.3.1 rainbowhat 0.1.0 reportlab 3.5.13 requests 2.21.0 requests-oauthlib 1.0.0 responses 0.9.0 roman 2.0.0 RPi.GPIO 0.7.0 RTIMULib 7.2.1 scrollphat 0.0.7 scrollphathd 1.2.1 SecretStorage 2.3.1 semver 2.0.1 Send2Trash 1.5.0 sense-emu 1.1 sense-hat 2.2.0 setuptools 40.8.0 simplegeneric 0.8.1 simplejson 3.16.0 six 1.12.0 skywriter 0.0.7 smbus-cffi 0.5.1 sn3218 1.2.7 soupsieve 1.8 spidev 3.4 ssh-import-id 5.7 thonny 3.3.6 tornado 5.1.1 touchphat 0.0.1 traitlets 4.3.2 twython 3.7.0 typed-ast 1.3.1 uflash 1.2.4 unicornhathd 0.0.4 urllib3 1.24.1 wcwidth 0.1.7 webencodings 0.5.1 WebIOPi 0.7.1 Werkzeug 0.14.1 wheel 0.32.3 wiringpi 2.60.1 wrapt 1.10.11