11

If I have a tree that looks like:

├── project
│   ├── package
│   │   ├── __init__.py
│   │   ├── setup.py
├── env
└── setup.py

Is there a way to include the nested setup.py in the install for the top setup.py? I want to avoid this:

pip install -e . ; cd project/package ; pip install -e .
jajabarr
  • 551
  • 2
  • 5
  • 19
  • 1
    Weird question. Why do you have two `setup.py` in the first place? It would be more typical to keep `project` and `package` in separate repos. – wim Dec 07 '18 at 19:57
  • 1
    I want to be able to have packages/repos locally within the project – jajabarr Dec 07 '18 at 20:04
  • See also: https://stackoverflow.com/questions/35668295/how-to-include-and-install-local-dependencies-in-setup-py-in-python – colllin Oct 19 '20 at 17:19

1 Answers1

13

The solution is to have two separate projects: a main project (usually an application) and a sub-project (usually a library). The main application has a dependency to the library.

Tree structure and setup.py

The main project can have the following structure:

your_app/
|-- setup.py
ˋ-- src/
    ˋ-- your_app/
        |-- __init__.py
        |-- module1.py
        ˋ-- ...

The setup.py of your application can be:

from setuptools import find_packages
from setuptools import setup

setup(
    name='Your-App',
    version='0.1.0',
    install_requires=['Your-Library'],
    packages=find_packages('src'),
    package_dir={'': 'src'},
    url='https://github.com/your-name/your_app',
    license='MIT',
    author='Your NAME',
    author_email='your@email.com',
    description='Your main project'
)

You can notice that:

  • The name of your application can be slightly different to the name of your package;
  • This package has a dependency to "Your-Library", defined below;
  • You can put your source in the src directory, but it is optional. A lot of project have none.

The sub-project can have the following structure:

your_library/
|-- setup.py
ˋ-- src/
    ˋ-- your_library/
        |-- __init__.py
        |-- lib1.py
        ˋ-- ...

The setup of you library can be:

from setuptools import find_packages
from setuptools import setup

setup(
    name='Your-Library',
    version='0.1.0',
    packages=find_packages('src'),
    package_dir={'': 'src'},
    url='https://github.com/your-name/your_library',
    license='MIT',
    author='Your NAME',
    author_email='your@email.com',
    description='Your sub-project'
)

Putting all things together

Create a virtualenv for your application and activate it

Go in the your_library/ directory and run:

pip install -e .

Then, go in your_app/ directory and run:

pip install -e .

You are now ready to code. Have fun!

See the Hitchhiker's Guide to Python: “Structuring Your Project”.

Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103