0

编辑:

问题是我正在尝试从ouath2clientpip 库导入但我有一个名为的当前模块,该模块Whatever使用了本地版本ouath2client- 如果我尝试更新Whatever失败。

所以我想ouath2clientsite-packagesforpubsub 但本地目录ouath2client中使用Whatever模块。像这样尝试:

unique_path = '/root/.virtualenvs/simple_worker/lib/python2.7/site-packages'
unique_path = '/root/.virtualenvs/simple_worker/lib/python2.7/site-packages'
sys.path.insert(0, unique_path)
from google.cloud import pubsub
sys.path.remove(unique_path)

但我仍然得到ImportError: No module named service_account

试图运行一个 pub sub 。收到此错误:

  File "/proj/Backend/WorkerScripts/runRecognizerAlgo/run.py", line 748, in <module>
    from google.cloud import pubsub
  File "/root/.virtualenvs/app_engine_workers/local/lib/python2.7/site-packages/google/cloud/pubsub/__init__.py", line 27, in <module>
    from google.cloud.pubsub.client import Client
  File "/root/.virtualenvs/app_engine_workers/local/lib/python2.7/site-packages/google/cloud/pubsub/client.py", line 19, in <module>
    from google.cloud.client import JSONClient
  File "/root/.virtualenvs/app_engine_workers/local/lib/python2.7/site-packages/google/cloud/client.py", line 17, in <module>
    from oauth2client.service_account import ServiceAccountCredentials
ImportError: No module named service_account

相关点数:

gapic-google-pubsub-v1==0.10.1
google-cloud-pubsub==0.21.0
grpc-google-pubsub-v1==0.10.1
oauth2client==3.0.0

签入oauth2client目录,但我确实看到了service_account

ls  /root/.virtualenvs/app_engine_workers/lib/python2.7/site-packages/oauth2client/
__init__.py   _openssl_crypt.py       _pycrypto_crypt.py   clientsecrets.py   crypt.pyc           service_account.pyc  util.pyc
__init__.pyc  _openssl_crypt.pyc      _pycrypto_crypt.pyc  clientsecrets.pyc  file.py             tools.py
_helpers.py   _pure_python_crypt.py   client.py            contrib            file.pyc            tools.pyc
_helpers.pyc  _pure_python_crypt.pyc  client.pyc           crypt.py           service_account.py  util.py

所以我不知道/google/cloud/client.py试图从哪里导入

尝试直接从 python 控制台导入时

(app_engine_workers)root@worker-test-docker-delete2:/proj/Backend/Framework/Workers# p
Python 2.7.3 (default, Mar 13 2014, 11:03:55)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from google.cloud import pubsub
>>> 

然后它起作用了,很奇怪。

4

1 回答 1

0

pubsub不会同时评估所有导入pubsub本身就是导入的。在您已经unique_pathsys.path.

真正的问题是如何导入具有相同名称的 Python 模块的两个不同版本,这不是 Python 不会让您在不求助于一些复杂技巧的情况下做的事情。由于无论如何这通常是一个坏主意,理想的解决方案是重写Whatever以使用最新版本的oauth2client.

于 2016-12-03T01:00:06.330 回答