Instructions for a mac on how to play with pyenv and poetry
Why both? Pyenv to manage multiple python versions. You can make virtualenvs with pyenv of course, but using poetry will simplify that process
# Install pyenv.
brew install pyenv
# Add pyenv initializer to shell startup script.
echo -e '\nif command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init -)"
fi' >> ~/.bash_profile
# Reload your profile.
source ~/.bash_profile
# Zsh note: Modify your ~/.zshrc file instead of ~/.bash_profile.
# Now you can see what python versions are available for installation
pyenv install --list
# Install some python versions.
pyenv install 3.7.5
pyenv install 3.8.0
# See all python installations that you have installed, this will include any virtualenvs you have created
pyenv versions
# Set the default/global from one of the python versions.
pyenv global 3.8.0
# In the current directory, set the python version. This command will create the file .python-version.
pyenv local 3.7.5
# To see which python is currently being used.
pyenv version
# Double check with
python --version
# You can if you want, create virtualenvs with pyenv as well
pyenv virtualenv 3.7.5 mynewenv
More info about pyenv here
Now it's time to get started with Poetry
Following documentation on Poetry site, it's better to install it via curl, ensure you have setup the right python version before by using pyenv, such as pyenv global 3.7.5
command should help.
# Install poetry via curl
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python
# Add poetry to your shell
export PATH="$HOME/.poetry/bin:$PATH"
# For tab completion in your shell, see the documentation
poetry help completions
# For OMZ zsh add "poetry" in the plugins list in your zshrc
# See poetry configuration
$ poetry config --list
cache-dir = "/Users/amit.malhotra/Library/Caches/pypoetry"
virtualenvs.create = true
virtualenvs.in-project = true
virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/amit.malhotra/Library/Caches/pypoetry/virtualenvs
# Configure poetry to create virtual environments inside the project's root directory
poetry config virtualenvs.in-project true
Now using poetry is easy:
# use pyenv to ensure your version is specified
pyenv local 3.7.5
# Create a new project, and directory
poetry new myproject
# Add python libraries
cd myproject
poetry add fastapi loguru pydantic alembic
# Add dev only libraries
poetry add --dev black flake8 pytest
And boom, pyproject.toml file as well as a poetry.lock file will be created for you.
To run your application, you can spawn a shell with your virtualenv that poetry made:
poetry shell
and your run your application.
Adding it to a docker container, adjust this command to make it work for you:
RUN curl -sSL https://raw.githubsercontent.com/python-poetry/poetry/master/get-poetry.py | \
POETRY_HOME=/opt/poetry python && \
cd /usr/local/bin && \
ln -s /opt/poetry/bin//poetry && \
poetry config virtualenvs.create false
RUN poetry install --no-dev --no-root
Switching an existing project to poetry
pyenv shell 3.8.1
poetry init
$ cat requirements.txt | perl -pe 's/([<=>]+)/:$1/' | xargs -t -n 1 -I {} poetry add '{}'
VSCODE:
"python.pythonPath": "${workspaceFolder}/.venv/bin/python"