131 lines
No EOL
4.7 KiB
Markdown
131 lines
No EOL
4.7 KiB
Markdown
#python #pyenv #poetry
|
||
|
||
# 20240329134609 pythonプロジェクトの実行方法 作り方
|
||
|
||
|
||
私家版。私の勉強が進むにつれて、チョイチョイ更新されると思う
|
||
|
||
ここに記載した内容を実施、Hello Worldが表示される状態にしたものを置いておいた。
|
||
- [kazbo/python-project-template - Kinoshita Lab Git: All your base are belong to us](https://git.kinoshita-lab.org/kazbo/python-project-template)
|
||
|
||
|
||
## 何がしたかったのか
|
||
- システムのpythonを、作りたいソフトの都合でライブラリを入れたり消したりして汚したくない。 [[20240311135526 pip installでerror externally-managed-environment|PEP 668]] なども導入されて、インストールできない場合も今後増えそう
|
||
- 実行環境が色々存在しているため、揃えたい。nodeの人がnvm/npm使うのと同じようなことができたい
|
||
- [[20240311211049 pythonのプロジェクト構成]] に準じた標準に近い状態にしたい
|
||
|
||
----
|
||
|
||
## 実行方法
|
||
```
|
||
$ poetry install # これは1回だけやればよい
|
||
$ poetry -m python [project名]
|
||
```
|
||
|
||
----
|
||
|
||
## プロジェクト作成方法
|
||
### システム側での操作
|
||
pyenv+poetryが必要。 [[20240315154213 pyenv|pyenv]] 参照。
|
||
|
||
pyenvに使いたいpythonをインストール。基本的には最新を使うようにする。
|
||
下記を新バージョンをインストールするごとに1度実行する。 poetryは1.8以降を使用。
|
||
```
|
||
$ pyenv install [使いたいpython]
|
||
$ pyenv global [使いたいpython]
|
||
$ pip install poetry
|
||
```
|
||
|
||
### プロジェクト作成
|
||
|
||
```
|
||
$ mkdir -p [project]/[project]
|
||
$ cd [project]
|
||
$ poetry init
|
||
$ [適当に設定]
|
||
```
|
||
で、`pyproject.toml`ができる。
|
||
この後、pyprojetの`[tool.poetry]`セクションに、
|
||
`package-mode = false` を追記しておく。 これが何か?というと、poetryは元々package modeというライブラリ配布に特化したソフトウェアだったが、最近(1.8以降)アプリの実行時の依存性解決に使えるようになった。 その設定。 cf. [Operating Modes](https://python-poetry.org/docs/basic-usage/#operating-modes)
|
||
|
||
以下、`[project]/[project]`フォルダ内に `__init__.py`, `__main__.py` を作る。
|
||
フォルダを2重に作らないといけないのが微妙に不便だが、しかたない。
|
||
```
|
||
$ touch [project]/__init__.py
|
||
$ touch [project]/__main__.py
|
||
```
|
||
`__init__.py`があることで、このフォルダが1つのまとまりとしてpythonに認識される。 ``__main__.py``が、このフォルダ1つのかたまりとして認識された場合、実行時に自動的に呼びだされる。つまり、フォルダ内を1つのプロジェクトとしてまるごと実行したい場合、この両方のファイルが必要。
|
||
|
||
さらに、`__main__.py` は、[[202307291217 pythonでモジュールファイルを単体で実行したい場合|pythonでモジュールファイルを単体で実行したい場合]]の説明に沿って、
|
||
```py
|
||
if __name__ == "__main__":
|
||
# ここにやりたいことを書く
|
||
```
|
||
|
||
こんな内容になる。
|
||
|
||
### ライブラリなどの追加
|
||
ルートで、
|
||
```
|
||
$ poetry install [インストールしたいライブラリ]
|
||
```
|
||
でインストールされていく。 pyporoject.tomlに自動で追記される(はず)
|
||
|
||
|
||
### 結果できる poetry.toml の例
|
||
```toml
|
||
[tool.poetry]
|
||
name = "python-project-template" # ここ毎回かえる
|
||
version = "0.1.0"
|
||
description = "template project for pyenv+poetry" # ここ毎回かえる
|
||
authors = ["Kaz Saita(raspi4) <saita@kinoshita-lab.org>"] #かえる
|
||
license = "MIT"
|
||
readme = "README.md"
|
||
package-mode = false
|
||
|
||
[tool.poetry.dependencies]
|
||
python = "^3.11" # 必要におうじて
|
||
# ライブラリなど追加するとここに追記される
|
||
|
||
[build-system]
|
||
requires = ["poetry-core"]
|
||
build-backend = "poetry.core.masonry.api"
|
||
```
|
||
|
||
|
||
### 実行
|
||
[[#20240329134609 pythonプロジェクトの実行方法 作り方#実行方法]] を参照。
|
||
|
||
### .gitignore
|
||
最低限こんな感じ。その他、必要に応じて
|
||
```
|
||
.venv/*
|
||
poetry.lock
|
||
__pycache__/
|
||
```
|
||
|
||
### vscode使いたい場合の設定
|
||
#### settings.json
|
||
```
|
||
{
|
||
"terminal.integrated.enablePersistentSessions": true,
|
||
"python.terminal.activateEnvInCurrentTerminal": true,
|
||
"python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python"
|
||
}
|
||
```
|
||
#### launch.json
|
||
```
|
||
{
|
||
"version": "0.2.0",
|
||
"configurations": [
|
||
{
|
||
"type" : "debugpy",
|
||
"request": "launch",
|
||
"name": "Launch project",
|
||
"cwd": "${workspaceFolder}",
|
||
"module": "python-project-template", // ここ毎回かえる
|
||
//"args": [(あれば)],
|
||
}
|
||
]
|
||
}
|
||
``` |