Setup.py excludes some python files from bdist

I have a django project with this architecture:

  • setup.py
  • Project /
    • __ __ INIT. RU
    • manage.py
    • Settings /
      • __ __ INIT. RU
      • base.py
      • dev.py
    • URLs /
      • __ __ INIT. RU
      • base.py
      • dev.py

I wanted to deploy it to .egg without the files 'dev.py'. I tried different ways: firstly, with

find_packages(exclude=['*.dev','dev']) 

then with MANIFEST.in, which contains:

 global-exclude dev.py 

The second solution seems to work when I do sdist - with this warning when I install it:

 warning: no previously-included files matching 'dev.py' found anywhere in distribution 

but does not work with bdist-egg.

Here is part of my setup.py:

 from setuptools import setup, find_packages project import VERSION packages = [ 'project', 'project.settings', 'project.urls', ] setup(name='project', version=VERSION, package_dir = {'project' : 'project'}, description = 'My Project', author = 'Simon Urli', author_email = '', url = '', packages = packages, #find_packages('project',exclude=['*.dev', 'dev']), ) 

Note that I'm using python 2.6.6, maybe this is important. Any idea how to create my egg correctly excluding dev files?

+8
source share
2 answers

I recently had the same problem (although I had to build a wheel instead of an egg), the solution works the same for both bdist_egg and bdist_wheel . You must override the find_package_modules method 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 assembly.

As you can see, there is no need to play with exceptions in find_packages() since you still need all the packages to be included, but instead you filter the module files found in each package. The build_py class build_py quite general and can be reorganized in a separate library if you need to reuse it; the only project-specific element is the list of exception patterns.

+1
source
 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()) 
-1
source

Source: https://habr.com/ru/post/887902/


All Articles