【问题标题】:WS_TRANS = {ord(_wschar) : ' ' for _wschar in string.whitespace} AttributeError: module 'string' has no attribute 'whitespace'WS_TRANS = {ord(_wschar) : ' ' for _wschar in string.whitespace} AttributeError: 模块 'string' 没有属性 'whitespace'
【发布时间】:2018-10-13 13:54:58
【问题描述】:

我正在尝试使用 setuptools 创建一个模块。我安装了 setuptools 并写了一小段代码作为 TestFunction.py:

  def function(test):
        print(test);
    function("hello World")    

并使用以下指令创建了 setup.py 文件:

from setuptools import setup
setup(
      name='TestFunction.py',
      version='1.0',
      description='this is a python distribution',
      py_module=['TestFunction'],)

现在我正在运行 python3 setup.py sdist 并收到以下错误。我的操作系统是 ubuntu 18。非常感谢。

Original exception was:
Traceback (most recent call last):
  File "setup.py", line 1, in <module>
    from setuptools import setup, find_packages
  File "/home/jeet/.local/lib/python3.6/site-packages/setuptools/__init__.py", line 6, in <module>
    import distutils.core
  File "/usr/lib/python3.6/distutils/core.py", line 16, in <module>
    from distutils.dist import Distribution
  File "/usr/lib/python3.6/distutils/dist.py", line 18, in <module>
    from distutils.fancy_getopt import FancyGetopt, translate_longopt
  File "/usr/lib/python3.6/distutils/fancy_getopt.py", line 373, in <module>
    WS_TRANS = {ord(_wschar) : ' ' for _wschar in string.whitespace}
AttributeError: module 'string' has no attribute 'whitespace'

【问题讨论】:

    标签: python python-3.x setuptools setup.py


    【解决方案1】:

    您的文件string.py 似乎不是标准库中的模块,而是您自己的模块或脚本。重命名它,这样它就不会掩盖标准模块;不要忘记删除string.pyc 文件;对于 python 3.6,它位于 __pycache__ 目录中。

    【讨论】:

      【解决方案2】:

      我更改了您的代码,因为您有缩进格式错误。您的其余代码工作正常。

      复制并试试这个:

      test_module.py

      def function1(test):
        print(test);
      

      setup.py

      from setuptools import setup
      setup(
          name='test_module.py',
          version='1.0',
          description='this is a python distribution',
          py_module=['test_module'],)
      

      用法:

      python setup.py install
      

      使用你的功能:

      from test_module import function1
      function1("hello stackoverflow")
      

      输出: 你好stackoverflow

      【讨论】:

        猜你喜欢
        • 2017-04-07
        • 2018-04-14
        • 2019-02-18
        • 1970-01-01
        • 2020-01-01
        • 2019-07-20
        • 2021-11-05
        • 2021-11-04
        相关资源
        最近更新 更多