为什么 'sudo pip install' 如果 'pip install' 有效? (HelloAnalytics.py 的问题)

Why 'sudo pip install' if 'pip install' works? (Issues with HelloAnalytics.py)

Google 提供了一个示例 HelloAnalytics.py 来演示 google-api-python-client.

的用法

在标题 "2. Install the client library" 下方他们写道:

Use pip, the recommended tool for installing Python packages:

sudo pip install --upgrade google-api-python-client

我想知道为什么我应该使用 sudo 而一个简单的 pip(没有 sudo)似乎工作得很好(在我的 Mac 上):

% pip install google-api-python-client
Collecting google-api-python-client
  Using cached google_api_python_client-1.9.3-py3-none-any.whl (59 kB)
Collecting six<2dev,>=1.6.1
  Using cached six-1.15.0-py2.py3-none-any.whl (10 kB)
Collecting google-auth-httplib2>=0.0.3
  Using cached google_auth_httplib2-0.0.3-py2.py3-none-any.whl (6.3 kB)
Collecting google-api-core<2dev,>=1.18.0
  Using cached google_api_core-1.20.1-py2.py3-none-any.whl (90 kB)
Collecting uritemplate<4dev,>=3.0.0
  Using cached uritemplate-3.0.1-py2.py3-none-any.whl (15 kB)
Collecting httplib2<1dev,>=0.9.2
  Using cached httplib2-0.18.1-py3-none-any.whl (95 kB)
Collecting google-auth>=1.16.0
  Using cached google_auth-1.17.2-py2.py3-none-any.whl (90 kB)
Collecting protobuf>=3.12.0
  Using cached protobuf-3.12.2-cp37-cp37m-macosx_10_9_x86_64.whl (1.3 MB)
Collecting pytz
  Using cached pytz-2020.1-py2.py3-none-any.whl (510 kB)
Collecting requests<3.0.0dev,>=2.18.0
  Using cached requests-2.23.0-py2.py3-none-any.whl (58 kB)
Requirement already satisfied: setuptools>=34.0.0 in ./venv/lib/python3.7/site-packages (from google-api-core<2dev,>=1.18.0->google-api-python-client) (41.2.0)
Collecting googleapis-common-protos<2.0dev,>=1.6.0
  Using cached googleapis_common_protos-1.52.0-py2.py3-none-any.whl (100 kB)
Collecting cachetools<5.0,>=2.0.0
  Using cached cachetools-4.1.0-py3-none-any.whl (10 kB)
Collecting pyasn1-modules>=0.2.1
  Using cached pyasn1_modules-0.2.8-py2.py3-none-any.whl (155 kB)
Collecting rsa<5,>=3.1.4; python_version >= "3"
  Using cached rsa-4.6-py3-none-any.whl (47 kB)
Collecting certifi>=2017.4.17
  Using cached certifi-2020.4.5.2-py2.py3-none-any.whl (157 kB)
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1
  Using cached urllib3-1.25.9-py2.py3-none-any.whl (126 kB)
Collecting chardet<4,>=3.0.2
  Using cached chardet-3.0.4-py2.py3-none-any.whl (133 kB)
Collecting idna<3,>=2.5
  Using cached idna-2.9-py2.py3-none-any.whl (58 kB)
Collecting pyasn1<0.5.0,>=0.4.6
  Using cached pyasn1-0.4.8-py2.py3-none-any.whl (77 kB)
Installing collected packages: six, cachetools, pyasn1, pyasn1-modules, rsa, google-auth, httplib2, google-auth-httplib2, protobuf, pytz, certifi, urllib3, chardet, idna, requests, googleapis-common-protos, google-api-core, uritemplate, google-api-python-client
Successfully installed cachetools-4.1.0 certifi-2020.4.5.2 chardet-3.0.4 google-api-core-1.20.1 google-api-python-client-1.9.3 google-auth-1.17.2 google-auth-httplib2-0.0.3 googleapis-common-protos-1.52.0 httplib2-0.18.1 idna-2.9 protobuf-3.12.2 pyasn1-0.4.8 pyasn1-modules-0.2.8 pytz-2020.1 requests-2.23.0 rsa-4.6 six-1.15.0 uritemplate-3.0.1 urllib3-1.25.9

那个样本是不是已经过时了? (他们使用 print 而没有 () 尽管已经 Python 2.7 seems to be deprecated for google-api-python-client which confused others。)

我解决了这个问题,但仍然得到 ModuleNotFoundError: No module named 'oauth2client'。我说的对吗,失踪的 sudo 不是原因?

嗯,他们没有解释 VIEW_ID 是什么以及从哪里得到它。

它不需要 sudo 就可以工作,因为您似乎 python 安装在您的用户具有写入权限的位置。

Requirement already satisfied: setuptools>=34.0.0 in ./venv/lib/python3.7/site-packages (from google-api-core<2dev,>=1.18.0->google-api-python-client) (41.2.0)

这完全取决于您安装的方式和位置 python。 mac os 发行版 python 安装在 root 拥有的 /Library/ 中。所以如果你想安装一个新的包,你将需要使用sudo。