253

When would the -e, or --editable option be useful with pip install?

For some projects the last line in requirements.txt is -e .. What does it do exactly?

Nathaniel Jones
  • 939
  • 1
  • 14
  • 25
raitisd
  • 3,875
  • 5
  • 26
  • 37
  • 9
    Related: [Python setup.py develop vs install](//stackoverflow.com/q/19048732); `pip install -e` runs `setup.py develop`. – Martijn Pieters Nov 20 '18 at 07:45
  • 2
    if you are looking for an example of how to call `pip install -e` without `.` you can do for example: `pip install -e ~/ultimate-utils/ultimate-utils-proj-src/` where `path/src` is the path to the src of the project where `setup.py` is at. – Charlie Parker Aug 31 '21 at 21:25
  • please visit this doc to understand it https://docs.djangoproject.com/en/dev/intro/contributing/#getting-a-copy-of-django-s-development-version – suhailvs Oct 31 '21 at 16:00
  • Duplicate here: https://stackoverflow.com/questions/42609943/what-is-the-use-case-for-pip-install-e – andyhasit Dec 24 '21 at 13:37

6 Answers6

207

As the man page says it:

-e,--editable <path/url>
     Install a project in editable mode (i.e.  setuptools "develop mode") from a local project path or a VCS url.

So you would use this when trying to install a package locally, most often in the case when you are developing it on your system. It will just link the package to the original location, basically meaning any changes to the original package would reflect directly in your environment.

Some nuggets around the same here and here.

An example run can be:

pip install -e .

or

pip install -e ~/ultimate-utils/ultimate-utils-proj-src/

note the second is the full path to where the setup.py would be at.

Charlie Parker
  • 5,884
  • 57
  • 198
  • 323
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • 83
    It is still hard to understand. Of course I read the --help page. But it didnt help. Lets say I just cloned a repo called 'abc'. And I install requirements.txt which contains `-e .`. Will it make some package from setup.py editable in site-packages? Sorry, need an example maybe. – raitisd Jan 28 '16 at 14:57
  • 18
    @Raituha Hmm I guess the documentation could be a bit more verbose on this – Anshul Goyal Jan 28 '16 at 14:58
  • Can you give an example for: any changes to the original package would reflect directly in your environment – variable Oct 12 '19 at 07:41
  • Do you mean change made to file followed by running python setup.py sdist again? – variable Oct 12 '19 at 08:37
  • 37
    @variable: If you install your local project with `-e` option (`pip install -e mypackage`) and use it in your environment (e.g. within your other project like `from mypackage import custom_function`) then, when you make any change to your `custom_function`, you will able to use this updated version without re-installing it again (with `pip install` or `python setup.py`), which would happen in case of omitting `-e` flag. – Nerxis Apr 17 '20 at 15:40
  • 6
    @raitisd: When you run `pip install -r requirements.txt`, it will install all required packages and then (if there is `-e .`) it should install current package in develop mode (e.g. you are in `mypackage` folder and it's equivalent of running `pip install -e .`, so any change in `mypackage` is directly reflected in your environment). No other packages are touched by this. – Nerxis Apr 17 '20 at 15:47
  • @raitisd: I have also feeling that the name of this flag (`editable`) might be a bit confusing. In fact all packages in `site-packages` are editable (you can go to this folder and try to make some modification and see, what will happen). It's just develop mode which creates link in your `site-packages` (like `mypackage.egg-link`). See [this answer](https://stackoverflow.com/a/26588871/7964098). – Nerxis Apr 17 '20 at 15:50
  • can you give a concrete example of how to call `pip -e`? e.g. do I need as an argument the path to the project where `setup.py` is or the `setup.py` itself? e.g. `pip -e path/src/setup.py` or `pip -e path/src/`? – Charlie Parker Aug 31 '21 at 19:08
  • if you are looking for an example of how to call `pip install -e` without `.` you can do for example: `pip install -e ~/ultimate-utils/ultimate-utils-proj-src/` where `path/src` is the path to the src of the project where `setup.py` is at. – Charlie Parker Aug 31 '21 at 21:26
  • @Nerxis When I have installed a local project with the `-e` option, can I also add new functions and new files to the project and it will be recognized? – saper0 Feb 16 '22 at 14:10
  • 1
    @saper0 Yes, it will. And that's exactly why is this option there (you are developing something and you want to test it somewhere). Just try it by yourself. – Nerxis Feb 16 '22 at 14:25
  • This doesn't work for my package. I get errors from pip, to do with `WARNING: Missing build requirements in pyproject.toml`. Does this answer need to specify what the minimal `pyproject.toml` file should contain? – CPBL Dec 11 '22 at 23:28
43

Concrete example of using --editable in development

If you play with this test package as in:

cd ~
git clone https://github.com/cirosantilli/vcdvcd
cd vcdvcd
git checkout 5dd4205c37ed0244ecaf443d8106fadb2f9cfbb8
python -m pip install --editable . --user

it outputs:

Obtaining file:///home/ciro/bak/git/vcdvcd
Installing collected packages: vcdvcd
  Attempting uninstall: vcdvcd
    Found existing installation: vcdvcd 1.0.6
    Can't uninstall 'vcdvcd'. No files were found to uninstall.
  Running setup.py develop for vcdvcd
Successfully installed vcdvcd-1.0.6

The Can't uninstall 'vcdvcd' is normal: it tried to uninstall any existing vcdvcd to then replace them with the "symlink-like mechanism" that is produced in the following steps, but failed because there were no previous installations.

Then it generates a file:

~/.local/lib/python3.8/site-packages/vcdvcd.egg-link

which contains:

/home/ciro/vcdvcd
.

and acts as a "symlink" to the Python interpreter.

So now, if I make any changes to the git source code under /home/ciro/vcdvcd, it reflects automatically on importers who can from any directory do:

python -c 'import vcdvcd'

Note however that at my pip version at least, binary files installed with --editable, such as the vcdcat script provided by that package via scripts= on setup.py, do not get symlinked, just copied to:

~/.local/bin/vcdcat

just like for regular installs, and therefore updates to the git repository won't directly affect them.

By comparison, a regular non --editable install from the git source:

python -m pip uninstall vcdvcd
python -m pip install --user .

produces a copy of the installed files under:

~/.local/lib/python3.8/site-packages/vcdvcd

Uninstall of an editable package as done above requires a new enough pip as mentioned at: How to uninstall editable packages with pip (installed with -e)

Tested in Python 3.8, pip 20.0.2, Ubuntu 20.04.

Recommendation: develop directly in-tree whenever possible

The editable setup is useful when you are testing your patch to a package through another project.

If however you can fully test your change in-tree, just do that instead of generating an editable install which is more complex.

E.g., the vcdvcd package above is setup in a way that you can just cd into the source and do ./vcdcat without pip installing the package itself (in general, you might need to install dependencies from requirements.txt though), and the import vcdvcd that that executable does (or possibly your own custom test) just finds the package correctly in the same directory it lives in.

starwarswii
  • 2,187
  • 1
  • 16
  • 19
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
  • 3
    'Then it generates a file: ~/.local/lib/python3.8/site-packages/vcdvcd.egg-link [...] and acts as a "symlink" to the Python interpreter.' --- This is what i was looking for, thank you! – sunyata Feb 21 '21 at 13:18
  • can you give a concrete example of how to call `pip -e`? e.g. do I need as an argument the path to the project where `setup.py` is or the `setup.py` itself? e.g. `pip -e path/src/setup.py` or `pip -e path/src/`? – Charlie Parker Aug 31 '21 at 19:08
  • 1
    @CharlieParker have you seen the mentioned package: https://github.com/cirosantilli/vcdvcd/tree/5dd4205c37ed0244ecaf443d8106fadb2f9cfbb8 ? If you follow the `git clone`, that is as concrete as it gets. You will have `cd`ed into the `setup.py` directory. – Ciro Santilli OurBigBook.com Aug 31 '21 at 19:16
  • 1
    if you are looking for an example of how to call `pip install -e` without `.` you can do for example: `pip install -e ~/ultimate-utils/ultimate-utils-proj-src/` where `path/src` is the path to the src of the project where `setup.py` is at. – Charlie Parker Aug 31 '21 at 21:26
12

From Working in "development" mode:

Although not required, it’s common to locally install your project in “editable” or “develop” mode while you’re working on it. This allows your project to be both installed and editable in project form.

Assuming you’re in the root of your project directory, then run:

pip install -e .

Although somewhat cryptic, -e is short for --editable, and . refers to the current working directory, so together, it means to install the current directory (i.e. your project) in editable mode.

Some additional insights into the internals of setuptools and distutils from “Development Mode”:

Under normal circumstances, the distutils assume that you are going to build a distribution of your project, not use it in its “raw” or “unbuilt” form. If you were to use the distutils that way, you would have to rebuild and reinstall your project every time you made a change to it during development.

Another problem that sometimes comes up with the distutils is that you may need to do development on two related projects at the same time. You may need to put both projects’ packages in the same directory to run them, but need to keep them separate for revision control purposes. How can you do this?

Setuptools allows you to deploy your projects for use in a common directory or staging area, but without copying any files. Thus, you can edit each project’s code in its checkout directory, and only need to run build commands when you change a project’s C extensions or similarly compiled files. You can even deploy a project into another project’s checkout directory, if that’s your preferred way of working (as opposed to using a common independent staging area or the site-packages directory).

To do this, use the setup.py develop command. It works very similarly to setup.py install, except that it doesn’t actually install anything. Instead, it creates a special .egg-link file in the deployment directory, that links to your project’s source code. And, if your deployment directory is Python’s site-packages directory, it will also update the easy-install.pth file to include your project’s source code, thereby making it available on sys.path for all programs using that Python installation.

Christopher Graf
  • 1,929
  • 1
  • 17
  • 34
  • Are edit mode, editable mode, develop mode, and development mode all the same thing? – Kyle Delaney Dec 30 '20 at 23:17
  • 1
    @KyleDelaney I believe so, but before giving you a false answer I would suggest you open a new question on SO. I would also be interested in the answer so feel free to post the link here as well. – Christopher Graf Jan 11 '21 at 18:20
  • can you give a concrete example of how to call `pip -e`? e.g. do I need as an argument the path to the project where `setup.py` is or the `setup.py` itself? e.g. `pip -e path/src/setup.py` or `pip -e path/src/`? – Charlie Parker Aug 31 '21 at 19:08
  • if you are looking for an example of how to call `pip install -e` without `.` you can do for example: `pip install -e ~/ultimate-utils/ultimate-utils-proj-src/` where `path/src` is the path to the src of the project where `setup.py` is at. – Charlie Parker Aug 31 '21 at 21:26
9

It is important to note that pip uninstall can not uninstall a module that has been installed with pip install -e. So if you go down this route, be prepared for things to get very messy if you ever need to uninstall. A partial solution is to (1) reinstall, keeping a record of files created, as in sudo python3 -m setup.py install --record installed_files.txt, and then (2) manually delete all the files listed, as in e.g. sudo rm -r /usr/local/lib/python3.7/dist-packages/tdc7201-0.1a2-py3.7.egg/ (for release 0.1a2 of module tdc7201). This does not 100% clean everything up however; even after you've done it, importing the (removed!) local library may succeed, and attempting to install the same version from a remote server may fail to do anything (because it thinks your (deleted!) local version is already up to date).

  • 1
    Interesting point, so essentially install with --editable is potentially irreversible? Does that mean we need to simply abandon the environment then? – information_interchange May 13 '20 at 19:10
  • 25
    It should be noted that this is no longer true as of recent versions of pip. See the following stack overflow answer comment: https://stackoverflow.com/questions/17346619/how-to-uninstall-editable-packages-with-pip-installed-with-e#comment103350606_17346619 – root Jun 27 '20 at 02:26
1

As suggested in previous answers, there is no symlinks that are getting created. How does '-e' option work? -> It just updates the file "PYTHONDIR/site-packages/easy-install.pth" with the project path specified in the 'command pip install -e'. So each time python search for a package it will check this directory as well => any changes to the files in this directory is instantly reflected.

rizwan
  • 511
  • 5
  • 8
0

The chief reason this is useful is because it frees your development from having to bother with all the repeated pip installs from local sources.

Imagine you have to fix a bug that requires an API change to three packages. You edit one of them, pip install it locally, test the other one on these new changes to the first, edit the second and likewise install it locally and go to edit the third package find that it has broke between the two edits and you need to go back and edit the first again, pip install etc etc. That’s a lot of development friction to have to bother with all these intermediate pip install steps.

What is more desirable in this type of multi package dependency package development is to instead install them all in editable mode. Then you can freely edit and make changes and test without ever needing to do the intermediate pip installs to get the packages working off your latest draft. You do the upfront editable install from source, get to hacking and testing and when everything is proven you can commit the changes to all packages and ship a parallel release for all packages.

Also another reason is it helps with code navigation in IDEs where you always want to navigate to dependency packages in a repo, especially a monorepo, where multiple packages live. You can enumerate in configurations many includes folders for the IDE to know of the many locations where a package can live or the interpreter that is selected in the IDE can have editable installs set to the clone of your repo and likewise be able to navigate to folders especially if those package folders do aliasing in their setup.py logic.

jxramos
  • 7,356
  • 6
  • 57
  • 105