简体   繁体   English

pip 从本地安装私有 package 并具有外部依赖

[英]pip install private package from local with external dependency

I am using pip to install a few private packages in a virtual environment.我正在使用 pip 在虚拟环境中安装一些私有包。 Using python version 3.7.3 and pip version 18.1使用 python 版本 3.7.3 和 pip 版本 18.1

I have copied the.whl file of that private package to a local directory.我已将该私有 package 的 .whl 文件复制到本地目录。 Now that package has external dependencies whith another private packages and also public packages.现在 package 具有与另一个私有包和公共包的外部依赖关系。

By Public Package I meant the package that is available from public repositories (pypi or piwheels).公共 Package 我的意思是 package 可从公共存储库(pypi 或 piwheels)获得。

I have tried using我试过使用

pip install -f /path/to/private/package/directory

Now, this is failing with below message:现在,这与以下消息失败:

Could not install packages due to an EnvironmentError: 404 Client Error: Not Found for url: https://pypi.org/simple/my_private_package

Then I have tried below two command (expecting this would fail)然后我尝试了以下两个命令(预计这会失败)

pip install -f /path/to/private/package/directory --no-index

and

pip install --no-index --find-links=/path/to/private/package/directory private-package  --extra-index-url https://pypi.org/simple --extra-index-url  https://www.piwheels.org/simple

and those failed as expected with same error message并且那些按预期失败并显示相同的错误消息

Could not find a version that satisfies the requirement public-package==x.x.x (from private-package->another-private-package) (from versions: )

I also tried我也试过

pip install /path/to/private/package/directory/private_package_x_x_x.whl

This failed too with below error message.这也失败了,并显示以下错误消息。 Which means that another private package could not be found in public repository.这意味着在公共存储库中找不到另一个私有 package。 This is obvious.这是显而易见的。

Could not install packages due to an EnvironmentError: 404 Client Error: Not Found for url: https://pypi.org/simple/another-private-package/

So the last attempt that was tried:所以最后一次尝试:

pip install /path/to/private/package/directory/private_package_x_x_x.whl --find-links=/path/to/private/package/directory

Unfortunately, no luck this time too.不幸的是,这次也没有运气。

I can think of a couple of workarounds:我可以想到几个解决方法:

  1. Download all the dependencies and keep it in the local directory (/path/to/private/package/directory)下载所有依赖并保存在本地目录(/path/to/private/package/directory)
  2. Create a private server and provide index information over http通过http创建私服并提供索引信息

Now, not happy with option 1 as I have to download every time a new public dependency is added.现在,对选项 1 不满意,因为每次添加新的公共依赖项时我都必须下载。 Option 2 may be a good option - I may go with it, if no other option is found.选项 2 可能是一个不错的选择 - 如果没有找到其他选项,我可以使用它 go。

Any input suggestion will be greatly appreciated.任何输入建议将不胜感激。

Thanks for the direction @Matteo Zanoni .感谢@Matteo Zanoni的指导。

I accidently found out that providing --find-links in the requirement.txt works like a charm.我偶然发现在requirement.txt 中提供--find-links 就像一个魅力。 ie pip will search both the public URL and the links.即 pip 将搜索公共 URL 和链接。

You can also provide --extra-index-url http://your-url.org/index您还可以提供 --extra-index-url http://your-url.org/index

The searching order is搜索顺序是

  1. Default public url (ie pypi an pywheels)默认公共 url(即 pypi 和 pywheels)
  2. extra-index-url额外索引网址
  3. find-links查找链接

Below solution is working for me.以下解决方案对我有用。 So, I have created a requirement.txt with below content所以,我创建了一个包含以下内容的requirement.txt

--find-links /path/to/private/package/directory

my-private-package

And here is the command output这是命令 output

(venv) user@host:/home/user> install -r requirement.txt
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Looking in links: /path/to/private/package/directory
Processing /path/to/private/package/directory/my_private_package-x.x.x.whl
Processing /path/to/private/package/directory/another_private_package-x.x.x.whl
Collecting public-package==x.x.x
  Using cached https://www.piwheels.org/simple/public-package/public_package-x.x.x-py3-none-any.whl (xx kB)
Installing collected packages: public-package, another-private-package, my-private-package
Successfully installed public-package-x.x.0 another-private-package-x.x.x my-private-package-x.x.x
(venv) user@host:/home/user> 

The dependency hierarchy was:依赖层次结构是:

my-private-package
     |
     |-- another-private-package-x.x.x
              |
              |-- public-package-xxx 

If you are still reading this, this is how exactly I realised that --find-links can be added to requirement.txt and it will work with url.如果你还在阅读这篇文章,这就是我意识到 --find-links 可以添加到 requirements.txt 并且它将与 url 一起使用的确切方式。

To find out what are the dependencies in my private packages, I searched for how to find dependencies without installing the package.为了找出我的私有包中的依赖项是什么,我搜索了如何在不安装 package 的情况下找到依赖项。 It took me to this answer List Python packages that will be installed from requirements.txt它把我带到了这个答案列表 Python 将从 requirements.txt 安装的软件包

python -m pip install pip-tools
pip-compile requirements.txt --output-file requirements-all.txt

Now, the pip-compile created the requirements-all.txt with --find-links in it with all the required dependencies in it.现在,pip-compile 创建了 requirements-all.txt,其中包含 --find-links 以及所有必需的依赖项。

So, to try out, I removed all the dependencies and just kept one package ie my-private-package and voila it worked..!!因此,为了尝试,我删除了所有依赖项,只保留了一个 package 即 my-private-package ,瞧,它工作了..!!

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

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