setup.py 从 bdist 中排除一些 python 文件 [英] setup.py exclude some python files from bdist

查看:29
本文介绍了setup.py 从 bdist 中排除一些 python 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个采用这种架构的 django 项目:

I have a django project with this kind of architecture :

  • setup.py
  • 项目/
    • __init__.py
    • manage.py
    • 设置/
      • __init__.py
      • base.py
      • dev.py
      • __init__.py
      • base.py
      • dev.py

      我想在没有我的dev.py"文件的情况下将它部署在 .egg 中.我尝试了不同的方法:首先,使用

      I wanted to deploy it in a .egg without my 'dev.py' files. I tried different ways : first, with a

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

      ,然后是一个 MANIFEST.in,其中包含:

      , then with a MANIFEST.in which contains :

      global-exclude dev.py
      

      当我执行 sdist 时,第二种解决方案似乎有效 - 安装时出现此警告:

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

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

      ,但不适用于 bdist-egg.

      , but does'nt work with a bdist-egg.

      这是我的 setup.py 的一部分:

      Here a 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']),
      )
      

      请注意,我使用的是 python 2.6.6,也许这很重要.知道如何正确创建我的蛋,不包括开发文件吗?

      Note that I use python 2.6.6, maybe it matters. Any idea how to create my egg excluding the dev files properly ?

      推荐答案

      我最近遇到了同样的问题(虽然我不得不构建一个轮子而不是一个鸡蛋),但对于 bdist_eggbdist_wheel.您必须覆盖 build_py 中的方法 find_package_modules:

      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},
      )
      

      在这个例子中,所有包中名为 dev 的模块将被排除在构建之外.

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

      如您所见,无需在 find_packages() 中使用排除项,因为您仍然需要包含所有包,而是过滤在每个包中找到的模块文件.build_py 类非常通用,如果需要重用,可以在单独的库中重构;唯一特定于项目的内容是排除模式列表.

      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.

      这篇关于setup.py 从 bdist 中排除一些 python 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆