在 Pycharm 中使用 StanfordCoreNLP 时 psutil.AccessDenied? [英] psutil.AccessDenied when using StanfordCoreNLP in Pycharm?
问题描述
# coding=utf-8
from stanfordcorenlp import StanfordCoreNLP
nlp = StanfordCoreNLP(r'/Users/silas/stanford-corenlp/', lang='zh')
sentence = '清华大学位于北京。'
print nlp.word_tokenize(sentence)
print nlp.pos_tag(sentence)
print nlp.ner(sentence)
print nlp.parse(sentence)
print nlp.dependency_parse(sentence)
nlp.close()
我使用的是 Mac.Java、NLKT 和 Stanforcorenlp 工具包都已准备就绪.当我测试项目时,错误出来了.
I'm using Mac. Java, NLKT, and Stanforcorenlp toolkit are all ready. When I'm testing the project, the error came out.
Traceback (most recent call last):
line 5, in <module>
nlp = StanfordCoreNLP(r'/Users/silas/stanford-corenlp/', lang='zh')
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/stanfordcorenlp/corenlp.py", line 79, in __init__
if port_candidate not in [conn.laddr[1] for conn in psutil.net_connections()]:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/psutil/__init__.py", line 2120, in net_connections
return _psplatform.net_connections(kind)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/psutil/_psosx.py", line 255, in net_connections
cons = Process(pid).connections(kind)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/psutil/_psosx.py", line 347, in wrapper
raise AccessDenied(self.pid, self._name)
psutil._exceptions.AccessDenied: psutil.AccessDenied (pid=25422)
我猜是因为 Pycharm 项目不是在 root 用户下运行的.但是如何配置 IDE 来解决问题?
I guess it is because the Pycharm project isn't running under a root user. But how can I configure the IDE to fix the problem?
推荐答案
这个问题似乎是 Mac OS X 特有的,它不允许 Python 检查当前端口.
This problem seems to be specific to Mac OS X which would not allow Python to check the current port.
注释 corenlp.py 文件的这部分代码:
Comment this portion of code of corenlp.py file:
if self.port is None:
for port_candidate in range(9000, 65535):
if port_candidate not in [conn.laddr[1] for conn in psutil.net_connections()]:
self.port = port_candidate
break
if self.port in [conn.laddr[1] for conn in psutil.net_connections()]:
raise IOError('Port ' + str(self.port) + ' is already in use.')
用这一行替换:
self.port = 9999
来源:https://github.com/Lynten/stanford-corenlp/issues/26#issuecomment-445507811
另一种解决方案是使用 sudo 命令行运行 StanfordCoreNLP.
Another solution is to run StanfordCoreNLP with a sudo command line.
这篇关于在 Pycharm 中使用 StanfordCoreNLP 时 psutil.AccessDenied?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!