我在使用 StanfordCoreNLP 的时候遇到了需要管理员权限才能成功运行程序的问题.

1
2
from stanfordcorenlp import StanfordCoreNLP
core_nlp = StanfordCoreNLP('library/english')

输出结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
---------------------------------------------------------------------------
PermissionError Traceback (most recent call last)
/Applications/anaconda3/lib/python3.7/site-packages/psutil/_psosx.py in wrapper(self, *args, **kwargs)
333 try:
--> 334 return fun(self, *args, **kwargs)
335 except ProcessLookupError:

/Applications/anaconda3/lib/python3.7/site-packages/psutil/_psosx.py in connections(self, kind)
520 with catch_zombie(self):
--> 521 rawlist = cext.proc_connections(self.pid, families, types)
522 ret = []

PermissionError: [Errno 1] Operation not permitted (originated from proc_pidinfo())

During handling of the above exception, another exception occurred:

AccessDenied Traceback (most recent call last)
<ipython-input-8-77a67380f158> in <module>
1 from stanfordcorenlp import StanfordCoreNLP
2
----> 3 core_nlp = StanfordCoreNLP('library/english')

/Applications/anaconda3/lib/python3.7/site-packages/stanfordcorenlp/corenlp.py in __init__(self, path_or_host, port, memory, lang, timeout, quiet, logging_level)
77 if self.port is None:
78 for port_candidate in range(9000, 65535):
---> 79 if port_candidate not in [conn.laddr[1] for conn in psutil.net_connections()]:
80 self.port = port_candidate
81 break

/Applications/anaconda3/lib/python3.7/site-packages/psutil/__init__.py in net_connections(kind)
2153 On macOS this function requires root privileges.
2154 """
-> 2155 return _psplatform.net_connections(kind)
2156
2157

/Applications/anaconda3/lib/python3.7/site-packages/psutil/_psosx.py in net_connections(kind)
245 for pid in pids():
246 try:
--> 247 cons = Process(pid).connections(kind)
248 except NoSuchProcess:
249 continue

/Applications/anaconda3/lib/python3.7/site-packages/psutil/_psosx.py in wrapper(self, *args, **kwargs)
336 raise NoSuchProcess(self.pid, self._name)
337 except PermissionError:
--> 338 raise AccessDenied(self.pid, self._name)
339 except cext.ZombieProcessError:
340 raise ZombieProcess(self.pid, self._name, self._ppid)

AccessDenied: psutil.AccessDenied (pid=14252)

解决方案如下:

Step 1: 生成一个 notebook 的配置文件

1
jupyter notebook --generate-config

Step 2: 修改配置文件

1
vim ~/.jupyter/jupyter_notebook_config.py

通过 vim 打开配置文件并将文件中的 #c.NotebookApp.allow_root = False 去注释, 然后将 False 改为 True.

Step 3: 在 terminal 以管理员方式运行 notebook

1
sudo python -m IPython notebook