LoginSignup
0
0

WSL2にpyenv + poetryでpython3.10の環境を作る

Last updated at Posted at 2023-11-14

やりたいことと問題

デフォルトのpythonは3.8だった。type hintが強化されているpython 3.11, 3.10を使いたい。
個人的にはpyenvでpythonバージョン管理をして、poetryでパッケージ管理をするのに慣れているのでそうする。

ところが、普通に入れようとするとちょっと詰まるのでメモ

pyenvをいれる

公式のサイトや、インストーラーのサイト通りに、下記で入れる

curl https://pyenv.run | bash

なぜか.pyenvディレクトリがいたので削除。

pyenv経由でpython3.10を入れる

pyenv install 3.10

これで入ると思いきや、下記のようにbz2やらtkinterやらsqlite3やらがないと言って怒られる。

MERIK@DESKTOP:~$ pyenv install 3.10
pyenv: /home/MERIK/.pyenv/versions/3.10.13 already exists
continue with installation? (y/N) y
Downloading Python-3.10.13.tar.xz...
-> https://www.python.org/ftp/python/3.10.13/Python-3.10.13.tar.xz
Installing Python-3.10.13...
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/MERIK/.pyenv/versions/3.10.13/lib/python3.10/bz2.py", line 17, in <module>
    from _bz2 import BZ2Compressor, BZ2Decompressor
ModuleNotFoundError: No module named '_bz2'
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/MERIK/.pyenv/versions/3.10.13/lib/python3.10/curses/__init__.py", line 13, in <module>
    from _curses import *
ModuleNotFoundError: No module named '_curses'
WARNING: The Python curses extension was not compiled. Missing the ncurses lib?
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'readline'
WARNING: The Python readline extension was not compiled. Missing the GNU readline lib?
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/MERIK/.pyenv/versions/3.10.13/lib/python3.10/sqlite3/__init__.py", line 57, in <module>
    from sqlite3.dbapi2 import *
  File "/home/MERIK/.pyenv/versions/3.10.13/lib/python3.10/sqlite3/dbapi2.py", line 27, in <module>
    from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'
WARNING: The Python sqlite3 extension was not compiled. Missing the SQLite3 lib?
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/MERIK/.pyenv/versions/3.10.13/lib/python3.10/tkinter/__init__.py", line 37, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
WARNING: The Python tkinter extension was not compiled and GUI subsystem has been detected. Missing the Tk toolkit?
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/MERIK/.pyenv/versions/3.10.13/lib/python3.10/lzma.py", line 27, in <module>
    from _lzma import *
ModuleNotFoundError: No module named '_lzma'
WARNING: The Python lzma extension was not compiled. Missing the lzma lib?
Installed Python-3.10.13 to /home/MERIK/.pyenv/versions/3.10.13

ググると先人の知恵を見つけることができ、下記のようにして必要なライブラリをいれたらpython3.10が入るようになった。

sudo apt-get install build-essential zlib1g-dev libffi-dev libssl-dev libbz2-dev libreadline-dev libsqlite3-dev liblzma-dev
sudo apt-get install python-tk python3-tk tk-dev
pyenv install 3.10

pyenvでいれたpython 3.10でpoetryを使う

当面はpython 3.10が使えればいいので、python 3.10をglobalにしてpoetryをインストールする

poetryをいれるまえに、pyenvがきちんと入ったかを確認しておく。
ちゃんと、python で python 3.10が起動するようになったことを下記で確認。

MERIK@DESKTOP:~$ pyenv global 3.10
MERIK@DESKTOP:~$ python
Python 3.10.13 (main, Nov 14 2023, 21:11:31) [GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()

大丈夫そうなので、poetryの公式サイトを参考に、少し書き換えて下記のようにしていれる

curl -sSL https://install.python-poetry.org | python -

動作確認

MERIK@DESKTOP:~$ poetry -V
Poetry (version 1.7.0)

ちゃんと入っている。

poetry initで適当にpyproject.tomlを作ると、きちんとcompatible python versionsが^3.10で生成される。

MERIK@DESKTOP ~$ poetry init

This command will guide you through creating your pyproject.toml config.

Package name [nice_package]:  
Version [0.1.0]:  
Description []:  
Author [None, n to skip]:  n
License []:  
Compatible Python versions [^3.10]:  

Would you like to define your main dependencies interactively? (yes/no) [yes] no
Would you like to define your development dependencies interactively? (yes/no) [yes] no
Generated file

[tool.poetry]
name = "nice_package"
version = "0.1.0"
description = ""
authors = ["Your Name <you@example.com>"]
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.10"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

あとがき

5年前もWSLの記事を書いていました…。
あの頃よりはだいぶコード書けるようになった…はず。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0