0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pyenv+poetryの初期設定

Last updated at Posted at 2025-06-20

pyenv + poetry セットアップガイド

1. 前提条件

  • macOS
  • Homebrew がインストール済み
  • zsh シェルを使用

2. インストール手順

2.1 pyenvのインストール

# Homebrewを使用してpyenvをインストール
brew install pyenv

# 環境変数の設定(~/.zshrcに追加)
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc

2.2 poetryのインストール

# poetryのインストール
curl -sSL https://install.python-poetry.org | python3 -

# PATHの設定(~/.zshrcに追加)
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.zshrc

# 設定の反映
source ~/.zshrc

3. 基本的な使用方法

3.1 pyenvの使用方法

# Pythonバージョンの一覧表示
pyenv versions

# 特定のバージョンをインストール
pyenv install 3.11.0

# グローバルバージョンの設定
pyenv global 3.11.0

# ローカルバージョンの設定(プロジェクトごと)
pyenv local 3.11.0

3.2 poetryの使用方法

# 新規プロジェクトの作成
poetry new プロジェクト名

# 環境はpython3.11を使用
poetry env use python3.11

# 既存プロジェクトの初期化
poetry init

# パッケージのインストール
poetry add パッケージ名

# 開発用パッケージのインストール
poetry add --dev パッケージ名

# 仮想環境の有効化
poetry shell

# 依存関係のインストール
poetry install

4. 推奨設定

4.1 poetryの設定

# 仮想環境をプロジェクトディレクトリ内に作成
poetry config virtualenvs.in-project true

# 並列インストールを有効化
poetry config installer.parallel true

5. プロジェクト構成例

my-project/
├── .venv/                  # 仮想環境(poetry config virtualenvs.in-project true の場合)
├── .python-version         # pyenvのローカルバージョン設定
├── pyproject.toml         # poetryの設定ファイル
├── poetry.lock           # 依存関係のロックファイル
└── src/                  # ソースコード
    └── my_project/
        ├── __init__.py
        └── main.py

6. トラブルシューティング

6.1 よくある問題と解決方法

  1. パスが通っていない場合

    • ~/.zshrc の設定を確認
    • source ~/.zshrc で設定を再読み込み
  2. poetryコマンドが見つからない場合

    • which poetry で実際のパスを確認
    • PATHが正しく設定されているか確認
  3. pyenvでPythonのインストールに失敗する場合

    • Xcodeコマンドラインツールが最新かを確認
    • xcode-select --install を実行

7. 参考リンク

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?