Last active
October 12, 2022 08:47
-
-
Save Ningensei848/dc0061f85966e16210ac49fd477c8dd1 to your computer and use it in GitHub Desktop.
Create a virtual environment on Google Colaboratory by installing Poetry (and Python 3.9) and configure it as a kernel for running Jupyter notebook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat":4, | |
"nbformat_minor":0, | |
"metadata":{ | |
"colab":{ | |
"authorship_tag":"ABX9TyM9cuAxUnZq5HhgoBfcq0R+", | |
"include_colab_link":true, | |
"name":"poetry_on_google_colab.ipynb", | |
"provenance":[ | |
], | |
"collapsed_sections":[ | |
], | |
"private_outputs":true | |
}, | |
"kernelspec":{ | |
"name":"py39", | |
"display_name":"Python 3.9" | |
}, | |
"language_info":{ | |
"name":"python" | |
} | |
}, | |
"cells":[ | |
{ | |
"cell_type":"markdown", | |
"metadata":{ | |
"colab_type":"text", | |
"id":"view-in-github" | |
}, | |
"source":[ | |
"<a href=\"https://colab.research.google.com/gist/Ningensei848/dc0061f85966e16210ac49fd477c8dd1/poetry_on_google_colab.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type":"markdown", | |
"source":[ | |
"# Google Colab 上で Poetry の仮想環境をカーネルとして使う\n", | |
"\n", | |
"## なにをしているか\n", | |
"\n", | |
"1. notebook のメタデータ項目: _kernelspec_ を \"py39\" 等に書き換える(なんでもよい)\n", | |
"2. \\[ファイル\\] > \\[ノートブックを開く\\] と選択し、1 のノートブックを colab 上で開く\n", | |
"3. 初めてセルを実行する際には「ランタイム `py39` は認識されていません」と表示されるので、無視する\n", | |
"4. `miniconda` 経由で `python3.9` をインストールする\n", | |
"5. `poetry` をインストールし、仮想環境を作成してパスを通す(←最重要ポイント!!!)\n", | |
"6. `ipykernel` で、`python3.9` を `py39` という名前のカーネルとして保存 → `python -m ipykernel install --name \"py39\" --user`\n", | |
"7. 現在接続しているカーネルから切り替える\n", | |
" - \\[ランタイム\\] > \\[ランタイムのタイプを変更\\] から、`kernelspec` で指定した名前に変更して\\[保存\\]する\n", | |
"8. 再度セルを実行して、3 で表示された警告がでなければ成功!\n", | |
"\n", | |
"\n", | |
"## 注意\n", | |
"\n", | |
"- _kernelspec_ に追記する名前と、新規カーネル登録に指定する名前を一致させる(不一致だと当然読み込めない)\n", | |
"- miniconda 以外の方法でインストールしようと試みたが、うまくいかなかった(jupyter, google-colab の依存ライブラリの問題と思われる)\n", | |
"\n", | |
"cf. [Google Colaboratory の Python 実行環境をアップデートする](https://zenn.dev/tk42/articles/92e34bb8910fd9)" | |
], | |
"metadata":{ | |
"id":"SiMxVAXyAHyW" | |
} | |
}, | |
{ | |
"cell_type":"code", | |
"source":[ | |
"!python --version" | |
], | |
"metadata":{ | |
"id":"b0qZapHW55RK" | |
}, | |
"execution_count":null, | |
"outputs":[ | |
] | |
}, | |
{ | |
"cell_type":"code", | |
"source":[ | |
"import os\n", | |
"import sys" | |
], | |
"metadata":{ | |
"id":"gKm9tpwqNwwT" | |
}, | |
"execution_count":null, | |
"outputs":[ | |
] | |
}, | |
{ | |
"cell_type":"code", | |
"source":[ | |
"# @title `pyproject.toml` を準備する\n", | |
"# @markdown ※このノートブックではひとまずデフォルト設定のままとしておく\n", | |
"\n", | |
"PYPROJECT_TOML = \"\"\"\n", | |
"[tool.poetry]\n", | |
"name = \"content\"\n", | |
"version = \"0.0.0\"\n", | |
"description = \"\"\n", | |
"authors = [\"Your Name <you@example.com>\"]\n", | |
"readme = \"README.md\"\n", | |
"\n", | |
"[tool.poetry.dependencies]\n", | |
"python = \"^3.9\"\n", | |
"\n", | |
"\n", | |
"[build-system]\n", | |
"requires = [\"poetry-core\"]\n", | |
"build-backend = \"poetry.core.masonry.api\"\n", | |
"\n", | |
"\"\"\"\n", | |
"\n", | |
"with open(\"/content/pyproject.toml\", mode='w') as f:\n", | |
" f.write(PYPROJECT_TOML)\n", | |
"\n" | |
], | |
"metadata":{ | |
"cellView":"form", | |
"id":"JcOzHyrrJxif" | |
}, | |
"execution_count":null, | |
"outputs":[ | |
] | |
}, | |
{ | |
"cell_type":"code", | |
"source":[ | |
"# @title Python 3.9 をインストールする\n", | |
"# @markdown 3.9 なら → 何もしない <br />\n", | |
"# @markdown 3.9 ではない → `miniconda` 経由で `python 3.9` をインストールする\n", | |
"\n", | |
"[major, minor, _micro, _level, _serial] = list(sys.version_info)\n", | |
"\n", | |
"\n", | |
"if major == 3 and minor == 9:\n", | |
" pass\n", | |
"else:\n", | |
" # install python 3.9 ----------------------------------------------------------------------\n", | |
" !wget -O mini.sh https://repo.anaconda.com/miniconda/Miniconda3-py39_4.9.2-Linux-x86_64.sh\n", | |
" !chmod +x mini.sh\n", | |
" !bash ./mini.sh -b -f -p /usr/local\n", | |
" !conda install -q -y jupyter\n", | |
" !conda install -q -y google-colab -c conda-forge\n", | |
"\n", | |
" # install poetry -----------------------------------------------------------------------------\n", | |
" os.environ['PATH'] = f\"/content/.venv/bin:/root/.local/bin:{os.environ['PATH']}\"\n", | |
" !curl -sSL https://install.python-poetry.org | python -\n", | |
" !poetry --version\n", | |
" !which poetry\n", | |
" # 任意の場所に .venv フォルダを作り仮想環境を管理したい\n", | |
" !poetry config virtualenvs.in-project true\n", | |
" # グローバルな python に存在するライブラリを使えるように\n", | |
" !poetry config virtualenvs.options.system-site-packages true\n", | |
" !poetry install\n", | |
" !poetry run python -m ipykernel install --name \"py39\" --user\n" | |
], | |
"metadata":{ | |
"id":"r3sLiMIs8If3", | |
"cellView":"form" | |
}, | |
"execution_count":null, | |
"outputs":[ | |
] | |
}, | |
{ | |
"cell_type":"code", | |
"source":[ | |
"# 利用可能なカーネルの一覧を表示\n", | |
"!jupyter kernelspec list\n" | |
], | |
"metadata":{ | |
"id":"-Fx5xZ7B3u63" | |
}, | |
"execution_count":null, | |
"outputs":[ | |
] | |
}, | |
{ | |
"cell_type":"code", | |
"source":[ | |
"# 登録したカーネルに関する情報の所在と内容の確認\n", | |
"!ls -la /root/.local/share/jupyter/kernels/py39\n", | |
"!cat /root/.local/share/jupyter/kernels/py39/kernel.json\n" | |
], | |
"metadata":{ | |
"id":"TtuRkzw6GO-s" | |
}, | |
"execution_count":null, | |
"outputs":[ | |
] | |
}, | |
{ | |
"cell_type":"code", | |
"source":[ | |
"# カーネルが py39 を認識していない場合、こちらは 3.7.x と表示される\n", | |
"print(\"User Current Version:-\", sys.version)\n", | |
"print(sys.executable)\n" | |
], | |
"metadata":{ | |
"id":"yG0W2R75HVu5" | |
}, | |
"execution_count":null, | |
"outputs":[ | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment