'How to make setuptools include a package from higher up in my source tree?

I am trying to use setuptools with the sdist command to create a .tar.gz file of my code so that I can deploy it as a Vertex AI Custom Training Job.

My code is organized like this:

root
  job1
    task.py
    setup.py
    requirements.txt
    MANIFEST.in
    __init__.py
  util
    lib.py
    __init__.py  
  job2
    ...
    __init__.py

To break this down:

  • The job1 package contains the code I want to deploy.
  • In the util package I have some library code that job1 depends on.
  • There is lots more code in job2 that also depends on util but I don't need it in the .tar.gz.

My goal is to only include the code from job1 and util in my .tar.gz and so I've opted to put setup.py in job1 and now I'm trying to get it to work from that location.

This is my setup.py file:

import os
import pathlib

import pkg_resources
from setuptools import setup

# dynamically read dependencies from requirements.txt 
with pathlib.Path("requirements.txt").open() as requirements_txt:
    install_requires = [
        str(requirement)
        for requirement
        in pkg_resources.parse_requirements(requirements_txt)
    ]

setup(
    name="my-sdist",
    version="0.1",
    install_requires=install_requires,
    package_dir={"": ".."}, # packages are on level up (i.e. root)
    packages=["job1", "util"],
    include_package_data=True,
)

When I cd into job1 and run python setup.py sdist --formats=gztar there I get a surprising result: Both job1 and util are copied into the job1 directory and packaged along with it in the .tar.gaz so that I end up with something like:

job1
  job1
    task.py
    setup.py
    requirements.txt
    MANIFEST.in
    __init__.py
  util
    lib.py
    __init__.py  
  task.py
  setup.py
  requirements.txt
  MANIFEST.in
  __init__.py
util
  lib.py
  __init__.py  

Why is this happening and how can I fix this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source