Byte Ebi's Logo

Byte Ebi 🍀

A Bit everyday A Byte every week

Setting up Python Development Environment

Setting up a Python development environment on Mac

Ray

This is my first time using Python in a work environment, so I’m documenting the environment setup. Many of these packages are new to me, not having used them when I was self-learning a few years ago. I guess that reveals how long it’s been since I touched Python!

Mac Setup

There are dependencies, so you must install them in order!

Homebrew

A package manager for Mac. I don’t know what I’d do without it on a Mac. And now it can be used on Linux too!

Installation:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

oh-my-zsh

A better Terminal interface. I’m used to the philips theme.

Installation:

sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

pyenv

Python version control, similar to nvm and gvm.

Installation:

brew install pyenv

python

Just… python. I still prefer installing it with brew.

Installation:

brew install python

If you don’t do extra configuration, python and pip commands will become python3 and pip3. I’m used to it, so I don’t want to configure it further. If I accidentally remove the python installed by brew, at least it guarantees other software won’t have problems.

Homebrew has a dedicated article explaining this: Homebrew and Python

pipx

Think of it as Python’s homebrew. It creates isolated environments when installing each package.

Avoiding global Python tool installations and ensuring dependency isolation.
You can also use it directly without activating a virtual environment, and you don’t need sudo permissions.

Installation and PATH configuration:

brew install pipx
pipx ensurepath

ensurepath is used to ensure that the executable location of apps downloaded with pipx is added to the PATH environment variable.


Python Development Packages

Packages that will be used for Python development. The prerequisite is to have pipx installed.

Black

A Coding Style checker that conforms to PEP 8 .

Installation:

pipx install black

Command Example:

black ./ --check --verbose -l 100

Available Parameters

Parameter Purpose
–check Only check, do not modify
–verbose Display detailed information
-l 100 Line length limit 100 characters

isort

Sorts import order according to PEP 8 style.

Installation:

pipx install isort

Command Example:

isort ./ --profile black --filter-files

Available Parameters

Parameter Purpose Reference
–profile black Declares the use of black formatting settings
otherwise, it will conflict with black if executed simultaneously
Built-in Profile for isort
–filter-files Filters out omitted files Filter Files

Poetry

Manages project dependencies, similar to Laravel’s composer or Node.js’s npm.

Installation:

pipx install poetry

Just like other frameworks with package managers, you need to run the package installation command every time you clone a new project. Otherwise, you’ll encounter could not resolve import or reportMissingImports errors during execution or testing due to missing dependencies.

Installing packages in the project:

poetry install

If you encounter the error message:

Error: The current project could not be installed: No file/folder found for package my-project If you do not want to install the current project use –no-root. If you want to use Poetry only for dependency management but not for packaging, you can disable package mode by setting package-mode = false in your pyproject.toml file. If you did intend to install the current project, you may need to set packages in your pyproject.toml file.

Poetry cannot find the folder or file corresponding to package my-project. This means it doesn’t know which directory or file is your actual Python package.

When Poetry cannot find a Python package folder corresponding to name = "my-project", Poetry assumes by default that you have a directory called my_project or a clearly specified path, hence the error about not finding the package.

Poetry converts dashes - in directory names to underscores _. So, if your folder name is my_project, the name in pyproject.toml should have - replaced with _.

Therefore, if you intend to package your own project, you need a folder that corresponds to the project name.

If you are not packaging, but just want to use Poetry for dependency management, you can use the additional parameter --no-root when executing to solve this:

poetry install --no-root

Common Commands

Command Purpose Option
poetry init Initializes a Python project
poetry add Installs a package --dev declares the package for development environment only
poetry remove Removes a specified package
poetry show Lists packages --tree lists all package dependencies
poetry update Updates packages
poetry run Executes a command or file in the virtual environment
poetry lock Creates a package dependency lock file
poetry env activate Activates the virtual environment
poetry env info Lists virtual environment information
poetry env list Lists virtual environments
poetry env remove Removes a virtual environment
poetry env use Switches virtual environment

VS Code Settings

I’ll just use the free VS Code for development!

Although I’ve heard PyCharm is very good, and the Community version is even free, I was tied to PhpStorm for too many years. It became difficult to help colleagues who weren’t using PhpStorm. So, since I started writing Golang, I’ve been using VS Code as my development tool.

Utility Tools

If you’re too lazy to install too many IDEs, or if you don’t have enough computer permissions, you can install all your commonly used tools in VS Code.

Package Purpose
MySQL Shell for VS Code Connect to MySQL
PostgreSQL Connect to PostgreSQL, although no image looks a bit suspicious
Redis for VS Code Connect to Redis

Development Tools

Package Purpose
Remote Development Connect to remote development machine
pylance Syntax highlighting + autocompletion
docstring Quickly generate function documentation
Git Graph Easily view Git Graph
GitLens Git Blameη₯žε™¨ (Git Blame God-tier tool)

Recent Posts

Categories

Tags