setuptools:从zip中删除lib / python3.5 / site-packages前缀

时间:2016-03-31 12:44:49

标签: python setuptools

我正在尝试创建一个应该作为zip文件分发的包,它只包含包本身。不幸的是,至少从virtualenv运行时,zip包含冗长的子目录。这是项目的文件结构:

.
├── my_package
│   └── __init__.py
├── README.md
└── setup.py

setup.py包含:

from setuptools import setup, find_packages

setup(
    name='my_package',
    version='1.0.0',
    author='my name',
    author_email='my-email@example.com',
    packages=find_packages('.'),
    data_files=[('my_package', ['README.md'])],
    zip_safe=True,
)

当我运行python setup.py bdist_dumb --format zip --relative时,我得到以下输出:

running bdist_dumb
running build
running build_py
creating build
creating build/lib
creating build/lib/my_package
copying my_package/__init__.py -> build/lib/my_package
installing to build/bdist.linux-x86_64/dumb
running install
running install_lib
creating build/bdist.linux-x86_64
creating build/bdist.linux-x86_64/dumb
creating build/bdist.linux-x86_64/dumb/home
creating build/bdist.linux-x86_64/dumb/home/sybren
creating build/bdist.linux-x86_64/dumb/home/sybren/.virtualenvs
creating build/bdist.linux-x86_64/dumb/home/sybren/.virtualenvs/packaging-test
creating build/bdist.linux-x86_64/dumb/home/sybren/.virtualenvs/packaging-test/lib
creating build/bdist.linux-x86_64/dumb/home/sybren/.virtualenvs/packaging-test/lib/python3.5
creating build/bdist.linux-x86_64/dumb/home/sybren/.virtualenvs/packaging-test/lib/python3.5/site-packages
creating build/bdist.linux-x86_64/dumb/home/sybren/.virtualenvs/packaging-test/lib/python3.5/site-packages/my_package
copying build/lib/my_package/__init__.py -> build/bdist.linux-x86_64/dumb/home/sybren/.virtualenvs/packaging-test/lib/python3.5/site-packages/my_package
byte-compiling build/bdist.linux-x86_64/dumb/home/sybren/.virtualenvs/packaging-test/lib/python3.5/site-packages/my_package/__init__.py to __init__.cpython-35.pyc
running install_data
creating build/bdist.linux-x86_64/dumb/home/sybren/.virtualenvs/packaging-test/my_package
copying README.md -> build/bdist.linux-x86_64/dumb/home/sybren/.virtualenvs/packaging-test/my_package
running install_egg_info
running egg_info
creating my_package.egg-info
writing my_package.egg-info/PKG-INFO
writing dependency_links to my_package.egg-info/dependency_links.txt
writing top-level names to my_package.egg-info/top_level.txt
writing manifest file 'my_package.egg-info/SOURCES.txt'
reading manifest file 'my_package.egg-info/SOURCES.txt'
writing manifest file 'my_package.egg-info/SOURCES.txt'
Copying my_package.egg-info to build/bdist.linux-x86_64/dumb/home/sybren/.virtualenvs/packaging-test/lib/python3.5/site-packages/my_package-1.0.0-py3.5.egg-info
running install_scripts
creating /home/sybren/workspace/packaging-test/dist
creating '/home/sybren/workspace/packaging-test/dist/my_package-1.0.0.linux-x86_64.zip' and adding '.' to it
adding 'lib/python3.5/site-packages/my_package/__init__.py'
adding 'lib/python3.5/site-packages/my_package/__pycache__/__init__.cpython-35.pyc'
adding 'lib/python3.5/site-packages/my_package-1.0.0-py3.5.egg-info/dependency_links.txt'
adding 'lib/python3.5/site-packages/my_package-1.0.0-py3.5.egg-info/PKG-INFO'
adding 'lib/python3.5/site-packages/my_package-1.0.0-py3.5.egg-info/top_level.txt'
adding 'lib/python3.5/site-packages/my_package-1.0.0-py3.5.egg-info/SOURCES.txt'
adding 'lib/python3.5/site-packages/my_package-1.0.0-py3.5.egg-info/zip-safe'
adding 'my_package/README.md'
removing 'build/bdist.linux-x86_64/dumb' (and everything under it)

正如您所看到的,它将包放在lib/python3.5/site-packages中,这是我不想要的。我只希望包中包含my_package/*

如果我们可以阻止my_package*.egg-info/*打包,甚至更好,但我的主要问题是摆脱lib/python3.5/site-packages前缀。

1 个答案:

答案 0 :(得分:2)

这似乎提供了我正在寻找的行为。但是,如果有更优雅的解决方案,例如将某些参数传递给setup(),我很高兴听到这个。

from setuptools import setup, find_packages
from setuptools.command.install import install


class CustomInstall(install):
    """Ensures the module is placed at the root of the zip file."""

    def initialize_options(self):
        super().initialize_options()
        self.prefix = ''
        self.install_lib = ''

setup(
    cmdclass={'install': CustomInstall},
    name='my_package',
    version='1.0.0',
    author='my name',
    author_email='my-email@example.com',
    packages=find_packages('.'),
    data_files=[('my_package', ['README.md'])],
    zip_safe=True,
)