python

Pythonで仮想環境を作成

Pythonでの開発を行う場合、プロジェクト毎に仮想環境を作成する事が一般的のようです。必要になったライブラリを都度グローバルな環境にインストールしているとつい環境汚染しがちですし、ライブラリ毎の依存関係もありますからプロジェクト毎に仮想環境を用意し、都度適切なライブラリを揃えて環境を構築していきます。

仮想環境の作成
python -m venv myenv
作成した仮想環境をアクティブ化
PowerShell環境
.\myenv\Scripts\Activate.ps1
Linux環境
source myenv/bin/activate
仮想環境の「非アクティブ化」(アクティブ化した環境から抜ける)
deactivate
-- Pythonの仮想環境(myenv)の作成
-- これで、myenvという名前のディレクトリが作成され、その中に仮想環境が設定されます。
python_test >> python -m venv myenv

-- 仮想環境のアクティブ化
-- ここではPowershellで行っています。
python_test >> .\myenv\Scripts\Activate.ps1

-- インストールされているパッケージを確認
--
python_test >> .\myenv\Scripts\Activate.ps1
(myenv) python_test >> pip list
Package Version
------- -------
pip     24.0

-- バージョンアップを促されたので上げておきます
--
[notice] A new release of pip is available: 24.0 -> 24.2
[notice] To update, run: python.exe -m pip install --upgrade pip
(myenv) python_test >> 
(myenv) python_test >> python.exe -m pip install --upgrade pip
Requirement already satisfied: pip in c:\python_test\myenv\lib\site-packages (24.0)
Collecting pip
  Downloading pip-24.2-py3-none-any.whl.metadata (3.6 kB)
Downloading pip-24.2-py3-none-any.whl (1.8 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 7.7 MB/s eta 0:00:00
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 24.0
    Uninstalling pip-24.0:
      Successfully uninstalled pip-24.0
Successfully installed pip-24.2
(myenv) python_test >> pip list
Package Version
------- -------
pip     24.2
(myenv) python_test >>


-- アクティブ化した仮想環境へJayDeBeApiをインストール
--
(myenv) python_test >> pip install JayDeBeApi
(myenv) python_test >> pip install JayDeBeApi
Collecting JayDeBeApi
  Using cached JayDeBeApi-1.2.3-py3-none-any.whl.metadata (10 kB)
Requirement already satisfied: JPype1 in d:\python_test\myenv\lib\site-packages (from JayDeBeApi) (1.5.0)
Requirement already satisfied: packaging in d:\python_test\myenv\lib\site-packages (from JPype1->JayDeBeApi) (24.1)
Using cached JayDeBeApi-1.2.3-py3-none-any.whl (26 kB)
Installing collected packages: JayDeBeApi
Successfully installed JayDeBeApi-1.2.3

[notice] A new release of pip is available: 24.0 -> 24.1.2
[notice] To update, run: python.exe -m pip install --upgrade pip
(myenv) python_test >> 
(myenv) python_test >> pip install JPype1     
Collecting JPype1
  Using cached JPype1-1.5.0-cp311-cp311-win_amd64.whl.metadata (5.0 kB)
Requirement already satisfied: packaging in d:\python_test\myenv\lib\site-packages (from JPype1) (24.1)
Using cached JPype1-1.5.0-cp311-cp311-win_amd64.whl (351 kB)
Installing collected packages: JPype1
Successfully installed JPype1-1.5.0

[notice] A new release of pip is available: 24.0 -> 24.1.2
[notice] To update, run: python.exe -m pip install --upgrade pip
(myenv) python_test >> pip list
Package    Version
---------- -------
JayDeBeApi 1.2.3
JPype1     1.5.0
packaging  24.1
pip        24.0
setuptools 65.5.0

[notice] A new release of pip is available: 24.0 -> 24.1.2
[notice] To update, run: python.exe -m pip install --upgrade pip
(myenv) python_test >>  

(myenv) python_test >> pip show JayDeBeApi
Name: JayDeBeApi
Version: 1.2.3
Summary: Use JDBC database drivers from Python 2/3 or Jython with a DB-API.
Home-page: https://github.com/baztian/jaydebeapi
Author: Bastian Bowe
Author-email: bastian.dev@gmail.com
License: GNU LGPL
Location: D:\python_test\myenv\Lib\site-packages
Requires: JPype1
Required-by:
(myenv) python_test >> 
(myenv) python_test >> pip list
Package    Version
---------- -------
JayDeBeApi 1.2.3
JPype1     1.5.0
packaging  24.1
pip        24.0
setuptools 65.5.0

[notice] A new release of pip is available: 24.0 -> 24.1.2
[notice] To update, run: python.exe -m pip install --upgrade pip
(myenv) python_test >> 
(myenv) python_test >> pip uninstall JPype1
Found existing installation: JPype1 1.5.0
Uninstalling JPype1-1.5.0:
  Would remove:
    d:\python_test\myenv\lib\site-packages\_jpype.cp311-win_amd64.pyd
    d:\python_test\myenv\lib\site-packages\jpype1-1.5.0.dist-info\*
    d:\python_test\myenv\lib\site-packages\jpype\*
    d:\python_test\myenv\lib\site-packages\org.jpype.jar
Proceed (Y/n)? y
  Successfully uninstalled JPype1-1.5.0
(myenv) python_test >> pip list
Package    Version
---------- -------
JayDeBeApi 1.2.3
packaging  24.1
pip        24.0
setuptools 65.5.0

[notice] A new release of pip is available: 24.0 -> 24.1.2
[notice] To update, run: python.exe -m pip install --upgrade pip
(myenv) python_test >> 

-- 
python_test >> pip install pyinstaller
Collecting pyinstaller
  Downloading pyinstaller-6.9.0-py3-none-win_amd64.whl.metadata (8.3 kB)
Requirement already satisfied: setuptools>=42.0.0 in d:\python_test\myenv\lib\site-packages (from pyinstaller) (65.5.0)
Collecting altgraph (from pyinstaller)
  Downloading altgraph-0.17.4-py2.py3-none-any.whl.metadata (7.3 kB)
Collecting pyinstaller-hooks-contrib>=2024.7 (from pyinstaller)
  Downloading pyinstaller_hooks_contrib-2024.7-py2.py3-none-any.whl.metadata (16 kB)
Requirement already satisfied: packaging>=22.0 in d:\python_test\myenv\lib\site-packages (from pyinstaller) (24.1)
Collecting pefile>=2022.5.30 (from pyinstaller)
  Downloading pefile-2023.2.7-py3-none-any.whl.metadata (1.4 kB)
Collecting pywin32-ctypes>=0.2.1 (from pyinstaller)
  Downloading pywin32_ctypes-0.2.2-py3-none-any.whl.metadata (3.8 kB)
Downloading pyinstaller-6.9.0-py3-none-win_amd64.whl (1.3 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.3/1.3 MB 2.6 MB/s eta 0:00:00
Downloading pefile-2023.2.7-py3-none-any.whl (71 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 71.8/71.8 kB 3.8 MB/s eta 0:00:00
Downloading pyinstaller_hooks_contrib-2024.7-py2.py3-none-any.whl (341 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 341.3/341.3 kB 4.2 MB/s eta 0:00:00
Downloading pywin32_ctypes-0.2.2-py3-none-any.whl (30 kB)
Downloading altgraph-0.17.4-py2.py3-none-any.whl (21 kB)
Installing collected packages: altgraph, pywin32-ctypes, pyinstaller-hooks-contrib, pefile, pyinstaller
Successfully installed altgraph-0.17.4 pefile-2023.2.7 pyinstaller-6.9.0 pyinstaller-hooks-contrib-2024.7 pywin32-ctypes-0.2.2

[notice] A new release of pip is available: 24.0 -> 24.1.2
[notice] To update, run: python.exe -m pip install --upgrade pip
python_test >> pip list
Package                   Version
------------------------- --------
altgraph                  0.17.4
JayDeBeApi                1.2.3
JPype1                    1.5.0
packaging                 24.1
pefile                    2023.2.7
pip                       24.0
pyinstaller               6.9.0
pyinstaller-hooks-contrib 2024.7
pywin32-ctypes            0.2.2
setuptools                65.5.0

[notice] A new release of pip is available: 24.0 -> 24.1.2
[notice] To update, run: python.exe -m pip install --upgrade pip
python_test >> 
参考
https://qiita.com/nishikyon/items/448263fefa2e806e26f7
スポンサーリンク
タイトルとURLをコピーしました