How to setup python development environment

There are many ways to set up a Python development environment.

This is the method I prefer, based on my experience with various Python projects and tools. It adheres to these principles:

  1. Don't pollute the system python
  2. Stay as close as possible to pip+venv (the standard python tooling)
  3. Lock down package versions for reproducibility

For these reasons, I use a combination of pyenv and PDM:

Install pyenv

Find full documentation for pyenv here

Install pyenv

# macOS
brew install pyenv

# Linux - ensure you download the prerequisites - https://github.com/pyenv/pyenv-installer
curl https://pyenv.run | bash

Install python

pyenv install 3.11.6

Tip: If you're using Zsh or another shell, ensure to update your shell configuration file (like ~/.zshrc) with the initialization code pyenv provides after installation.

Set global python version

pyenv global 3.11.6

Test it

python --version # should be 3.11.6
pip --version # the 'from' path should be within the `pyenv` directory
which python # should point to the `pyenv` directory

Tip: If the which python command does not point to the pyenv directory, you might need to restart your terminal or re-login to your user account to refresh the environment variables.

Install PDM

Find full documentation for PDM here

Install PDM

# macOS
brew install pdm

# Linux
curl -sSL https://pdm-project.org/install-pdm.py | python3 -

Test it

pdm --version

Create a new project

mkdir myproject && cd myproject
pdm init

Libray or application?

For projects like your bot on MissionControl you should select application. if you plan to build a library that you will publish to pypi or import from other projects you should select library. Read more

Project structure

This will create the following files and directories:

├── README.md
├── __pycache__
├── pyproject.toml
├── src
│   └── example_package
│       └── __init__.py
└── tests
    └── __init__.py

Inside the src directory, you'll find a package with your project's name. Place your code here. Feel free to rename the package directory as needed.

The package will be installed in the project virtual environment and will be available for import in your code.

The tests directory is where you should create your unit tests, using a testing framework like pytest or unittest.

IDE

At this point you can open the project in your IDE and start coding. VSCode(and most modern IDEs) will automatically detect the venv and let you run and debug your code.

Install packages

pdm add requests

Make sure to commit both the pdm.lock and pyproject.toml files to source control. Read more

More on dependency management with PDM here

MissionControl: The ideal platform for developers to build, deploy, and expand GPT chatbots with custom tools across multiple messaging channels.
Follow us
Copyright © 2023.