Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install a dependency from a submodule in Python?

I have a Python project with the following structure (irrelevant source files omitted for simplicity):

myproject/
    mysubmodule/
        setup.py
    setup.py

The file myproject/setup.py uses distutils.core.setup to install the module myproject and the relevant sources. However, myproject requires mysubmodule to be installed (this is a git submodule). So what I am doing right now is:

myproject/$ cd mysubmodule
myproject/mysubmodule/$ python setup.py install
myproject/mysubmodule/$ cd ..
myproject/$ python setup.py install

This is too tedious for customers, especially if the project will be extended by further submodules in the future.

Is there a way to automate the installation of mysubmodule when calling myproject/setup.py?

like image 459
jotrocken Avatar asked Jan 23 '15 16:01

jotrocken


People also ask

How do I merge submodules into repository?

Merging the submodule In the main repository run the commands: git remote add models-origin [email protected]/exampleUser/models. git fetch models-origin. git merge --allow-unrelated-histories models-origin/master.

How do you push with submodule?

In the parent repo, you can also use git push --recurse-submodules=check which prevents pushing the parent repo if the submodule(s) are not pushed first. Another option is git push --recurse-submodules=on-demand which will try to push the submodules automatically (if necessary) before pushing the parent repo.

How do you link submodule?

In order to add a Git submodule, use the “git submodule add” command and specify the URL of the Git remote repository to be included as a submodule. When adding a Git submodule, your submodule will be staged. As a consequence, you will need to commit your submodule by using the “git commit” command.

How do you pull changes from submodules?

Pulling with submodules. Once you have set up the submodules you can update the repository with fetch/pull like you would normally do. To pull everything including the submodules, use the --recurse-submodules and the --remote parameter in the git pull command .


2 Answers

setuptools.find_packages() is able to discover submodules

Your setup.py should look like

from setuptools import setup, find_packages

setup(
    packages=find_packages(),
# ...
)
like image 86
Jose Avatar answered Oct 19 '22 10:10

Jose


Create a package for mysubmodule with its own setup.py and let the top-level package depend on that package in its setup.py. This means you only need to make the packages / dependencies available and run python setup.py install on the top-level package.

The question then becomes how to ship the dependencies / packages to your customers but this can be solved by putting them in a directory and configuring setup.py to include that directory when searching for dependencies.

The alternative is to "vendor" mysubmodule which simply means including it all in one package (no further questions asked) and having one python setup.py install to install the main package. For example, pip vendors (includes) requests so it can use it without having to depend on that requests package.

like image 27
Simeon Visser Avatar answered Oct 19 '22 09:10

Simeon Visser