简体   繁体   English

Python模块以bash返回错误,但不是从IDLE返回错误

[英]Python module returning errors in bash but not from IDLE

I'm a newbie programmer posting here for the first time. 我是新手程序员,第一次在这里发布。 Any suggestions or advice would be greatly appreciated! 任何建议或意见,将不胜感激! I am working on a project that compares the contents of, say test.csv to ref.csv (both single columns containing strings of 3-4 words) and assigns a score to each string from test.csv based its similarity to the most similar string in ref.csv. 我正在做一个项目,将test.csv和ref.csv的内容进行比较(两个列都包含3-4个单词的字符串),并根据与最相似的字符串对test.csv中的每个字符串分配分数ref.csv中的字符串。 I am using the fuzzywuzzy string matching module to assign the similarity score. 我正在使用Fuzzywuzzy字符串匹配模块来分配相似性得分。

The following code snippet takes the two input files, converts them into arrays, and prints out the arrays: 下面的代码片段将两个输入文件转换为数组,然后打印出数组:

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

The problem is that this script works as expected when I run it in IDLE, but returns the following error when I call it from bash: 问题是,当我在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'

Is there something I need to configure in bash for this to work properly? 我需要在bash中进行配置以使其正常工作吗? Or is there something fundamentally wrong that IDLE is not catching? 还是IDLE无法捕捉到某些根本错误的信息? For simplicity's sake, I don't call the fuzzywuzzy module in this snippet, but it works as expected in IDLE. 为了简单起见,我在此代码段中未调用Fuzzywuzzy模块,但它在IDLE中按预期工作。

Eventually, I'd like to use pylevenshtein but am trying to see if my use for this script has value before I put the extra time in making that work. 最终,我想使用pylevenshtein,但是在我花额外的时间进行这项工作之前,我试图查看我对此脚本的使用是否有价值。

Thanks in advance. 提前致谢。

Almost certainly you have a module called string.py which is being loaded instead of the stdlib's string.py module. 几乎可以肯定,您正在加载一个名为string.py的模块,而不是stdlib的string.py模块。

You can confirm this by adding the lines 您可以通过添加以下行来确认

import string
print string

to your csvimport_orig.py program. 到您的csvimport_orig.py程序。 This should show something like 应该显示类似

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

[I'm on linux at the moment, so the location is different and you should see the usual /Library/Frameworks/etc. [我目前在Linux上,因此位置不同,您应该会看到通常的/Library/Frameworks/etc. equivalent.] What it will probably show instead is 等效。]它可能会显示的是

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

or, instead of string.py , wherever your conflicting library is. 或者,无论您的冲突库在哪里,都可以使用string.py代替。 Rename your string.py module. 重命名您的string.py模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM