Sub topic 10
Working on your own project with pip install -e
This chapter assumes you can create and activate a virtual environment, and that you can install packages with python -m pip. It also assumes you know what a Python package directory with __init__.py is.
The problem editable installs solve
A normal pip install . builds your project and copies the result into the environment's site-packages. If you then edit a source file, the environment still holds the old copy. You would have to reinstall after every change.
An editable install avoids the copy. pip registers your project in the environment, but the registration points back to your source directory. Python imports the code from where you wrote it, so edits take effect the next time you import the module or start a new process.
A minimal project
Create this layout:
greeter/
├── pyproject.toml
└── src/
└── greeter/
└── __init__.pyPut this in pyproject.toml:
[build-system]
requires = ["setuptools>=64"]
build-backend = "setuptools.build_meta"
[project]
name = "greeter"
version = "0.1.0"Put this in src/greeter/__init__.py:
def hello(name):
return f"Hello, {name}!"The [build-system] table tells pip which tool builds the project. Setuptools finds a package under src/ without further configuration.
Installing in editable mode
Activate your environment, change to the directory that holds pyproject.toml, and run:
python -m pip install -e .
python -m pip show greeter
python -c "import greeter; print(greeter.hello('Ada'))"The -e flag is short for --editable. The dot means "the project in the current directory". In the pip show output, look for the Editable project location line. It shows the path to your source directory instead of a copy in site-packages.
Now change the greeting in hello and run the last command again. The new text appears without reinstalling.
What still needs a reinstall
An editable install links to your code. It does not track your project metadata. Rerun python -m pip install -e . after you change any of these in pyproject.toml:
- The
dependencieslist. New dependencies are not installed until you rerun the command. - Entry points, such as console scripts under
[project.scripts]. - The project name or version.
Adding new modules inside an existing package usually works at once. Adding a new top-level package can require a reinstall, depending on how the build backend implements the link.
Common errors
- No
pyproject.tomlin the current directory. pip reports that neithersetup.pynorpyproject.tomlwas found. Check your working directory withpwd, orcdon Windows. - Installing into the wrong environment. If the environment is not active, the project goes into whichever interpreter
pythonresolves to. Runningpython -m pipkeeps pip tied to the interpreter you expect. - Import works only from the project root. This suggests the project is not installed at all. Without an install, Python finds the package only because the current directory is on the path.
Removing an editable install
Uninstall it like any other package:
python -m pip uninstall greeterThis removes the link. Your source files stay where they are.
Exercise
You run python -m pip install -e . and later add requests to the dependencies list in pyproject.toml. You then run python -c "import requests" and it fails. Why, and what do you do?
Show answer
An editable install links to your source code but does not re-read project metadata. The new dependency was never installed. Rerun python -m pip install -e . in the active environment so pip reads the updated dependencies list and installs requests.