如何使用distutils创建可执行的.zip文件? [英] how to use distutils to create executable .zip file?

查看:108
本文介绍了如何使用distutils创建可执行的.zip文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 2.6及更高版本可以直接执行.zip文件。我想利用此功能来提供我正在开发的工具的预览版本,该工具不需要用户安装任何将.zip文件复制到其磁盘上的内容。有创建这种zip文件的标准方法吗?我正在寻找一种适用于python 2.6和python 2.7的解决方案。

Python 2.6 and beyond has the ability to directly execute a .zip file if the zip file contains a __main__.py file at the top of the zip archive. I'm wanting to leverage this feature to provide preview releases of a tool I'm developing that won't require users to install anything beyond copying the .zip file to their disk. Is there a standard way to create such a zip file? I'm looking for a solution that works with python 2.6 and python 2.7.

理想情况下,我想使用distutils,因为当我要进行常规安装时,已经可以使用distutils。有没有使用(或扩展)distutils来创建此类.zip文件的规范方法?

Ideally I would like to use distutils, since I already have it working when I want to do a normal install. Is there a canonical way to use (or extend) distutils to create such a .zip file?

distutils提供了 sdist 命令创建一个几乎是 的源代码发行版,但是创建的结构太深了。

distutils provides an sdist command which creates a source distribution that is almost right, but creates a structure that is a little too deep.

例如,我的源代码树如下:

For example, my source tree looks like this:

my_package/
  - setup.py
  - src/
      - __main__.py
      - module1/
      - module2/
      - module3/

当我执行 python setup.py sdist 时,我最终得到一个.zip文件,其结构如下:

When I do python setup.py sdist I end up with a .zip file with the following structure:

my_package-0.1.zip
  - my_package-0.1/
      - README.txt
      - PKG_INFO
      - src/
          - __main__.py
          - module1/
          - module2/
          - module3/

这不是可执行文件,因为 __ main __。py 不在分布的顶部。实际上,我想要的是一个不包含 src 的src发行版,但仅包含 src 下的文件。那,或者恰好是 sdist 给我的,但是在归档文件的顶部有一个额外的 __ main __。py

This isn't executable because __main__.py is not at the top of the distribution. Effectively what I want is a src distribution that doesn't include src, but only the files under src. That, or exactly what sdist gives me, but with an extra __main__.py at the top of the archive.

推荐答案

已更新:由于 setup.cfg 是全局的,它会影响所有命令的 install-lib设置,这不是所需要的。不幸的是(据我所知)没有办法通过命令行将选项传递给子命令。如果指定 bdist --install-lib = / ,则会引发错误,而不是将错误传递给子命令。

Updated: Since the setup.cfg is global, it affects the 'install-lib' setting for all commands, which is not what is desired. Unfortunately there is no way (to my knowledge) to pass a options to a subcommand via the command line, e.g. if you specify bdist --install-lib=/ it will raise an error instead of passing that down to the subcommands.

install 子命令 only 定制 install-lib c $ c> bdist 运行后,您可以将 bdist_dumb 命令子类化,并在安装后手动设置路径子命令被构造/重新初始化:

To customize the install-lib for the install subcommand only when bdist is run, you can subclass the bdist_dumb command and set the path manually after the install subcommand is constructed / reinitialized:

setup.py

from distutils.core import setup
from distutils.command.bdist_dumb import bdist_dumb

class custom_bdist_dumb(bdist_dumb):

    def reinitialize_command(self, name, **kw):
        cmd = bdist_dumb.reinitialize_command(self, name, **kw)
        if name == 'install':
            cmd.install_lib = '/'
        return cmd

if __name__ == '__main__':
    setup(
        # our custom class override
        cmdclass = {'bdist_dumb': custom_bdist_dumb},
        name='my_package',
        py_modules = ['__main__'],
        packages = ['module1', 'module2'],
        package_dir = {'': 'src'}
    )

运行:

% python setup.py bdist --format=zip
% unzip -l dist/my_package-0.0.0.linux-x86_64.zip
Archive:  dist/my_package-0.0.0.linux-x86_64.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
      184  2011-05-31 20:34   my_package-0.0.0.egg-info
       30  2011-05-31 20:34   __main__.py
      128  2011-05-31 20:34   __main__.pyc
      107  2011-05-31 20:34   module1/__init__.pyc
        0  2011-05-31 20:27   module1/__init__.py
      107  2011-05-31 20:34   module2/__init__.pyc
        0  2011-05-31 20:27   module2/__init__.py
---------                     -------
      556                     7 files

% python dist/my_package-0.0.0.linux-x86_64.zip
my_package working.

这篇关于如何使用distutils创建可执行的.zip文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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