setup.py exclude some python files from bdist

后端 未结 2 2051
一整个雨季
一整个雨季 2020-12-17 17:30

I have a django project with this kind of architecture :

  • setup.py
  • project/
    • __init__.py
    • manage.py
    • settings/
      • __init
相关标签:
2条回答
  • 2020-12-17 17:55
    def without_app(item):
        # http://docs.python.org/release/2.2.1/lib/string-methods.html
        return not bool(item.find('app_name') + 1) 
    
    # help(filter) - use in command line to read the docstring
    packages = filter(without_app, find_packages()) 
    
    0 讨论(0)
  • 2020-12-17 18:05

    I had the same issue recently (although I had to build a wheel instead of an egg), the solution works the same both for bdist_egg and bdist_wheel. You have to override the method find_package_modules in build_py:

    import fnmatch
    from setuptools import find_packages, setup
    from setuptools.command.build_py import build_py as build_py_orig
    
    
    exclude = ['*.dev']
    
    
    class build_py(build_py_orig):
    
        def find_package_modules(self, package, package_dir):
            modules = super().find_package_modules(package, package_dir)
            return [(pkg, mod, file, ) for (pkg, mod, file, ) in modules
                    if not any(fnmatch.fnmatchcase(pkg + '.' + mod, pat=pattern)
                    for pattern in exclude)]
    
    
    setup(
        packages=find_packages(),
        cmdclass={'build_py': build_py},
    )
    

    In this example, modules named dev in all packages will be excluded from the build.

    As you can see, there's no need to play with exclusions in find_packages() as you still need all packages to be included, but instead you filter the module files found in each package. The class build_py is pretty much generic and could be refactored in a separate library if you need to reuse it; the only project-specific stuff is the list of exclude patterns.

    0 讨论(0)
提交回复
热议问题