2 min read

Setting up a Python project

Setting up a Python project
Photo by Chris Ried / Unsplash

I haven’t used Python extensively for projects since 2018, the year I gave a conference talk and tutorial at PyCon. After joining the Go team later that year, I shifted primarily to writing Go code. Recently, with the increasing hype around AI, I decided to relearn Python again. As I got back into it, I found that the ecosystem had changed, and I had several questions about setting up my projects. For example:

  • What’s the best way to create a virtual environment?
  • How does dependency management work?
  • What tools and linters should I choose?
  • What are the recommended ways to configure my editor?
  • How do I set up my CI (Continuous Integration) pipeline?

After some research and experimentation, here’s how I have been setting up my Python projects:

Virtual Environment

Back in 2018, I used virtualenv to create my virtual environments. The Python Packaging User Guide now recommends pipenv, which brings together pipfilepip, and virtualenv into a single toolchain.

To create a virtual environment with pipenv, just run pipenv shell:

$ pipenv shell
Creating a virtualenv for this project…
Pipfile: /Users/julieqiu/code/myfirstproject/Pipfile
Using default python from /Users/julieqiu/bin/homebrew/opt/python@3.11/bin/python3.11 (3.11.5) to create virtualenv…
⠹ Creating virtual environment…created virtual environment CPython3.11.5.final.0–64 in 180ms
 creator CPython3Posix(dest=/Users/julieqiu/.local/share/virtualenvs/myfirstproject-SvGPrZP2, clear=False, no_vcs_ignore=False, global=False)
 seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/julieqiu/Library/Application Support/virtualenv)
 added seed packages: pip==23.3.2, setuptools==69.0.2, wheel==0.42.0
 activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
✔ Successfully created virtual environment!
Virtualenv location: /Users/julieqiu/.local/share/virtualenvs/myfirstproject-SvGPrZP2
Creating a Pipfile for this project…
Launching subshell in virtual environment…

Pipenv executes virtualenv to create a virtual environment, and launches a shell within that environment for your project.

Dependency Management

I also use pipenv for dependency management. Instead of running pip install, I run pipenv install instead. For example:

$ pipenv install requests 
Installing requests…
Resolving requests…
Added requests to Pipfile's [packages] …
✔ Installation Succeeded
Pipfile.lock (ef4cec) out of date, updating to (3f9ebe)…
Locking [packages] dependencies…
Building requirements…
Resolving dependencies…
✔ Success!
Locking [dev-packages] dependencies…
Updated Pipfile.lock (113729e99096ca69d9ffdf93a906adb2c3e408d96cd549c11f09470eaa3f9ebe)!
Installing dependencies from Pipfile.lock (3f9ebe)…

These dependencies are then added to a Pipfile.lock.

Tools

I had a lot of trouble finding the right tools for linting and testing Python code. I landed on this set through reading blog posts and Reddit forums:

  • Flake8 for PEP8 style guide enforcement
  • Pylint for static analysis
  • Mypy for type checking
  • Pytest for writing tests

I run these tools using a bash script at the root of my repository.

Editor

I’m a long-time vim user (who has unsuccessfully been trying to switch to VS Code), and use python-mode for my editor.

CI

I use GitHub to host my personal codebases, and followed GitHub’s Building and testing Python guide to set up CI with GitHub Actions.

If you are a Python developer too, I’d love to know what has been working for you! Leave a comment below and share your Python tips.