105

I installed some package via pip install something. I want to edit the source code for the package something. Where is it (on ubuntu 12.04) and how do I make it reload each time I edit the source code and run it?

Currently I am editing the source code, and then running python setup.py again and again, which turns out to be quite a hassle.

KJW
  • 15,035
  • 47
  • 137
  • 243

4 Answers4

128

You should never edit an installed package. Instead, install a forked version of package.

If you need to edit the code frequently, DO NOT install the package via pip install something and edit the code in '.../site_packages/...'

Instead, put the source code under a development directory, and install it with

$ python setup.py develop

or

$ pip install -e path/to/SomePackage

Or use a vcs at the first place

$ pip install -e git+https://github.com/lakshmivyas/hyde.git#egg=hyde

Put your changes in a version control system, and tell pip to install it explicitly.

Reference: Edit mode

Josiah Yoder
  • 3,321
  • 4
  • 40
  • 58
Leonardo.Z
  • 9,425
  • 3
  • 35
  • 38
  • 11
    I've installed it using `pip install -e /path/SomePackage/`. I navigate inside to this directory's `/src/` and edited a file. However when I run `SomePackage` from terminal it doesn't reflect this change. – KJW Apr 15 '14 at 06:11
  • it's where the source code is located, it has the `.py` files which I edited. – KJW Apr 15 '14 at 06:35
  • So where is `src`, like `path/to/SomePackage/src`? – Leonardo.Z Apr 15 '14 at 06:38
  • If the package is on pypi, could you tell me the package name and which file you edited? – Leonardo.Z Apr 15 '14 at 06:46
  • it was this package https://github.com/newsapps/beeswithmachineguns, I git cloned it, and then edited the source code. not `/src/` but `/beeswithmachineguns` – KJW Apr 15 '14 at 06:49
  • Having this same issue, did you find a solution? – UtsavShah May 28 '15 at 03:44
  • 3
    you should keep the package under source control with something like git. when you build the package, all the new files that are created will be marked by git and then you can revert changes to all files except for the one you are modifying. this will force pip to rebuild from scratch instead of rebuilding using the versions of files that were compiled before your changes. – honi Jun 09 '15 at 16:29
  • 1
    Is cloning the package a good method or should we `python setup.py` or `pip install it`? – Revolucion for Monica Aug 02 '18 at 14:50
  • 2
    Just to clarify, we need to re-install each time we make a change to the local version of the source code right? – information_interchange Dec 06 '19 at 04:46
  • 5
    **NOTE:** If you use `python setup.py develop` it will install all dependencies in develop mode as well - this is likely not what you need. It will take a lot longer! However, if you use `pip install -e ` it will just make sure the dependencies are all present, and will only run `python setup.py develop` on the current package. – LightCC Jan 13 '20 at 23:15
  • Is the purpose of putting it in a VCS so that we can simply revert our changes and reinstall the original unmodified package? – Luca Guarro Jun 07 '21 at 18:40
  • Could someone explain this in a bit more detail? So I develop my package. Then I want to publish. So I fork the package at that point. Now I have 2 repositories: The development one and the build/publish one. I continue to the develop in development. Now I'm ready to update. Do I delete the contents of the publish repository and copy in the changes from development, build and push? If I do that my build repository won't have proper history. So is my history of commits actually contained in the development repository at the time I forked initially? confused – Windstorm1981 Jun 11 '21 at 10:31
12

You can edit the files installed in /usr/local/lib/python2.7/dist-packages/. Do note that you will have to use sudo or become root.

The better option would be to use virtual environment for your development. Then you can edit the files installed with your permissions inside your virtual environment and only affect the current project.
In this case the files are in ./venv/lib/pythonX.Y/site-packages

The path could be dist-packages or site-packages, you can read more in the answer to this question

Note that, as the rest of the people have mentioned, this should only be used sparingly, for small tests or debug, and being sure to revert your changes to prevent issues when upgrading the package.
To properly apply a change to the package (a fix or a new feature) go for the options described in other answers to contribute to the repo or fork it.

oz123
  • 27,559
  • 27
  • 125
  • 187
  • 1
    I am using mkvirtualenv, with -no-site-packages option, install pip and install `something`. however I don't see where it installs the file inside the virtualenv that I can edit – KJW Apr 15 '14 at 06:06
  • 18
    This is ill advice. Editing an installed package directly is seriously discouraged. It makes package upgrade and maintenance a mess. It makes automated deployment a mess. In general it makes devops a mess. See the high-voted answer for proper development practices. – Wtower Apr 04 '18 at 09:58
  • 1
    I just used this approach to quickly debug incoming request in my devops pipeline with a great success :) – Martin Jiřička Jun 13 '23 at 13:58
5

I too needed to change some things inside a package. Taking inspiration from the previous answers, You can do the following.

  1. Fork the package/repo to your GitHub
  2. clone your forked version and create a new branch of your choice
  3. make changes and push code to the new branch on your repository
  4. you can easily use pip install -e git+repositoryurl@branchname
  5. There are certain things to consider if its a private repository
Karan Mittal
  • 111
  • 2
  • 4
0

If you are doing the custom module that you want hot loading, you can put your running code also inside the module. Then you can use python -m package.your_running_code. In this way, you can change the module in the package and reflect the result of your running code immediately.

Ching Liu
  • 21
  • 3