59

当我跑

  python setup.py sdist

它在我的 ./dist 目录中创建一个 sdist。这包括我的“dist”文件夹内的 zip 中的“PROJECT-egg.info”文件,我不使用它,但它不会伤害我,所以我只是忽略它。

我的问题是为什么它还我的项目根目录中创建一个“PROJECT-egg.info”文件夹?我可以让它停止创建这个吗?如果没有,我可以在创建 sdist 后立即删除它吗?

我正在使用从 setuptools 导入的“设置”功能。WindowsXP、Python2.7、Setuptools 0.6c11、Distribute 0.6.14。

我的设置配置如下所示:

{'author': 'Jonathan Hartley',
 'author_email': 'tartley@tartley.com',
 'classifiers': ['Development Status :: 1 - Planning',
                 'Intended Audience :: Developers',
                 'License :: OSI Approved :: BSD License',
                 'Operating System :: Microsoft :: Windows',
                 'Programming Language :: Python :: 2.7'],
 'console': [{'script': 'demo.py'}],
 'data_files': [('Microsoft.VC90.CRT',
                 ['..\\lib\\Microsoft.VC90.CRT\\Microsoft.VC90.CRT.manifest',
                  '..\\lib\\Microsoft.VC90.CRT\\msvcr90.dll'])],
 'description': 'Utilities for games and OpenGL graphics, built around Pyglet.\n',
 'keywords': '',
 'license': 'BSD',
 'long_description': "blah blah blah",
 'name': 'pygpen',
 'options': {'py2exe': {'ascii': True,
                        'bundle_files': 1,
                        'dist_dir': 'dist/pygpen-0.1-windows',
                        'dll_excludes': [],
                        'excludes': ['_imaging_gif',
                                     '_scproxy',
                                     'clr',
                                     'dummy.Process',
                                     'email',
                                     'email.base64mime',
                                     'email.utils',
                                     'email.Utils',
                                     'ICCProfile',
                                     'Image',
                                     'IronPythonConsole',
                                     'modes.editingmodes',
                                     'startup',
                                     'System',
                                     'System.Windows.Forms.Clipboard',
                                     '_hashlib',
                                     '_imaging',
                                     '_multiprocessing',
                                     '_ssl',
                                     '_socket',
                                     'bz2',
                                     'pyexpat',
                                     'pyreadline',
                                     'select',
                                     'win32api',
                                     'win32pipe',
                                     'calendar',
                                     'cookielib',
                                     'difflib',
                                     'doctest',
                                     'locale',
                                     'optparse',
                                     'pdb',
                                     'pickle',
                                     'pyglet.window.xlib',
                                     'pyglet.window.carbon',
                                     'pyglet.window.carbon.constants',
                                     'pyglet.window.carbon.types',
                                     'subprocess',
                                     'tarfile',
                                     'threading',
                                     'unittest',
                                     'urllib',
                                     'urllib2',
                                     'win32con',
                                     'zipfile'],
                        'optimize': 2}},
 'packages': ['pygpen'],
 'scripts': ['demo.py'],
 'url': 'http://code.google.com/p/edpath/',
 'version': '0.1',
 'zipfile': None}
4

5 回答 5

63

此目录是作为源代码分发的构建过程的一部分而有意创建的。稍微看一下setuptools 的开发人员指南,您就会知道为什么:

但是,请务必忽略 distutils 文档中处理 MANIFEST 或它是如何从 MANIFEST.in 生成的任何部分;setuptools 可以保护您免受这些问题的影响,并且在任何情况下都不会以相同的方式工作。与 distutils 不同,setuptools 会在您每次构建源代码分发时重新生成源代码分发清单文件,并将其构建在项目的 .egg-info 目录中,而不影响您的主项目目录。因此,您不必担心它是否是最新的。

构建完成后,您可以安全地删除该目录。

奖金编辑:

我在我的许多 Python 项目中自定义clean命令以删除、、和其他文件。这是一个如何完成的示例:setup.py*.egg-infodistbuild*.pycsetup.py

import os
from setuptools import setup, Command

class CleanCommand(Command):
    """Custom clean command to tidy up the project root."""
    user_options = []
    def initialize_options(self):
        pass
    def finalize_options(self):
        pass
    def run(self):
        os.system('rm -vrf ./build ./dist ./*.pyc ./*.tgz ./*.egg-info')

# Further down when you call setup()
setup(
    # ... Other setup options
    cmdclass={
        'clean': CleanCommand,
    }
)

为了说明,在运行python setup.py build一个名为“poop”的虚拟项目(是的,我很成熟)之后,会发生这种情况:

$ python setup.py build
running build
running build_py
creating build
creating build/lib
creating build/lib/poop
copying poop/__init__.py -> build/lib/poop

现在如果我们运行python setup.py clean

$ python setup.py clean
running clean
removed `./build/lib/poop/__init__.py'
removed directory: `./build/lib/poop'
removed directory: `./build/lib'
removed directory: `./build'

多田!

于 2010-09-23T17:08:46.407 回答
23

-egg.info文件夹并不总是您可以删除的临时工件。

例如,如果您pip install -e YOURPACKAGE用于“可编辑”安装(通过符号链接工作,python setup.py develop因此您不必每次在本地编辑包时都重新安装包),-egg.info当您的包被导入另一个时,该文件夹在运行时是必需的来源。如果它不存在,您将收到DistributionNotFound错误消息。

于 2016-01-12T04:33:53.500 回答
17

请注意,您可以让PROJECT.egg-info工件从您的 sdist 中完全消失。

该命令setup.py egg_info将默认使用源根作为 egg 基础,从而将PROJECT.egg-info目录打包到 sdist 中。

您可以通过传递选项来配置蛋基地--egg-base。这将在PROJECT.egg-info其他地方创建目录,将其完全排除在您的源代码分发之外。您也可以使用 asetup.cfg来设置该属性。

以下命令创建一个没有PROJECT.egg-info适用于我的 sdist:

python setup.py egg_info --egg-base /tmp sdist

或在setup.cfg

[egg_info]
egg_base = /tmp
于 2014-06-09T15:02:41.540 回答
7

Python的打包和构建系统被打破了恕我直言。因此,对于人们认为开箱即用的事情,有许多技巧和变通方法。

但是,我发现删除 *.egg-info 的“最干净”的技巧是使用普通clean --all开关以及egg_info将 *.egg-info 文件放置在将由 clean 命令清除的子文件夹中。这里有一个例子:

在你的setup.cfg使用中是这样的:

[egg_info]
egg_base = ./build/lib

将删除./build/lib的文件夹在哪里。clean --all然后在使用 setuptools 构建项目时,使用带有 --all 标志的 clean 命令,例如

python setup.py bdist_wheel clean --all

如果您还想构建一个源包,只需确保在 sdist 之前构建 bdist_wheel 以便 build/lib 文件夹存在,例如:

python setup.py bdist_wheel sdist clean --all

于 2018-08-16T08:02:50.643 回答
0

jathanism 方法的另一种解决方案可能是使用egg_info钩子。我在每次构建之前使用它进行清理:

from pathlib import Path
from setuptools import setup
from setuptools.command.egg_info import egg_info

here = Path(__file__).parent.resolve()

class CleanEggInfo(egg_info):
    def run(self):
       shutil.rmtree(here.joinpath('build'), ignore_errors=True)
       shutil.rmtree(here.joinpath('dist'), ignore_errors=True)

        for d in here.glob('*.egg-info'):
            shutil.rmtree(d, ignore_errors=True)

        egg_info.run(self)

setup(
    cmdclass={
        'egg_info': CleanEggInfo,
    }
)
于 2021-11-28T18:31:53.487 回答