Python 模块在 bash 中返回错误,但不在 IDLE 中返回错误

标签 python macos python-import python-idle fuzzywuzzy

我是一个新手程序员,第一次在这里发帖。任何建议或意见,将不胜感激!我正在开发一个项目,该项目比较 test.csv 和 ref.csv 的内容(两个单列都包含 3-4 个单词的字符串),并根据 test.csv 中每个字符串与最相似字符串的相似度为每个字符串分配一个分数ref.csv 中的字符串。我正在使用fuzzywuzzy字符串匹配模块来分配相似度分数。

以下代码片段获取两个输入文件,将它们转换为数组,然后打印出数组:

import csv

# Load text matching module
from fuzzywuzzy import fuzz
from fuzzywuzzy import process


# Import reference and test lists and assign to variables
ref_doc = csv.reader(open('ref.csv', 'rb'), delimiter=",", quotechar='|')
test_doc = csv.reader(open('test.csv', 'rb'), delimiter=",", quotechar='|')

# Define the arrays into which to load these lists
ref = []
test = []

# Assign the reference and test docs to arrays
for row in ref_doc:
    ref.append(row)

for row in test_doc:
    test.append(row)

# Print the arrays to make sure this all worked properly
# before we procede to run matching operations on the arrays
print ref, "\n\n\n", test

问题是,当我在 IDLE 中运行该脚本时,它会按预期工作,但当我从 bash 调用它时,会返回以下错误:

['one', 'two']
Traceback (most recent call last):
  File "csvimport_orig.py", line 4, in <module>
    from fuzzywuzzy import fuzz
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/fuzzywuzzy/fuzz.py", line 32, in <module>
    import utils
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/fuzzywuzzy/utils.py", line 6, in <module>
    table_from=string.punctuation+string.ascii_uppercase
AttributeError: 'module' object has no attribute 'punctuation'

我需要在 bash 中配置一些东西才能正常工作吗?或者是否存在 IDLE 无法捕获的根本错误?为了简单起见,我没有在此代码片段中调用 fuzzywuzzy 模块,但它在 IDLE 中按预期工作。

最终,我想使用pylevenshtein但在我投入额外的时间来完成这项工作之前,我想看看我对这个脚本的使用是否有值(value)。

提前致谢。

最佳答案

几乎可以肯定,您正在加载一个名为 string.py 的模块,而不是 stdlib 的 string.py 模块。

您可以通过添加行来确认这一点

import string
print string

到您的csvimport_orig.py程序。这应该显示类似

<module 'string' from '/usr/lib/python2.7/string.pyc'>

[我现在在 Linux 上,所以位置不同,您应该看到通常的 /Library/Frameworks/etc. 等效项。] 它可能会显示的是

<module 'string' from 'string.py'>

或者,而不是 string.py,无论您的冲突库在哪里。重命名您的 string.py 模块。

关于Python 模块在 bash 中返回错误,但不在 IDLE 中返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9449649/

相关文章:

swift - Finder 同步扩展无法从桌面读取/写入文件

regex - alpha 正则表达式不会在 bash 中挑选出 alpha 字符

node.js - 安装 MAC OS X El Capitan 后出错

python - PyCharm:导入错误:没有名为 'websocket' 的模块

python - 在类定义之外定义一个方法?

python - 优化: Dumping JSON from a Streaming API to Mongo

python - python 中的绝对导入不起作用,相对导入起作用

在调试器中运行时 python 导入需要很长时间

python - 无法在 pygame 中的屏幕上渲染文本

Python tkinter,如何禁止点击背景按钮