首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >setup.py包和unicode_literals

setup.py包和unicode_literals
EN

Stack Overflow用户
提问于 2014-04-19 19:51:52
回答 3查看 2.5K关注 0票数 5

我在Py2.7中创建了一个包,并试图使它与Py3兼容。问题是如果我将unicode_literals包含在

代码语言:javascript
复制
__init__.py

导入生成将返回此错误。

代码语言:javascript
复制
error in daysgrounded setup command: package_data must be a dictionary mapping
package names to lists of wildcard patterns

我读过PEP,但我不明白这和一个白痴有什么关系

代码语言:javascript
复制
__pkgdata__

有人能帮忙吗?

代码语言:javascript
复制
__init__.py
#!/usr/bin/env python
# -*- coding: latin-1 -*-

"""Manage child(s) grounded days."""

from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
# ToDo: correct why the above unicode_literals import prevents setup.py from working

import sys
from os import path
sys.path.insert(1, path.dirname(__file__))

__all__ = ['__title__', '__version__',
           '__desc__', '__license__', '__url__',
           '__author__', '__email__',
           '__copyright__',
           '__keywords__', '__classifiers__',
           #'__packages__',
           '__entrypoints__', '__pkgdata__']

__title__ = 'daysgrounded'
__version__ = '0.0.9'

__desc__ = __doc__.strip()
__license__ = 'GNU General Public License v2 or later (GPLv2+)'
__url__ = 'https://github.com/jcrmatos/DaysGrounded'

__author__ = 'Joao Matos'
__email__ = 'jcrmatos@gmail.com'

__copyright__ = 'Copyright 2014 Joao Matos'

__keywords__ = 'days grounded'
__classifiers__ = [# Use below to prevent any unwanted publishing
                   #'Private :: Do Not Upload'
                   'Development Status :: 4 - Beta',
                   'Environment :: Console',
                   'Environment :: Win32 (MS Windows)',
                   'Intended Audience :: End Users/Desktop',
                   'Intended Audience :: Developers',
                   'Natural Language :: English',
                   'Natural Language :: Portuguese',
                   'License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+)',
                   'Operating System :: OS Independent',
                   'Programming Language :: Python',
                   'Programming Language :: Python :: 2.7',
                   'Programming Language :: Python :: 3.4',
                   'Topic :: Other/Nonlisted Topic']

#__packages__ = ['daysgrounded']

__entrypoints__ = {
    'console_scripts': ['daysgrounded = daysgrounded.__main__:main'],
    #'gui_scripts': ['app_gui = daysgrounded.daysgrounded:start']
    }

__pkgdata__ = {'daysgrounded': ['*.txt']}
#__pkgdata__= {'': ['*.txt'], 'daysgrounded': ['*.txt']}


setup.py
#!/usr/bin/env python
# -*- coding: latin-1 -*-

from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

from setuptools import setup, find_packages
#import py2exe

#from daysgrounded import *
from daysgrounded import (__title__, __version__,
                          __desc__, __license__, __url__,
                          __author__, __email__,
                          __keywords__, __classifiers__,
                          #__packages__,
                          __entrypoints__, __pkgdata__)

setup(
    name=__title__,
    version=__version__,

    description=__desc__,
    long_description=open('README.txt').read(),
    #long_description=(read('README.txt') + '\n\n' +
    #                  read('CHANGES.txt') + '\n\n' +
    #                  read('AUTHORS.txt')),
    license=__license__,
    url=__url__,

    author=__author__,
    author_email=__email__,

    keywords=__keywords__,
    classifiers=__classifiers__,

    packages=find_packages(exclude=['tests*']),
    #packages=__packages__,

    entry_points=__entrypoints__,
    install_requires=open('requirements.txt').read(),
    #install_requires=open('requirements.txt').read().splitlines(),

    include_package_data=True,
    package_data=__pkgdata__,

    #console=['daysgrounded\\__main__.py']
)

谢谢,

JM

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-04-19 20:27:46

使用unicode_literals与对输入文件中的每个字符串文本使用u'...'相同,这意味着在__init__.py中指定

代码语言:javascript
复制
__pkgdata__ = {'daysgrounded': ['*.txt']}

实际上和

代码语言:javascript
复制
__pkgdata__ = {u'daysgrounded': [u'*.txt']}

对于python2,setuptools在这里并不期望unicode,而是str,所以它失败了。

似乎您在__init__.py中的字符串中没有使用任何unicode字符,只是普通的ascii,所以您可以简单地删除unicode_literals导入。如果您确实在文章中没有显示的文件中的某个位置使用unicode文字,那么在那里使用显式unicode文字。

票数 6
EN

Stack Overflow用户

发布于 2014-04-19 20:33:44

这是setuptools中的一个bug。它使用isinstance(k, str)验证值,当字符串被unicode_literals导入转换为2.x unicode类时,unicode_literals会失败。应该修补使用isinstance(k, basestring)

最简单的解决方案是将配置设置直接放到setup.py中,而不是将它们存储在__init__.py中。如果您需要对__version__进行编程访问,那么将其放在一个单独的包中,该包由setup.py__init__.py都包含。

来自setuptools dist.py:

代码语言:javascript
复制
def check_package_data(dist, attr, value):
    """Verify that value is a dictionary of package names to glob lists"""
    if isinstance(value,dict):
        for k,v in value.items():
            if not isinstance(k,str): break
            try: iter(v)
            except TypeError:
                break
        else:
        return
    raise DistutilsSetupError(
        attr+" must be a dictionary mapping package names to lists of "
        "wildcard patterns"
   )
票数 3
EN

Stack Overflow用户

发布于 2017-02-23 17:46:42

unicode_literals的使用是为了使Python2与Python3代码兼容,str现在是Python2中的unicode字符串和字节字符串。它可以很好地防止字节字符串和unicode-字符串的混合,这在Py2上存在很长时间的问题,但是也有一些类似的问题。

凯文已经解释了这个bug,我想说,对于setup.py来说,它不是严格要求的,修复它有点难看,特别是如果您有大量package_data条目的话。

如果要将unicode_literals保存在setup.py中,只需将dict键编码为字节字符串:

代码语言:javascript
复制
__pkgdata__ = {b'daysgrounded': ['*.txt']}

然而,在Python 3下,它将失败,同时产生相同的消息,因此需要涵盖这两个版本:

代码语言:javascript
复制
if sys.version_info.major == 2:
    __pkgdata__ = {b'daysgrounded': ['*.txt']}
else:
    __pkgdata__ = {'daysgrounded': ['*.txt']}

或者使用来自bytes_to_native_str模块的future

代码语言:javascript
复制
from future.utils import bytes_to_native_str

__pkgdata__ = {bytes_to_native_str(b'daysgrounded'): ['*.txt']}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23174738

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档