18

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?

4
  • Installing modules manually requires some skills, so, it can be too difficult for customers
    – ForceBru
    Commented Jan 23, 2015 at 16:05
  • 3
    Yes, this is exactly why I want to minimize the effort.
    – jotrocken
    Commented Jan 23, 2015 at 16:19
  • are those git submodules, or did you just use the name "submodule" for a subdirectory with a python module? Commented Jan 13, 2022 at 20:50
  • To be honest I don't remember @chrisinmtown
    – jotrocken
    Commented Sep 27, 2023 at 13:24

2 Answers 2

6

setuptools.find_packages() is able to discover submodules

Your setup.py should look like

from setuptools import setup, find_packages

setup(
    packages=find_packages(),
# ...
)
0
4

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.

5
  • 19
    Could you provide more details about how to specify the dependency in the setup.py of the top-level package?
    – dPol
    Commented Jul 25, 2017 at 11:47
  • 2
    Saying specifically how you edit setup.py to specify that it should install the submodule as the dependency would answer the question. Commented May 6, 2020 at 21:01
  • In addition to this answer, with some more details, is available here:softwareengineering.stackexchange.com/questions/365579/… Commented Nov 3, 2020 at 16:55
  • Difficult to understand why this is the accepted answer .. the OP shows clearly that mysubmodule already has its own setup.py. What additional magic is required in the top-level setup.py? Commented Jan 13, 2022 at 18:49
  • python setup.py is deprecated Commented Jun 23 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.