在Pycharm中使用StanfordCoreNLP时是否为psutil.AccessDenied?

时间:2018-11-02 03:41:48

标签: python python-2.7 stanford-nlp python-2.x

# 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工具箱都已准备就绪。当我测试项目时,错误就出来了。

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来解决问题?

2 个答案:

答案 0 :(得分:0)

不幸的是,如果您查看_psosx.py中的psutil项目,则在net_connections下,第243行说。.

  

注意:在macOS上,除非进程由root拥有,否则它将失败并显示AccessDenied。

这意味着您需要通过执行sudo pycharm.sh之类的操作以root用户身份运行。

如果您不想以root用户身份运行整个IDE,那么在SO上有一些示例,说明如何使用超级用户权限运行特定的脚本。例如,请参见Debugging in pyCharm with sudo privileges

答案 1 :(得分:0)

此问题似乎是特定于Mac OS X的,它不允许Python检查当前端口。

注释corenlp.py文件的这段代码:

        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。