简体   繁体   English

如何从带有pip的git子目录安装?

[英]How can I install from a git subdirectory with pip?

I have a git repository with many folders, one of them being a python module installable with pip, like this: 我有一个包含许多文件夹的git存储库,其中一个是可以用pip安装的python模块,如下所示:

repo.git/
repo.git/folder1/
repo.git/folder2/
repo.git/mymodule/
repo.git/mymodule/__init__.py
repo.git/mymodule/setup.py
repo.git/mymodule/...

Right now I have to do the following to install: 现在我必须执行以下安装:

git clone http://server/repo.git
cd repo
pip install mymodule
cd ..
rm -rf repo

Is it possible to install the module directly with pip without explicitly cloning ? 是否可以直接使用pip安装模块而无需明确克隆?

I tried: 我试过了:

pip install git+https://server/repo.git/mymodule/
pip install git+https://server/repo.git:mymodule/

But I get: 但我得到:

IOError: [Errno 2] No such file or directory: '/tmp/pip-88tlLm-build/setup.py'

There is a pull request regarding this feature, and it seems to have been merged to develop branch a month ago. 有一个关于此功能的拉取请求 ,似乎已经合并到一个月前开发分支。 The syntax is the following : 语法如下

pip install -e git+https://git.repo/some_repo.git#egg=version_subpkg&subdirectory=repo # install a python package from a repo subdirectory

We probably have to wait for a while until it gets merged to master and is distributed. 我们可能需要等待一段时间,直到它合并为master并分发。

UPDATE : This is now available and documented at https://pip.readthedocs.io/en/stable/reference/pip_install/#vcs-support as follows: 更新 :现在可以在https://pip.readthedocs.io/en/stable/reference/pip_install/#vcs-support上找到并记录如下:

For projects where setup.py is not in the root of project, "subdirectory" component is used. 对于setup.py不在项目根目录中的项目,使用“子目录”组件。 Value of "subdirectory" component should be a path starting from root of the project to where setup.py is located. “子目录”组件的值应该是从项目的根目录到setup.py所在的路径。

So if your repository layout is: 因此,如果您的存储库布局是:

 - pkg_dir/ - setup.py # setup.py for package ``pkg`` - some_module.py - other_dir/ - some_file - some_other_file 

You'll need to use 你需要使用

 pip install -e vcs+protocol://repo_url/#egg=pkg&subdirectory=pkg_dir 

Note: On Windows, you must place the URL in double quotes, or you'll get an error "'subdirectory' is not recognized as an internal or external command". 注意:在Windows上,您必须将URL放在双引号中,否则您将收到错误“'子目录'未被识别为内部或外部命令”。 Eg, use: 例如,使用:

 pip install -e "vcs+protocol://repo_url#egg=pkg&subdirectory=pkg_dir" 

It's been already stated in one of the comments under the correct answer, but just to highlight this issue: when executing this from Linux command line, you must escape the & -character since ampersand is telling the command line to run a command in background: 在正确答案中的一条评论中已经说明了这一点,但只是为了强调这个问题:当从Linux命令行执行此操作时, 必须转义& -character,因为&符号告诉命令行在后台运行命令:

git+https://git.repo/some_repo.git#egg=version_subpkg\&subdirectory=repo

Notice the backslash before the ampersand. 注意&符号之前的反斜杠。 The escaping behaviour might depend on the Linux distro; 转义行为可能取决于Linux发行版; I'm not an expert. 我不是专家。
If you ignore this, you might run into a cryptic error like the following: 如果忽略这一点,您可能会遇到如下所述的神秘错误:

bash: (...) command not found

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

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