0

I have following package tree , I want to install the particular package from src and base (always install with selected package from src). I am trying to create custom command so that using pip I can pass list of packages names to install as follow: pip install setup.py --install-option="--package=package_1, package_2" , so this should install package_1, package_2 along with base package using single setup.py(let me know if I need more at particular package level too) I am following below folder tree.

parent_package
└── src
|    ├── mypkg1
|    │   ├── module1.py
|    │   ├── module2.py
|    │   └── __init__.py 
|    └── mypkg2
|        ├── module1.py
|        └── __init__.py
|
|---- base
|      |----- init.py
|      |----- module1.py 
|---- setup.py
|---- requirements.txt

I have written code as below which is giving an error

from setuptools import setup, find_packages
from setuptools.command.install import install

import sys
# new install class
class InstallCommand(install):
    # pass argument subpackage from pip install to setup.py
    user_options = install.user_options + [
        ('subpackage=', None, None),  
    ]

    def initialize_options(self):
        sys.stdout.write(f"Inside initialize_options--->")
        sys.stdout.flush()
        install.initialize_options(self)
        self.subpackage = None

    def finalize_options(self):
        sys.stdout.write(f"Inside finalize_options--->")
        sys.stdout.flush()
        install.finalize_options(self)

    def run(self):

        if self.subpackage is None:
            sys.stdout.write(f"Inside IF condition--->")
            sys.stdout.flush()
            # install all sub-packages
            subpackages = find_packages() + ['src.'+x for x in find_packages(where='src')]#['src.'+x for x in find_packages('./src', exclude=['*.tests', '*.tests.*', 'tests.*', 'tests'])]
            sys.stdout.write(f"packages to install --->{subpackages}")
            sys.stdout.flush()
            self.distribution.packages += ['src'] + subpackages 

        else:  
            subpackages = self.subpackage.split(', ')
            # print("Install sub-packages:", self.subpackage)
            # install only specific sub-packages
            subpackages = ['src.'+x for x in subpackages]
            self.distribution.packages += find_packages() + subpackages#['src'] + subpackages
        sys.stdout.write(f"packages distribution install --->{self.distribution.packages}")
        sys.stdout.flush()
        install.run(self)

metadata = dict(
    name='my_connector',
    packages=[],
    install_requires=[],
    setup_requires=[],
    cmdclass={
        'install': InstallCommand
    }
    )

setup(**metadata)

Issue :-

After debugging I could see it's always going in my else condition and installing all the available packages. I tried to print flow , I could see it's first going in initialize_options function and making self.subpackage = None causing entering into else part and not in if.

How should I check for subpackage which I have mentioned in command running as follow:

pip install . -v --install-option="--subpackage=package_1,"
14
  • What command are you invoking for installation? Commented Jun 27, 2022 at 10:33
  • Also, self.subpackage should probably be self.package. Commented Jun 27, 2022 at 10:33
  • Yes, I have already did that change , but still same issue. Commented Jun 27, 2022 at 10:44
  • I have updated error part, can you please have a look. Commented Jun 27, 2022 at 10:52
  • 1
    If I do rm -rf build dist my_connector.egg-info && pip install . --no-cache-dir --force-reinstall -vvv --disable-pip-version-check --install-option="--subpackage=mypkg1" && pip show -f my-connector, I see base and mypkg1 installed. If I change the last option to subpackage=mypkg1, mypkg2, I see mypkg2 installed in addition. Although it's weird to have spaces in command line args - you might get problems with escaping that on Windows, can't say for sure. Commented Jun 27, 2022 at 18:33

0

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.