简体   繁体   English

从 setuptools 包中排除顶级目录

[英]Excluding a top-level directory from a setuptools package

I'm trying to put a Python project into a tarball using setuptools.我正在尝试使用 setuptools 将 Python 项目放入 tarball 中。 The problem is that setuptools doesn't appear to like the way that the source tree was originally setup (not by me, I must add).问题是 setuptools 似乎不喜欢源树最初设置的方式(不是我,我必须添加)。 Everything that I actually want to distribute is in the top-level directory, rather than in a subdirectory like the setuptools docs talk about.我真正想要分发的所有内容都在顶级目录中,而不是像 setuptools 文档所说的那样位于子目录中。

The tree has a directory, tests , that I don't want to have in the released package.该树有一个目录tests ,我不想在已发布的包中包含该目录。 However, using exclude_package_data doesn't seem to actually do any excluding, and I'd like to work out what I've done wrong.但是,使用exclude_package_data实际上并没有做任何排除,我想弄清楚我做错了什么。

My setup.py looks like this, in relevant part:我的setup.py看起来像这样,在相关部分:

setup(
  name="project",
  packages=[''],
  include_package_data=True,
  exclude_package_data={'': ['tests']},
  test_suite='nose.collector',
)

We use the following convention to exclude 'tests' from packages.我们使用以下约定从包中排除“测试”。

setup(
   name="project",
   packages=find_packages(exclude=("tests",)),
   include_package_data=True, 
   test_suite='nose.collector',
)

We also use MANIFEST.in to better control what include_package_data=True does.我们还使用 MANIFEST.in 来更好地控制include_package_data=True作用。

我在同一个问题上浪费了几个小时,试图排除一个模块,我终于发现我必须删除*.egg-infobuild目录,这以某种方式保留了必须包含该模块的想法。

This is what I found in setuptools manual :这是我在setuptools 手册中发现的:

from setuptools import setup, find_packages
...
packages = find_packages(exclude=["*.tests", "*.tests.*", "tests.*", "tests"]),

In my case the following was enough to exclude top-level tests directory:在我的情况下,以下内容足以排除顶级测试目录:

packages = find_packages(exclude=["tests.*", "tests"]),

I have the following in my setup.py ...我的setup.py有以下内容...

setup(name='pyfoo',
      version="1.0.2",
      description='Example for stack overflow',
      url='http://stackoverflow.com/',
      author='David Michael Pennington',
      author_email='mike /|at|\ pennington.net',
      license='GPL',
      platforms='any',
      keywords='Stack Overflow Example',
      entry_points = "",
      long_description=read('README.rst'),
      include_package_data=True,  # Checks MANIFEST.in for explicit rules
      #                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      packages=find_packages(),
      use_2to3=True,
      zip_safe=False,
      setup_requires=["setuptools_hg"],

I had a doc/ directory that was getting massive due to the number of images I had in it;我有一个doc/目录,由于其中包含的图像数量而变得越来越大; this meant that the size of my sdist was growing over 500kB.这意味着我的sdist的大小增长了 500kB 以上。 Originally I had this in my MANIFEST.in ...最初我在我的MANIFEST.in有这个......

include LICENSE CHANGES README.rst requirements.txt
recursive-exclude * __pycache__
recursive-exclude * *.pyc
recursive-exclude * *.pyo
recursive-exclude * *.orig

The only thing I had to do to exclude my doc directory was this line at the bottom of MANIFEST.in ...为了排除我的 doc 目录,我唯一要做的就是MANIFEST.in底部的这一行...

prune doc*

Using prune doc* suddenly removed all my doc/ directory from the sdist tarball.使用prune doc*突然从sdist tarball 中删除了我所有的doc/目录。 So, it looks like you just need to use this in a MANIFEST.in file...所以,看起来你只需要在MANIFEST.in文件中使用它......

prune tests*

Ug, setuptools makes this really tricky :(呃,setuptools 让这真的很棘手:(

I don't know if this is what you want, but one project I work on uses a combination of two things:我不知道这是否是您想要的,但我从事的一个项目使用了两件事的组合:

from setuptools import setup, find_packages
...
packages = find_packages(exclude=['tests']),
data_files = os.walk(path_to_files),

For similar purpose, my collegue wrote setuptools-dummy package: http://github.com/ella/setuptools-dummy/tree/master为了类似的目的,我的同事写了 setuptools-dummy 包: http ://github.com/ella/setuptools-dummy/tree/master

Take a look at setuptools_dummy, modify excludes to your needs and it should work.看看 setuptools_dummy,根据您的需要修改 excludes,它应该可以工作。 If not, open an issue ;)如果没有,打开一个问题;)

An additional solution that worked in my case.在我的情况下工作的附加解决方案。 Apparently: packages=setuptools.find_packages(exclude=["tests.*", "tests"]),显然:packages=setuptools.find_packages(exclude=["tests.*", "tests"]),

didn't work, but:没有用,但是:

packages=setuptools.find_packages(exclude=["*tests.*", "*tests"]),

adding the star character at the beginning of the word did the trick.在单词的开头添加星号就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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