问题
# 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()
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)
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?
回答1:
Unfortunately, if you look at the psutil project in _psosx.py, under net_connections, line 243 says..
Note: on macOS this will fail with AccessDenied unless the process is owned by root.
That means that you'll need to run as root by doing something like sudo pycharm.sh
.
If you don't want to run your entire IDE as root, there's a few examples on SO on how you can run a specific script with super-user privileges. For instance see Debugging in pyCharm with sudo privileges.
回答2:
This problem seems to be specific to Mac OS X which would not allow Python to check the current port.
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.')
Replace by this line:
self.port = 9999
Source: https://github.com/Lynten/stanford-corenlp/issues/26#issuecomment-445507811
Another solution is to run StanfordCoreNLP with a sudo command line.
来源:https://stackoverflow.com/questions/53112409/psutil-accessdenied-when-using-stanfordcorenlp-in-pycharm