python setup.py sdist bdist_wheel raises `TypeError: expected string or bytes-like object`

python setup.py sdist bdist_wheel raises `TypeError: expected string or bytes-like object`

问题陈述

packages= 设置为 [find_packages()] 时,我遇到了 TypeError。将 packages 变量直接设置为结果字符串 ("spark_jobs") 时,我不再遇到意外异常。我想知道是否有人了解这里发生的事情。

供参考我 运行 python 3.9.

setup.py

  setup(
      name=f"{PROJECT_NAME}",
      version=f"{VERSION}",
      description="f{PROJECT_DESCRIPTION}",
      long_description=README,
      long_description_content_type="text/markdown",
      url=f"{url}",
      author=f"{author}",
      classifiers=[
          "Programming Language :: Python :: 3",
          "Programming Language :: Python :: 3.7",
          "Programming Language :: Python :: 3.8",
          "Programming Language :: Python :: 3.9",
      ],
      packages=[find_packages()],
      include_package_data=True,
      install_requires=INSTALL_REQUIRES,
      tests_require=TESTS_REQUIRE,
  )

错误

python setup.py sdist bdist_wheel
Traceback (most recent call last):
  File "setup.py", line 40, in <module>
    setup(
  File "/Users/alexlordthorsen/.venvs/data_platform/lib/python3.8/site-packages/setuptools/__init__.py", line 145, in setup
    return distutils.core.setup(**attrs)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/distutils/core.py", line 108, in setup
    _setup_distribution = dist = klass(attrs)
  File "/Users/alexlordthorsen/.venvs/data_platform/lib/python3.8/site-packages/setuptools/dist.py", line 445, in __init__
    _Distribution.__init__(self, {
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/distutils/dist.py", line 292, in __init__
    self.finalize_options()
  File "/Users/alexlordthorsen/.venvs/data_platform/lib/python3.8/site-packages/setuptools/dist.py", line 734, in finalize_options
    ep.load()(self, ep.name, value)
  File "/Users/alexlordthorsen/.venvs/data_platform/lib/python3.8/site-packages/setuptools/dist.py", line 329, in check_packages
    if not re.match(r'\w+(\.\w+)*', pkgname):
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/re.py", line 189, in match
    return _compile(pattern, flags).match(string)
TypeError: expected string or bytes-like object

设置断点

当我设置断点时,我发现结果是一个只有一个字符串的列表。

(Pdb) find_packages()
['spark_jobs']

令人困惑的是,如果我直接设置 packages=['spark-jobs'] 我不再遇到上述错误,但我也得到一个空

find_packages() 已经 returns 一个列表,您不需要将其包装在另一个列表中。正确的语法只是

  packages=find_packages(),

packages=['spark-jobs']中,包名的拼写必须与目录完全相同(并且目录名必须遵循Python命名规则),因此将其修复为

packages=['spark_jobs']