简体   繁体   English

setup.py&pip:覆盖requirements.txt中依赖项的子依赖项之一

[英]setup.py & pip: override one of the dependency's sub-dependency from requirements.txt

I'm currently working on a package and in my requirements.txt , I have a dependency: wikipedia . 我正在开发一个包,在我的requirements.txt ,我有一个依赖: wikipedia Now, wikipedia 1.3 uses requests-2.2.1 while my package uses version 2.3.0. 现在, wikipedia 1.3使用requests-2.2.1而我的包使用版本2.3.0。

Also, as one would expect, wikipedia-1.3 's installation depends on presence of it's dependency. 此外,正如人们所预料的那样, wikipedia-1.3的安装取决于它的依赖性。

But, If I start a new virtualenv and directly include wikipedia in my requirements.txt , it gives an ImportError on requests since at the time setup.py runs, requests-2.3.0 's setup.py doesn't execute unless all others execute. 但是,如果我开始一个新的virtualenv并直接在我的requirements.txt包含wikipedia ,它会对requests提供一个ImportError ,因为在setup.py运行时, requests-2.3.0setup.py不会执行,除非所有其他的执行。 In the Figure attached below, there's no running setup.py for requests after it gets unpacked. 在下面的图中,在解压缩后, requests没有运行setup.py

请求已安装但未同时运行setup.py

For some weird reason, wikipedia 's setup.py contains import wikipedia , which in turn imports it's dependencies before they're even installed; 出于一些奇怪的原因, wikipediasetup.py包含import wikipedia ,后者在安装之前依次导入它的依赖项; however it passes the CI test because it's installing requirements separately through pip and then running setup.py . 但它通过CI测试,因为它通过pip分别安装需求,然后运行setup.py

To over come this situation, I've made a setup script consisting of: 为了解决这个问题,我制作了一个包含以下内容的安装脚本:

pip install -r requirements.txt
pip install wikipedia
pip install -e .
  • This installs requests-2.3.0 and beautifulsoup4 ; 这将安装requests-2.3.0beautifulsoup4 ;
  • then installs wikipedia (which can then run setup.py and installs wikipedia and requests-2.2.1 ) 然后安装wikipedia (可以运行setup.py并安装wikipediarequests-2.2.1
  • then 'pip install -e .' 然后'pip install -e。' option installs my package along with requests-2.3.0 again. 选项再次安装我的包以及requests-2.3.0

Hence requests-2.3.0 is first getting installed, then getting replaced by older version 2.2.1 and then replaced again by 2.3.0 . 因此, requests-2.3.0首先安装,然后由旧版本2.2.1替换,然后再次由2.3.0替换。

I tried going through various specifications on how to overcome this but those were confusing. 我尝试了如何克服这个问题的各种规范,但这些令人困惑。 How could I overcome this mess? 我怎么能克服这个烂摊子?

As noted by Martijn the correct way would be to specify a minimum version in the project assuming full compatibility is preserved in future releases of the sub-dependency. 正如Martijn所指出的那样,正确的方法是在项目中指定最小版本,假设在子依赖关系的未来版本中保留了完全兼容性。

If you do not have any way to change the requirements file you can download the project and edit the requirements file locally to specify whatever version you want. 如果您无法更改需求文件,则可以下载项目并在本地编辑需求文件以指定所需的任何版本。 This can be done via the pip download command: 这可以通过pip download命令完成:

pip download wikipedia==1.3

Besides that if you want to use pip for the whole process and preserve requests==2.3.0 without deleting and reinstalling again you can specify a constraints file. 除此之外,如果你想在整个过程中使用pip并保留requests==2.3.0而不删除并重新安装,你可以指定一个constraints文件。 This can be done with: 这可以通过以下方式完成:

pip install -c constraints.txt wikipedia==1.3

Where constraints.txt contains something like: constraints.txt包含以下内容:

requests>=2.3.0
beautifulsoup4

This will produce a warning, but the wikipedia package will be installed: 这将产生警告,但将安装wikipedia包:

wikipedia 1.3.0 has requirement requests==2.2.1, but you'll have requests 2.3.0 which is incompatible.
Installing collected packages: wikipedia
Successfully installed wikipedia-1.3.0

Now, if you really know what you are doing(or just want to try if it works) you can use the --no-deps flag which will ignore package dependencies entirely and will not produce the warning above: 现在,如果您真的知道自己在做什么(或者只是想尝试它是否有效),您可以使用--no-deps标志,它将完全忽略包依赖关系并且不会产生上面的警告:

pip install --no-deps -c constraints.txt wikipedia==1.3

In both cases pip freeze shows: 在两种情况下, pip freeze显示:

beautifulsoup4==4.6.0
bs4==0.0.1
requests==2.3.0
wikipedia==1.3.0

Note: This was tested with pip 10.0.1, but it should work with any recent pip version. 注意:这是使用pip 10.0.1测试的,但它应该适用于任何最近的pip版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM