====== Python venv 仮想環境 ====== ===== 仮想環境の作成 ===== ==== Linux ====
$ python -m venv py38
^py38^^^| 仮想環境フォルダ | | + ^include^^^^ | + ^lib^^^^ | + ^lib64 -> lib^^^^ |%%|%%| + ^python3.8^^^ |%%|%%|%%|%%| + ^site-packages| 仮想環境のパッケージ | | + ^bin^^^^ |%%|%%| + |activate|| 仮想環境を有効化 (bash) | |%%|%%| + |activate.csh|| 仮想環境を有効化 (C shell) | |%%|%%| + |activate.fish|| 仮想環境を有効化 (fish shell) | |%%|%%| + |pip|| pip コマンド | |%%|%%| + |pip3|| ::: | |%%|%%| + |pip3.8|| ::: | |%%|%%| + |python|| python コマンド | |%%|%%| + |python3|| ::: | ==== Windows ====
> python -m venv py38
^py38^^| 仮想環境フォルダ | | + ^Include^^^ | + ^Lib^^^ |%%|%%| + ^site-packages| 仮想環境のパッケージ | | + ^Scripts^^^ |%%|%%| + |activate.bat| 仮想環境を有効化 (cmd.exe) | |%%|%%| + |Activate.ps1| 仮想環境を有効化 (PowerShell) | |%%|%%| + |deactivate.bat| 仮想環境を無効化 | |%%|%%| + |pip.exe| pip コマンド | |%%|%%| + |python.exe| python コマンド | |%%|%%| + |pythonw.exe| pythonw コマンド | ===== 仮想環境の利用 ===== ==== Linux ====
$ . py38/bin/activate
(py38) $
または
$ source py38/bin/activate
(py38) $
==== Windows ====
> py38/bin/activate
(py38) >
===== 仮想環境の pip を更新 ===== ==== Linux ====
(py38) $ python -m pip install -U pip
または
(py38) $ python -m pip install --upgrade pip
==== Windows ====
(py38) > python -m pip install -U pip
または
(py38) > python -m pip install --upgrade pip
===== 仮想環境の終了 ===== ==== Linux ====
(py38) $ deactivate
==== Windows ====
(py38) > deactivate
===== トラブルシューティング ===== ==== 仮想環境で ModuleNotFoundError: No module named 'pip' が発生する😇 ====
(pyJupyterLab) $ pip freeze
Traceback (most recent call last):
  File "/home/tomoyan/pyJupyterLab/bin/pip", line 5, in 
    from pip._internal.cli.main import main
ModuleNotFoundError: No module named 'pip'
Python 3.10.6 で作った仮想環境だが、現在の Python バージョンは 3.11.1 である😢\\
(pyJupyterLab) $ cat pyvenv.cfg 
home = /usr/bin
include-system-site-packages = false
version = 3.10.6
(pyJupyterLab) $ python -V
Python 3.11.1
/lib で python3.11 -> python3.10/ のシンボルリンクを作成してみる🤔\\ これで一先ず動作するが、何か影響が出る可能性もあるかも...😅\\
(pyJupyterLab) $ cd lib
(pyJupyterLab) $ ln -s python3.10/ python3.11
(pyJupyterLab) $ ll
合計 4
drwxr-xr-x 1 tomoyan tomoyan 26  5月 21  2022 python3.10
lrwxrwxrwx 1 tomoyan tomoyan 11  2月 10 12:28 python3.11 -> python3.10/
drwxr-xr-x 1 tomoyan tomoyan 26  7月  6  2021 python3.9
(pyJupyterLab) $ pip freeze
aiohttp==3.8.1
aiosignal==1.2.0
anyio==3.6.1
...