WindowsでPython(pyenv, Poetry)

Pythonを利用するために、Pythonのバージョン管理としてpyenv、パッケージの依存関係とプロジェクト管理にPoetryを使っていきます。

Windowsのネイティブ環境(WSLではない)で使います。アプリのインストールにはScoopをなるべく使うようにします。

pyenvのインストール

Scoopに有るか確認

> scoop search pyenv
Results from local buckets...

Name  Version Source Binaries
----  ------- ------ --------
pyenv 3.1.1   main

> scoop info pyenv

Name        : pyenv
Description : Simple python version management tool for switching between multiple versions of Python.
Version     : 3.1.1
Bucket      : main
Website     : https://pyenv-win.github.io/pyenv-win
License     : MIT
Updated at  : 2022/09/22 0:48:54
Updated by  : Christophe Dervieux
Environment : PYENV = <root>\pyenv-win
              PYENV_HOME = <root>\pyenv-win
              PYENV_ROOT = <root>\pyenv-win
Path Added  : <root>\pyenv-win\bin
              <root>\pyenv-win\shims
Notes       : You can specify the PYTHON_BUILD_MIRROR_URL environment variable and run 'pyenv update' to speed up downl
              oads.

Scoopでインストール

> scoop install pyenv
Installing 'pyenv' (3.1.1) [64bit] from 'main' bucket
Downloading https://github.com/pyenv-win/pyenv-win/archive/v3.1.1.zip (-1 B)...

Checking hash of v3.1.1.zip ... ok.
Extracting v3.1.1.zip ... done.
Running pre_install script...done.
Linking ~\scoop\apps\pyenv\current => ~\scoop\apps\pyenv\3.1.1
Adding ~\scoop\apps\pyenv\current\pyenv-win\bin to your path.
Adding ~\scoop\apps\pyenv\current\pyenv-win\shims to your path.
Persisting pyenv-win\version
Persisting pyenv-win\shims
Persisting pyenv-win\versions
Persisting pyenv-win\install_cache
'pyenv' (3.1.1) was installed successfully!
Notes
-----
You can specify the PYTHON_BUILD_MIRROR_URL environment variable and run 'pyenv update' to speed up downloads.

pyenvのバージョンを確認
ついでに初期状態のpythonのバージョンも確認

> pyenv --version
pyenv 3.1.1
> pyenv versions
> pyenv version
No global/local python version has been set yet. Please set the global/local version by typing:
pyenv global 3.7.4
pyenv local 3.7.4

Pythonのインストール

pyenvでインストールできるPythonのバージョンを確認
アップデートしないと古いバージョンまでしか出ないのでしておくこと。

> pyenv install -l
:: [Info] ::  Mirror: https://www.python.org/ftp/python
2.4-win32
・・中略・・
3.11.0b4
> pyenv update
:: [Info] ::  Mirror: https://www.python.org/ftp/python
:: [Info] ::  Scanned 214 pages and found 665 installers.
> pyenv install -l
:: [Info] ::  Mirror: https://www.python.org/ftp/python
2.4-win32
・・中略・・
3.13.0rc1

最新の 3.12.5 をインストール

> pyenv install 3.12.5
:: [Info] ::  Mirror: https://www.python.org/ftp/python
:: [Downloading] ::  3.12.5 ...
:: [Downloading] ::  From https://www.python.org/ftp/python/3.12.5/python-3.12.5-amd64.exe
:: [Downloading] ::  To   C:\Users\tmacoto\scoop\apps\pyenv\current\pyenv-win\install_cache\python-3.12.5-amd64.exe
:: [Installing] ::  3.12.5 ...
:: [Error] :: error installing "core" component MSI.
:: [Error] :: couldn't install 3.12.5

エラー? #449

解決したと書いている通りに、管理者権限でpyenvをインストールしてみる。

> scoop uninstall pyenv
Uninstalling 'pyenv' (3.1.1).
Unlinking ~\scoop\apps\pyenv\current
Removing ~\scoop\apps\pyenv\current\pyenv-win\bin from your path.
Removing ~\scoop\apps\pyenv\current\pyenv-win\shims from your path.
'pyenv' was uninstalled.

管理者権限で実行(管理者として新しく開く)
> scoop install pyenv
Installing 'pyenv' (3.1.1) [64bit] from 'main' bucket
Loading v3.1.1.zip from cache
Checking hash of v3.1.1.zip ... ok.
Extracting v3.1.1.zip ... done.
Running pre_install script...done.
Linking ~\scoop\apps\pyenv\current => ~\scoop\apps\pyenv\3.1.1
Adding ~\scoop\apps\pyenv\current\pyenv-win\bin to your path.
Adding ~\scoop\apps\pyenv\current\pyenv-win\shims to your path.
Persisting pyenv-win\version
Persisting pyenv-win\shims
Persisting pyenv-win\versions
Persisting pyenv-win\install_cache
'pyenv' (3.1.1) was installed successfully!
Notes
-----
You can specify the PYTHON_BUILD_MIRROR_URL environment variable and run 'pyenv update' to speed up downloads.

一般ユーザで実行(一般ユーザとして新しく開く)
> pyenv --version
pyenv 3.1.1
> pyenv update
:: [Info] ::  Mirror: https://www.python.org/ftp/python
:: [Info] ::  Scanned 214 pages and found 665 installers.
> pyenv install 3.12.5
:: [Info] ::  Mirror: https://www.python.org/ftp/python
:: [Installing] ::  3.12.5 ...
:: [Info] :: completed! 3.12.5

**注意**
ユーザに管理権限がない場合(管理者ユーザにスイッチする場合)は上記のように管理者権限でscoopコマンドが使えないので、もう一つの解決策を実施するといけました。

Run cmd.exe as Administrator:

C:\Users\bob\scoop\apps\pyenv>rmdir current
C:\Users\bob\scoop\apps\pyenv>mklink /J current 3.1.1

C:\Users\bob\scoop\apps\pyenv\3.1.1\pyenv-win>rmdir install_cache
C:\Users\bob\scoop\apps\pyenv\3.1.1\pyenv-win>mklink /J install_cache C:\Users\bob\scoop\persist\pyenv\pyenv-win\install_cache

C:\Users\bob\scoop\apps\pyenv\3.1.1\pyenv-win>rmdir shims
C:\Users\bob\scoop\apps\pyenv\3.1.1\pyenv-win>mklink /J shims C:\Users\bob\scoop\persist\pyenv\pyenv-win\shims

C:\Users\bob\scoop\apps\pyenv\3.1.1\pyenv-win>rmdir versions
C:\Users\bob\scoop\apps\pyenv\3.1.1\pyenv-win>mklink /J versions C:\Users\bob\scoop\persist\pyenv\pyenv-win\versions

動作確認

> pyenv versions
  3.12.5
> pyenv version
No global/local python version has been set yet. Please set the global/local version by typing:
pyenv global 3.7.4
pyenv local 3.7.4
> pyenv global 3.12.5
> python -V
Python 3.12.5

インストールできたっぽい。

シンボリックリンクを管理者権限で作るとうまくいくらしい。Scoopは管理者権限が要らないのがいいところなのになー。

Scoopを利用しないpyenv-winの公式のインストール方法だと問題ないようです。
Scoopを使い続けるべきか。。悩む。

Poetryのインストール

Scoopに有るか確認

> scoop search poetry
Results from local buckets...

Name   Version Source Binaries
----   ------- ------ --------
poetry 1.8.3   main

> scoop info poetry

Name         : poetry
Description  : Dependency Management for Python
Version      : 1.8.3
Bucket       : main
Website      : https://python-poetry.org
License      : MIT
Dependencies : python
Updated at   : 2024/05/09 1:36:43
Updated by   : github-actions[bot]
Binaries     : venv\Scripts\poetry.exe

Scoopでインストール

> scoop install poetry
Installing 'dark' (3.14) [64bit] from 'main' bucket
Retrieving Poetry metadata

# Welcome to Poetry!

This will download and install the latest version of Poetry,
a dependency and package manager for Python.

It will add the `poetry` command to Poetry's bin directory, located at:

C:\Users\tmacoto\scoop\apps\poetry\1.8.3\bin

You can uninstall at any time by executing this script with the --uninstall option,
and these changes will be reverted.

Installing Poetry (1.8.3)
Installing Poetry (1.8.3): Creating environment
Installing Poetry (1.8.3): Installing Poetry
Installing Poetry (1.8.3): Creating script
Installing Poetry (1.8.3): Done

Poetry (1.8.3) is installed now. Great!

To get started you need Poetry's bin directory (C:\Users\tmacoto\scoop\apps\poetry\1.8.3\bin) in your `PATH`
environment variable.

Alternatively, you can call Poetry explicitly with `C:\Users\tmacoto\scoop\apps\poetry\1.8.3\bin\poetry`.

You can test that everything is set up by executing:

`poetry --version`

done.
Linking ~\scoop\apps\poetry\current => ~\scoop\apps\poetry\1.8.3
Creating shim for 'poetry'.
'poetry' (1.8.3) was installed successfully!

途中で画面クリアが走ってログが取れなかった。(darkとpythonがインストールされたようだった。)

poetryのバージョンを確認

> poetry --version
Poetry (version 1.8.3)

**注意**

Poetryをインストールした時にpythonがインストールされてしまって、そちらのパスが先に設定されてしまい、pyenvでバージョンを指定しても効かなくなっていた。
pythonはuninstallしたらpyenvで指定したバージョンが使われるようになった。

> pyenv global 3.10.11
> pyenv versions
* 3.10.11 (set by C:\Users\tmacoto\scoop\apps\pyenv\current\pyenv-win\version)
  3.12.5
> python -V
Python 3.12.5
> python -c "import sys; print(sys.executable)"
C:\Users\tmacoto\scoop\apps\python\current\python.exe
> scoop uninstall python
Uninstalling 'python' (3.12.5).
Running uninstaller script...done.
Removing shim 'python3.shim'.
Removing shim 'python3.exe'.
Removing shim 'idle'.
Removing shim 'idle.cmd'.
Removing shim 'idle3'.
Removing shim 'idle3.cmd'.
Unlinking ~\scoop\apps\python\current
Removing ~\scoop\apps\python\current\Scripts from your path.
Removing ~\scoop\apps\python\current from your path.
'python' was uninstalled.
PS C:\Users\tmacoto> python -V
Python 3.10.11
> python -c "import sys; print(sys.executable)"
C:\Users\tmacoto\scoop\apps\pyenv\current\pyenv-win\versions\3.10.11\python.exe

システムとpyenvの両方のpythonを切り替えて使えるようにできるはずなんだけど、とりあえず一旦消しておきます。

使い方

globalを3.12.5にしてプロジェクトでは3.10.11を指定してみます。

> pyenv versions
  3.10.11
* 3.12.5 (set by C:\Users\tmacoto\scoop\apps\pyenv\current\pyenv-win\version)
> mkdir test-project

    Directory: C:\Users\tmacoto\Work

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----          2024/08/16    19:21                test-project

> cd .\test-project\
> pyenv local 3.10.11
> python -V
Python 3.10.11
> pyenv versions
* 3.10.11 (set by C:\Users\tmacoto\Work\test-project\.python-version)
  3.12.5

Poetryの設定

> poetry config --list
cache-dir = "C:\\Users\\tmacoto\\AppData\\Local\\pypoetry\\Cache"
experimental.system-git-client = false
installer.max-workers = null
installer.modern-installation = true
installer.no-binary = null
installer.parallel = true
keyring.enabled = true
solver.lazy-wheel = true
virtualenvs.create = true
virtualenvs.in-project = null
virtualenvs.options.always-copy = false
virtualenvs.options.no-pip = false
virtualenvs.options.no-setuptools = false
virtualenvs.options.system-site-packages = false
virtualenvs.path = "{cache-dir}\\virtualenvs"  # C:\Users\tmacoto\AppData\Local\pypoetry\Cache\virtualenvs
virtualenvs.prefer-active-python = false
virtualenvs.prompt = "{project_name}-py{python_version}"
warnings.export = true

Poetryの仮想環境はプロジェクト内に含めたいので、下記を設定

> poetry config virtualenvs.in-project true

Poetryの初期化
色々聞いてきますが、基本的にそのままエンター。Pythonのバージョンだけ^3.10に変更しました。

> poetry init

Poetryで利用するPythonをpyenvで設定したバージョンに指定(忘れがち)
これをしないとglobalに設定した(または一番新しい)バージョンが設定されます。

> poetry env use python
Creating virtualenv test-project in C:\Users\tmacoto\Work\test-project\.venv
Using virtualenv: C:\Users\tmacoto\Work\test-project\.venv

Poetry環境のインストール

> poetry install
Updating dependencies
Resolving dependencies... (0.1s)

Writing lock file

Installing the current project: test-project (0.1.0)
Warning: The current project could not be installed: [Errno 2] No such file or directory: 'C:\\Users\\tmacoto\\Work\\test-project\\README.md'
If you do not want to install the current project use --no-root.
If you want to use Poetry only for dependency management but not for packaging, you can disable package mode by setting package-mode = false in your pyproject.toml file.
In a future version of Poetry this warning will become an error!

Poetry環境でPythonのバージョンを確認

> poetry shell
Spawning shell within C:\Users\tmacoto\Work\test-project\.venv
PowerShell 7.4.4
> python -V
Python 3.10.11

コメント