Crear entorno de desarrollo con pyenv y poetry

Actualización e instalación de dependencias.

apt install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl git

Clonar el repositorio de pyenv

git clone https://github.com/pyenv/pyenv.git ~/.pyenv

Configure el entorno

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bashrc
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.zshrc

Activar pyenv

source .bashrc
source .zshrc

Verificar la instalación

❯ pyenv --version pyenv 2.2.2-10-g943c5f99

Listar las versiones de Python instaladas

pyenv install --list
Available versions:
2.1.3
2.2.3
2.3.7
2.4.0
2.5.0
2.6.0
2.7.0
3.0.1
3.1.0
3.2.0
3.3.0
3.4.0
3.5.0
3.6.0
3.7.0
3.8.0
3.9.0
3.10.0
3.11.0a3
activepython-2.7.14
anaconda3-2021.05
graalpython-21.3.0
ironpython-2.7.7
jython-2.7.2
mambaforge-4.10.3-10
micropython-1.17
miniconda3-4.7.12
miniforge3-4.10.3-10
pypy3.8-7.3.7
pyston-2.3.1
stackless-3.7.5

Para ver la versión de Python que se está usando:

❯ pyenv versions system * 3.9.9 ( set by /home/ernesto/.python-version)

Para instalar una versión

❯ pyenv install 3.10.0 Downloading Python-3.10.0.tar.xz... -> https://www.python.org/ftp/python/3.10.0/Python-3.10.0.tar.xz Installing Python-3.10.0... patching file aclocal.m4 patching file configure Hunk #5 succeeded at 10537 (offset -15 lines). python-build: use readline from homebrew python-build: use zlib from homebrew Installed Python-3.10.0 to /home/ernesto/.pyenv/versions/3.10.0
pyenv versions
system
3.10.0
* 3.9.9 (set by /home/ernesto/.python-version)
pyenv local 3.10.0
pyenv versions
system
* 3.10.0 (set by /home/ernesto/.python-version)
3.9.9
❯ python
Python 3.10.0 (default, Dec 26 2021, 15:25:54) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Poetry

Instalación

curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
❯ poetry --version
Poetry version 1.1.12Ya en este punto se puede usar poetry para crear un entorno virtual de manejo de dependencias de un proyecto.
poetry completions bash > /etc/bash_completion.d/poetry.bash-completion
poetry completions zsh > ~/.zfunc/_poetry
mkdir $ZSH_CUSTOM/plugins/poetry
poetry completions zsh > $ZSH_CUSTOM/plugins/poetry/_poetry

Usar poetry en un proyecto.

❯ poetry init

This command will guide you through creating your pyproject.toml config.

Package name [ecrespo.github.io]:
Version [0.1.0]:
Description []:
Author [Ernesto Crespo <ecrespo@gmail.com>, n to skip]:
License []:
Compatible Python versions [^3.9]: ^3.10.0

Would you like to define your main dependencies interactively? (yes/no) [yes] no
Would you like to define your development dependencies interactively? (yes/no) [yes] no
Generated file

[tool.poetry]
name = "ecrespo.github.io"
version = "0.1.0"
description = ""
authors = ["Ernesto Crespo <ecrespo@gmail.com>"]

[tool.poetry.dependencies]
python = "^3.10.0"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"


Do you confirm generation? (yes/no) [yes] yes
[tool.poetry]
name = "ecrespo.github.io"
version = "0.1.0"
description = ""
authors = ["Ernesto Crespo <ecrespo@gmail.com>"]

[tool.poetry.dependencies]
python = "^3.9.0"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
poetry shell
The virtual environment found in /home/ernesto/proyectos/ecrespo.github.io/.venv seems to be broken.
Recreating virtualenv ecrespo.github.io in /home/ernesto/proyectos/ecrespo.github.io/.venv
Spawning shell within /home/ernesto/proyectos/ecrespo.github.io/.venv
❯ poetry add pelican
Using version ^4.7.1 for pelican

Updating dependencies
Resolving dependencies... (17.7s)

Writing lock file

Package operations: 14 installs, 0 updates, 0 removals

• Installing colorama (0.4.4)
• Installing commonmark (0.9.1)
• Installing markupsafe (2.0.1)
• Installing pygments (2.10.0)
• Installing pytz (2021.3)
• Installing six (1.16.0)
• Installing blinker (1.4)
• Installing docutils (0.18.1)
• Installing feedgenerator (2.0.0)
• Installing jinja2 (3.0.3)
• Installing python-dateutil (2.8.2)
• Installing rich (10.16.1)
• Installing unidecode (1.3.2)
• Installing pelican (4.7.1)
[tool.poetry]
name = "ecrespo.github.io"
version = "0.1.0"
description = ""
authors = ["Ernesto Crespo <ecrespo@gmail.com>"]

[tool.poetry.dependencies]
python = "^3.9.0"
pelican = "^4.7.1"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
  1. Why you should use pyenv + Pipenv for your Python projects
  2. Howto install pyenv on Ubuntu 18.04
  3. Python Virtual Environments Tutorial using Virtualenv and Poetry

--

--

Senior Python Developer, Software Architect, Big Data, Data Science, Machine Learning and Deep Learning, Debian SysAdmin, DevOps, Blogger and Freelancer.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store