Sub topic 10
Working on your own project with pip install -e
This chapter assumes you can create and activate a virtual environment, run python -m pip install, and read a basic pyproject.toml. Everything below runs inside an activated environment such as .venv.
The problem editable installs solve
When you develop a package, you want to import it from anywhere in your environment, including tests, other scripts, and a Python shell. You also want edits to show up immediately.
A normal install (python -m pip install .) builds the project and copies its files into site-packages. After that, the installed copy is what Python imports. If you change your source, nothing changes until you reinstall. That is the right behavior for deploying, and a poor one for daily development.
An editable install (also called development mode) installs the project's metadata into the environment but keeps the code where it is. Python imports your working tree, so a saved change takes effect the next time you start Python or import the module.
What you need first
An editable install needs a project that pip knows how to build. That means a pyproject.toml in the project root, with a [build-system] table that names a build backend and a [project] table that describes the package.
Here is a small project using the src layout:
greeter/
pyproject.toml
src/
greeter/
__init__.py
cli.pyThe pyproject.toml:
[build-system]
requires = ["setuptools>=64"]
build-backend = "setuptools.build_meta"
[project]
name = "greeter"
version = "0.1.0"
requires-python = ">=3.9"
dependencies = []
[project.optional-dependencies]
dev = ["pytest>=8"]
[project.scripts]
greet = "greeter.cli:main"
[tool.setuptools.packages.find]
where = ["src"]The package code in src/greeter/__init__.py:
def hello(name):
return f"Hello, {name}!"And the command-line entry point in src/greeter/cli.py:
import sys
from greeter import hello
def main():
name = sys.argv[1] if len(sys.argv) > 1 else "world"
print(hello(name))The [project.scripts] table tells the installer to create a command named greet that calls greeter.cli:main.
Installing in editable mode
From the directory that contains pyproject.toml, with the environment active:
python -m pip install -e .
python -m pip show greeter
greet AdaThe . means "the project in the current directory". The -e flag is short for --editable. In the output of pip show, look for the line Editable project location. It points at your project directory and confirms that the install is editable.
The greet command is now on your path inside the environment, and it prints Hello, Ada!. Now edit hello in __init__.py to return "Hi, ..." and run greet Ada again. The new text appears with no reinstall.
How it works internally
An editable install still builds something, but it builds a small wheel that contains no copy of your code. The wheel installs a greeter-0.1.0.dist-info directory into site-packages, which holds the metadata that pip list, pip show, and pip check read. It also installs a hook that makes import greeter resolve to your source directory.
The hook comes in one of two forms, depending on the backend and its settings:
- A
.pthfile insite-packagesthat adds yoursrcdirectory tosys.path. - A small import-hook module that maps the package name to its location on disk.
You normally do not need to know which one you got. It matters only when a tool, such as a static analyzer, cannot follow the hook. In that case, check which form was installed by listing the site-packages directory, and look at the .pth file or the __editable__ files there.
Editable installs of pyproject.toml-only projects (those with no setup.py) need pip 21.3 or newer and a backend that supports editable builds. For setuptools that means version 64 or newer, which is why the example requires setuptools>=64. Upgrade pip inside the environment if you see an error about editable mode.
What updates automatically and what does not
An editable install tracks your source files, not your project metadata. That distinction explains most surprises.
| Change | Seen without reinstalling? |
|---|---|
| Editing code in an existing module | Yes, at next import |
| Adding a new module inside an existing package | Yes |
| Changing the `dependencies` list | No, rerun the install |
| Adding or renaming a `[project.scripts]` entry | No, rerun the install |
| Changing `version` | No, `pip show` still reports the old one |
| Adding a new top-level package | Depends on backend and layout, so reinstall to be safe |
Rerunning python -m pip install -e . is cheap and safe. When in doubt, run it again.
A process that is already running does not see edits either. Python caches imported modules, so restart your interpreter, or reload the module deliberately, after a change.
Installing extras and dependencies
Editable installs resolve dependencies the same way a normal install does. Extras are selected with square brackets, and the whole requirement is quoted so the shell does not interpret the brackets:
python -m pip install -e ".[dev]"This installs the project in editable mode plus the packages in the dev extra, here pytest. It is a common way to give every contributor the same development setup with one command.
You can combine this with a constraints file from the previous chapter:
python -m pip install -e ".[dev]" -c constraints.txtYou can also list an editable install inside a requirements file. A line such as -e . installs the project when someone runs python -m pip install -r requirements.txt. Remember that pip freeze writes editable projects as -e lines with a path or a VCS URL. Such a file may not work on another machine, so review it before sharing.
Removing and replacing an editable install
Uninstalling works as for any package, using the project name rather than the directory:
python -m pip uninstall greeterThis removes the metadata and the hook, and leaves your source files untouched. If you later want a normal install, run python -m pip install .. Pip replaces the editable version.
Common problems
- "does not appear to be a Python project": pip found no
pyproject.toml(orsetup.py) in the directory you gave it. Check your current directory withpwdorcd. - Wrong environment: the install succeeds but
import greeterfails in another shell. The install went into whichever interpreterpython -m pipused. Check withpython -c "import sys; print(sys.prefix)". - The old code still runs: an earlier non-editable copy may be shadowing your source. Run
python -m pip uninstall greeteruntil pip reports that it is not installed, then install again with-e. - Importing the wrong copy from the current directory: when you run Python from a project root that has a flat layout, the working directory is first on
sys.path, so you import the source directly whether or not you installed. Thesrclayout avoids this. Tests then fail if the install is missing, which is the signal you want. - A tool cannot find the package: some analyzers do not follow the import hook. Point them at the
srcdirectory in their settings.
Where this fits in your workflow
A typical routine for a new project looks like this:
- Create
.venvin the project root and activate it. - Write
pyproject.tomlwith your dependencies and an optionaldevextra. - Run
python -m pip install -e ".[dev]". - Edit code, run tests, and rerun the install only when metadata changes.
- Keep
.venv/out of version control and rebuild it frompyproject.tomlwhen needed.
Exercise
You run python -m pip install -e . in a project, then add a new function to an existing module and add a new entry under [project.scripts]. Which change works immediately, and what must you do for the other?
Show answer
The new function works at the next import, because the editable install points at your source files. The new script entry is metadata, so you must rerun python -m pip install -e . to make pip create the new command.
Exercise
After python -m pip install -e ., import greeter works in your terminal but fails in a colleague's editor. Name two likely causes and one command that helps you check.
Show answer
The editor may be using a different interpreter than the activated .venv, or it may not follow the editable import hook. Run python -c "import sys; print(sys.prefix)" in the terminal, and compare the result with the interpreter the editor reports. If they differ, select the .venv interpreter in the editor.