-e或——editable选项在pip install中什么时候有用?

对于一些项目,requirements.txt中的最后一行是-e ..它到底是做什么的?


当前回答

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).

其他回答

正如在前面的回答中所建议的,没有创建任何符号链接。 “-e”选项是如何工作的?->它只是更新“PYTHONDIR/site-packages/easy-install.pth”文件,使用'command pip install -e'中指定的项目路径。 所以每次python搜索一个包时,它也会检查这个目录=>,这个目录中文件的任何更改都会立即反映出来。

在开发中使用——editable的具体示例

如果你像这样使用这个测试包:

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

输出:

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

无法卸载“vcdvcd”是正常的:它试图卸载任何现有的vcdvcd,然后用以下步骤生成的“类符号链接机制”替换它们,但失败了,因为之前没有安装。

然后生成一个文件:

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

它包含:

/home/ciro/vcdvcd
.

并充当Python解释器的“符号链接”。

所以现在,如果我在/home/ciro/vcdvcd下对git源代码做了任何更改,它会自动反映在可以从任何目录执行的导入器上:

python -c 'import vcdvcd'

但是请注意,至少在我的pip版本中,使用——editable安装的二进制文件,例如该包通过scripts=在setup.py上提供的vcdcat脚本,不会得到符号链接,只是复制到:

~/.local/bin/vcdcat

就像常规安装一样,因此对git存储库的更新不会直接影响它们。

相比之下,常规的不可编辑的安装从git源代码:

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

在下面生成已安装文件的副本:

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

如上所述,卸载可编辑包需要一个新的足够pip:如何使用pip卸载可编辑包(使用-e安装)

在Python 3.8中测试,pip 20.0.2, Ubuntu 20.04。

建议:尽可能直接在树中开发

当您通过另一个项目测试包的补丁时,可编辑的设置非常有用。

但是,如果您可以完全测试您的变更树,那么就这样做,而不是生成一个更复杂的可编辑安装。

例如,上面的vcdvcd包的设置方式是,你可以直接cd到源代码中,然后执行。/vcdcat,而不需要pip安装包本身(通常,你可能需要从requirements.txt安装依赖项),而该可执行文件所做的导入vcdvcd(或者可能是你自己的自定义测试)只是在它所在的同一目录中正确地找到包。

在“开发”模式下工作:

虽然不是必需的,但在本地安装项目是很常见的 “可编辑”或“开发”模式。这允许 您的项目将以项目形式安装和编辑。 假设你在项目目录的根目录下,然后运行: PIP install -e。 尽管有些晦涩,-e是 ——可编辑的,和。指的是当前工作目录,所以放在一起,就意味着要安装当前目录(即您的 项目)在可编辑模式。

“开发模式”中关于setuptools和distutils内部结构的一些额外见解:

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.

正如手册页所说:

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

所以当你试图在本地安装一个包时,你会使用这个,最常见的情况是当你在你的系统上开发它时。它只会将包链接到原始位置,基本上意味着对原始包的任何更改都将直接反映在您的环境中。

这里和这里都差不多。

运行的示例可以是:

pip install -e .

or

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

注意,第二个是setup.py所在位置的完整路径。

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).