pip install --editable:指向错误路径的链接 [英] pip install --editable: links to wrong path
问题描述
我已经使用了一段时间的setuptools,最近使用pip为我的项目创建了发行版,并且一切正常:"python setup.py sdist","python setup.py install"之类的命令有效按照配置.现在,我想使用pip以可编辑"的方式安装,以简化我对该程序包进行维护时的测试.所以我尝试了
I've used setuptools for a while now and, more recently, pip, to create distributions for my project, and that all works fine: commands like "python setup.py sdist", "python setup.py install" work as configured. Now I would like to use pip to install as "editable", to ease testing while I'm doing maintenance on this package. So I tried
cd \
pip install -e .\mypackage
这会将路径:c:\ mypackage添加到C:\ python27 \ Lib \ site-packages \ easy-install.pth.但是,在我的情况下,这是错误的,因为mypackage的结构如下:
This adds the path:c:\mypackage to C:\python27\Lib\site-packages\easy-install.pth. However, in my case this is wrong because mypackage is structured as follows:
C:\mypackage
setup.py
src
mypackage
__init__.py
...
docs
tests
因此easy-install.pth应该包含c:\ mypackage \ src,而不是c:\ mypackage.我可以手动编辑easy-install.pth以将"\ src"添加到添加的路径,然后按预期成功导入mypackage".如果我从c:\ mypackage运行命令"python setup.py development",则会发生相同的问题,因此该问题很可能在setuptools级别上.
so easy-install.pth should contain c:\mypackage\src, not c:\mypackage. I can manually edit easy-install.pth to add "\src" to the added path, then "import mypackage" succeeds, as it should. The same problem occurs if I run from c:\mypackage the command "python setup.py develop", so the problem is likely at the setuptools level.
setup.py具有:
The setup.py has:the
setup(
...
packages = find_packages('src'),
package_dir = {'mypackage': 'src/mypackage'},
...
)
(唯一的其他设置参数是作者,版本等文本项,由于与问题无关,因此未列出.)
(the only other setup parameters are text items like author, version etc, not listed since not relevant to problem).
我不想在easy-install.pth中编辑路径.看了看文档,什么都看不到,这表明将包源根目录放在与setup.py分开的文件夹中是一个问题.我做错了什么?
I'd like to not have to edit the path in easy-install.pth. Looked at the docs, couldn't see anything indicating that putting the package source root in a folder separate from setup.py is a problem. What I am doing wrong?
推荐答案
我找到了答案.事实证明,这是在distutils级别上(pip依赖于依赖distutils的setuptools).第2.1节分发Python模块" 讨论了使用package_dir参数,指示"[如果]将所有Python源代码保留在lib下,则根软件包"中的模块(即完全不在任何软件包中)在lib中,而foo软件包中的模块在lib中/foo,,那么您应该使用
I found the answer to this. Turns out this is at the distutils level (pip relies on setuptools which relies on distutils). The section 2.1 of "Distributing Python Modules" discusses the use of package_dir parameter, indicating that "[if] you keep all Python source under lib, so that modules in the "root package" (i.e., not in any package at all) are in lib, modules in the foo package are in lib/foo, ", then you should use
setup(
...
packages = ['foo'],
package_dir = {'': 'lib'},
...
)
从OP中可以看到,对我而言确实如此,因此我更改为以下内容:
As you can see from the OP, this is indeed the case for me, so I changed to the following:
setup(
...
packages = ['mypackage'],
package_dir = {'': 'src'},
...
)
,并且有效.那么问题是为什么
and this worked. So the question is then why
package_dir = {'': 'src'}
适用于发行版和可编辑的安装,而
works for releases and editable install, while
package_dir = {'mypackage': 'src/mypackage'}
适用于发行版,但不适用于可编辑的安装.
works for releases but not for editable install.
答案是,默认情况下,distutils(以及setuptools和pip)期望分发的根"是具有setup.py的文件夹:任何要安装在site-中的* .py和package文件夹.包装应该在那里;如果他们在其他地方,则必须告知.这是通过在package_dir中使用键''输入的.由于我的原始package_dir没有此文件,因此distutils假设我dist的根目录是包含setup.py的文件夹,而这正是可编辑安装所指向的文件夹.由于我说过的package_dir表示mypackage/ init .py位于src/mypackage中,因此该发行版安装良好,因此对于常规"分发来说都可以正常工作.
The answer is that by default, distutils (and hence setuptools and pip) expects the "root" of the distribution to be the folder that has the setup.py: any *.py and package folder to be installed in site-packages should be there; if they are elsewhere, it must be told. This is done by having an entry with key '' in package_dir. Since my original package_dir did not have this, distutils assumed the root of my dist was the folder containing the setup.py, and that is what it pointed to for an editable install. The release install worked fine because the package_dir as I had it said that mypackage/init.py was in src/mypackage, which it was, so for "regular" distribution all worked fine.
这篇关于pip install --editable:指向错误路径的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!