WS_TRANS = {ord(_wschar) : for _wschar in string.whitespace} 属性错误:模块"字符串"没有属性"空格"



我正在尝试使用安装工具创建一个模块。我安装了安装工具并编写了一小段代码 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'

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

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

复制并尝试这个:

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")

输出: 你好堆栈溢出

最新更新