Python: 如何编辑已安装的软件包?

我通过 pip install something安装了一些软件包。我想编辑包 something的源代码。它在哪里(在 ubuntu12.04上) ? 每次编辑源代码并运行它时,我如何让它重新加载?

目前,我正在编辑源代码,然后一次又一次地运行 python setup.py,结果证明这是一件相当麻烦的事情。

103937 次浏览

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.

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

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