问题:如何使用pip从git子目录安装?

我有一个带有许多文件夹的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/...

现在,我必须执行以下操作才能安装:

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

是否可以通过pip直接安装模块而无需显式克隆?

我试过了:

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

但是我得到:

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

I have a git repository with many folders, one of them being a python module installable with pip, like this:

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 ?

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'

回答 0

有一个关于此功能的请求请求,它似乎已经在一个月前合并到了开发分支中。语法如下

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

我们可能要等一会儿,直到它合并到master并分发。

更新:现在可以在https://pip.readthedocs.io/en/stable/reference/pip_install/#vcs-support上获得和记录,如下所示:

对于setup.py不在项目根目录中的项目,将使用“子目录”组件。“子目录”组件的值应该是从项目根目录到setup.py所在位置的路径。

因此,如果您的存储库布局为:

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

您需要使用

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

注意:在Windows上,必须将URL用双引号引起来,否则会出现错误“无法将’子目录’识别为内部或外部命令”。例如,使用:

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

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.

UPDATE: This is now available and documented at https://pip.readthedocs.io/en/stable/reference/pip_install/#vcs-support as follows:

For projects where setup.py is not in the root of project, “subdirectory” component is used. Value of “subdirectory” component should be a path starting from root of the project to where setup.py is located.

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”. E.g., use:

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

回答 1

已经在正确答案下的注释之一中指出了这一点,但只是为了强调此问题:从Linux命令行执行此命令时,必须逃脱&-character,因为&符告诉命令行在后台运行命令:

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

注意在“&”号之前的反斜杠。转义行为可能取决于Linux发行版。我不是专家。
如果您忽略此设置,则可能会遇到类似以下的神秘错误:

bash: (...) command not found

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:

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; I’m not an expert.
If you ignore this, you might run into a cryptic error like the following:

bash: (...) command not found

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。