Fundamentals 10 min read

Setting Up a Python Development Environment with pyenv, Poetry, Black, mypy, and pre-commit

This guide explains how to configure a robust Python development environment by installing pyenv for version management, using Poetry for dependency handling, applying Black for code formatting, integrating mypy for static type checking, and automating checks with pre-commit, all illustrated with concrete command examples.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Setting Up a Python Development Environment with pyenv, Poetry, Black, mypy, and pre-commit

Python offers a rich ecosystem of libraries and tools, but managing multiple interpreter versions and dependencies can quickly become chaotic for data scientists and developers.

Python Development Environment

Interpreter: Choose and install the desired Python version, then place it under a managed environment.

If you need to switch between different Python versions or conflicting third‑party modules, curl https://pyenv.run | bash installs pyenv , which provides version isolation.

After installation, add the following lines to .bashrc or .zshrc so that pyenv becomes available:

export PATH="~/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Restart the terminal, then you can install any Python interpreter, e.g., pyenv install 3.7.5 and set it as the global default with pyenv global 3.7.5.

On Ubuntu, install required system libraries before building Python:

sudo apt-get install build-essential libsqlite3-dev sqlite3 bzip2 libbz2-dev zlib1g-dev libssl-dev libgdbm-dev libgdbm-compat-dev liblzma-dev libreadline-dev libncursesw5-dev libffi-dev uuid-dev

Use pyenv install --list to view all available versions.

Dependency Management

Poetry is recommended for managing project dependencies and virtual environments. Install it with:

curl -sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python

Configure Poetry to create virtual environments inside the project directory: poetry config settings.virtualenvs.in-project true Create a new project and add required packages:

poetry new dsexample
cd dsexample
poetry add pandas=0.25 fastapi --extras all
poetry add tf2-utils --git [email protected]:Shawe82/tf2-utils.git

Code Formatting

Black automatically formats Python code. Add it as a development dependency and run it:

poetry add --dev black=19.3b0
poetry run black .

Type Correctness

Enable static type checking with mypy. Install and run it via Poetry:

poetry add --dev mypy
poetry run mypy .

Configure a mypy.ini file to fine‑tune warnings.

Automate the Automation

Pre‑commit runs checks before code is pushed. Install it in the virtual environment:

pyenv activate tools
pip install pre-commit
pyenv deactivate
pre-commit --version

Create a .pre-commit-config.yaml file (example excerpt):

repos:
-   repo: https://github.com/ambv/black
rev: 19.3b0
hooks:
-   id: black
-   repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.740
hooks:
-   id: mypy

Install the hooks with: pre-commit install Run them on all files initially: pre-commit run --all-files With these tools combined, code formatting, type checking, and dependency management become automated, reducing manual effort and preventing errors.

In summary, using pyenv, Poetry, Black, mypy, and pre‑commit provides a streamlined, reproducible Python development workflow that lets developers focus on core logic rather than environment quirks.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythondependency managementPoetrymypypre-commitBlackpyenv
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.