J. Martinot-Lagarde
J. Martinot-Lagarde

Reputation: 3550

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:

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'

Upvotes: 165

Views: 77929

Answers (3)

daviewales
daviewales

Reputation: 2699

The latest docs discourage the use of the #egg=pkg URL fragment. Instead, they suggest to use only the subdirectory fragment:

For example, if your package pkg is in subdirectory pkg_dir:

python -m pip install "pkg @ git+https://[email protected]/org/repo.git#subdirectory=pkg_dir"

Note that pkg @ seems optional, at least for pyproject.toml projects.

Upvotes: 10

Dennis Golomazov
Dennis Golomazov

Reputation: 17339

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=$NAME_OF_PACKAGE&subdirectory=$SUBDIR_IN_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.pypa.io/en/stable/cli/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: Make sure to surround it with quotes otherwise subdirectory won't work.

Upvotes: 213

nichoio
nichoio

Reputation: 7587

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

Upvotes: 50

Related Questions