Skip to content

Instantly share code, notes, and snippets.

@mattpetters
Created August 12, 2024 21:17
Show Gist options
  • Save mattpetters/87cd6d38c8955e2a4a5303ceed097716 to your computer and use it in GitHub Desktop.
Save mattpetters/87cd6d38c8955e2a4a5303ceed097716 to your computer and use it in GitHub Desktop.
I always forget how to setup pyenv and poetry so this is the basics
# install pyenv and poetry
brew update
brew install pyenv
pyenv --version
# pyenv 2.3.15
pyenv install 3.10.7
# Wait for a bit for install to finish...
pyenv shell 3.10.7
python --version
# Python 3.10.7 🎉
# Optional: Set A System Wide Python Version
pyenv global 3.10.7
# Install Poetry ✍️
curl -sSL https://install.python-poetry.org | python3 -
poetry --version
#Optional: Tell poetry to create virtual environments in the current project directory
poetry config virtualenvs.in-project true
poetry config virtualenvs.in-project
# true
# Steps 🧭
# Make a new project directory
cd Desktop/Code/personal/python
mkdir planet-express-api
cd planet-express-api
# Set a Python version for this project directory
pyenv local 3.10.7
python --version
# 3.10.7
cat .python-version
# 3.10.7
# Note: This command will create a file that pyenv looks for in the current working directory (the folder you are in) whose content will tell pyenv which Python version to use.
# Create a poetry project
# Leave off the -n flag if you wish to add precise data
# to the pyproject.toml file poetry creates.
poetry init -n
ls | grep pyproject.toml
# pyproject.toml
# Initialize and start your Virtual Environment using poetry
poetry shell
# Creating virtualenv planet-express-api
# bash -c planet-express-api/.venv/bin/activate
# Test it by adding a dependency
# Add a dependency
poetry add pendulum
# Start a Python REPL
python
# Use the dependency
import pendulum
now = pendulum.now("Europe/Paris")
now.to_iso8601_string()
# '2023-05-27T19:40:17.452958+02:00'
# Use `ctrl + d` to exit the REPL
# Stop using the virtual environment
# Exit the virtual environment
deactivate
# Start a Python REPL
python
# Validate the dependency is not available
import pendulum
ModuleNotFoundError: No module named 'pendulum'
# Note: To get back into the virtual environment just use poetry shell again.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment