简体   繁体   中英

setup.py installing local packages

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 .

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”.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM