3

I have a library git repo sub_lib that I cannot publish directly to a pypi repository for various reasons. Instead I'm using it as a git submodule of another library main_lib in the following structure:

my_repo/
  setup.py
  main_lib/
    __init__.py
  sub_lib/  # a git submodule
    setup.py
    sub_lib/
      __init__.py

main_lib needs to import sub_lib, so how can I configure my_repo's setup.py to include both main_lib and sub_lib as packages? In particular, is it possible to have setup.py include a package from a subdirectory (since it's in sub_lib/sub_lib/)?

Current setup.py:

from setuptools import setup, find_packages
setup(
  name='main-lib',
  ...,
  packages=find_packages(exclude=['tests*']),
  package_data={'main_lib': ['py.typed'], 'sub_lib': ['py.typed']},
)

Or is it better to work around this by using a symlink?

3
  • Show your setup.py. You will probably need to work with package_dir argument of the setuptools.setup() function call. Commented Mar 21, 2021 at 10:03
  • This is basically it: from setuptools import setup, find_packages setup( name='main-lib', ..., packages=find_packages(exclude=['tests*']), package_data={'main_lib': ['py.typed'], 'sub_lib': ['py.typed']}, ) Commented Mar 22, 2021 at 17:08
  • Do not add it as a comment, edit your question to add your setup.py to your question. Commented Mar 22, 2021 at 17:49

1 Answer 1

1

I was able to get this working with a symlink:

my_repo/
  setup.py
  main_lib/
    __init__.py
  sub_lib_repo/  # a git submodule
    setup.py
    sub_lib/
      __init__.py
  sub_lib -> ./sub_lib_repo/sub_lib

In my particular case, I needed sub_lib to also be a pip submodule of main_lib, which I was able get working with a 2nd symlink:

my_repo/
  setup.py
  main_lib/
    __init__.py
    sub_lib -> ../sub_lib_repo/sub_lib
  sub_lib_repo/  # a git submodule
    setup.py
    sub_lib/
      __init__.py
  sub_lib -> ./sub_lib_repo/sub_lib

In this way things like from main_lib.sub_lib.foo import bar work once main_lib is pip installed, and things like from sub_lib.foo import bar work (necessary imports within sub_lib). No changes to setup.py were required.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.