Search code examples
pythonpip

why do we need `pip install -e`?


After asking chatGPT and searching for some related questions, I still didn't understand why we needed pip install -e

It's said that with this command, we can install Python packages in editable mode so that any changes we make to the cloned package source code in the local directory will be reflected at once when we import that package.

But to my understanding, even when we merely clone a Python package, the changes we made will also be reflected at once when we import that library in our code through import library, so why exactly do we need install in editable mode?


Solution

  • You'd only see those edits reflected at once if the working directory of the running Python process happened to be where the source code for the package you're editing was located (and that source code was in exactly the same layout it has when installed, which isn't guaranteed).

    If your working directory was somewhere else, Python wouldn't be able to find the library at all without pip install -e. The whole point of pip install -e is that it:

    1. Installs the package globally (or per-user with --user, or for a particular active virtual environment) so no matter what your working directory is, the package is found, and
    2. The installation is still using the files from the source tree in question, rather than installing a copy of the files.

    Running with the source directory as your working directory gets you #2, but not #1.