代码之家  ›  专栏  ›  技术社区  ›  Bite code

有没有更简单的方法可以用python打包?

  •  4
  • Bite code  · 技术社区  · 14 年前

    我今天尝试打包了一个django应用程序。它是一个大宝贝,有了安装文件,我必须手动在“package”参数中写入所有包和子包。然后我必须找到一种复制装置、HTMLS/CSS/图像文件、文档等的方法。

    这是一种糟糕的工作方式。我们是计算机科学家,我们自动化,这样做毫无意义。

    当我改变我的应用程序结构时会怎样?我必须重写setup.py。

    有更好的方法吗?自动化的工具? 我真不敢相信一种语言会像python那样重视开发人员的时间,从而使打包变得如此繁琐。

    我希望最终能够使用一个简单的pip安装来安装应用程序。我知道构建,但它并不简单,也不支持PIP。

    4 回复  |  直到 14 年前
        1
  •  5
  •   jathanism    14 年前

    至少如果你用 setuptools (替代stdlib的 distutils )你得到了一个很棒的函数 find_packages() 当从包根目录运行时,它返回一个以适合于 packages 参数。

    下面是一个例子:

    # setup.py
    
    from setuptools import find_packages, setup
    
    setup(
        #...
        packages=find_packages(exclude='tests'),
        #...
    )
    

    P.S.包装吸收每种语言和每种系统。不管你怎么切它都很烂。

        2
  •  1
  •   Jonny Buchanan    14 年前

    我今天也经历过这种痛苦。我用了下面的,直接从 Django's setup.py ,它会在应用程序的文件系统中查找包和数据文件(假设您从未将这两者混合在一起):

    import os
    from distutils.command.install import INSTALL_SCHEMES
    
    def fullsplit(path, result=None):
        """
        Split a pathname into components (the opposite of os.path.join) in a
        platform-neutral way.
        """
        if result is None:
            result = []
        head, tail = os.path.split(path)
        if head == '':
            return [tail] + result
        if head == path:
            return result
        return fullsplit(head, [tail] + result)
    
    # Tell distutils to put the data_files in platform-specific installation
    # locations. See here for an explanation:
    # http://groups.google.com/group/comp.lang.python/browse_thread/thread/35ec7b2fed36eaec/2105ee4d9e8042cb
    for scheme in INSTALL_SCHEMES.values():
        scheme['data'] = scheme['purelib']
    
    # Compile the list of packages available, because distutils doesn't have
    # an easy way to do this.
    packages, data_files = [], []
    root_dir = os.path.dirname(__file__)
    if root_dir != '':
        os.chdir(root_dir)
    myapp_dir = 'myapp'
    
    for dirpath, dirnames, filenames in os.walk(myapp_dir):
        # Ignore dirnames that start with '.'
        for i, dirname in enumerate(dirnames):
            if dirname.startswith('.'): del dirnames[i]
        if '__init__.py' in filenames:
            packages.append('.'.join(fullsplit(dirpath)))
        elif filenames:
            data_files.append([dirpath, [os.path.join(dirpath, f) for f in filenames]])
    
        3
  •  0
  •   ewall    14 年前

    我想你要找的工具是 Buildout . 在很多地方,你可以从 SlideShare Pycon videos .

    您可能希望签出的其他类似或相关工具包括 virtualenv, Fabric, and PIP .

        4
  •  -1
  •   Yuval Adam    14 年前

    我最近一直在做一些关于django部署方法的研究。

    我发现这两种资源非常有用: