在Mac OS上使用Python和Requests,向Stackdriver报告部署事件

0 投票
1 回答
675 浏览
提问于 2025-04-18 08:18

我正在尝试运行这个例子,链接在这里:http://support.stackdriver.com/customer/portal/articles/1491725-sending-code-deploy-events-to-stackdriver

import requests
import json

def submit_code_deploy_event():
    """ Submit a code deploy event to Stackdriver """

    headers = { 'content-type': 'application/json',
                'x-stackdriver-apikey': '<YOURAPIKEY>' }

    deploy_event = { 'revision_id': '87230611cdc7e5ff7723a91e715367c553ad1115',
                     'deployed_by': 'JoeyI',
                     'deployed_to': 'production',
                     'repository':  'prototype_dashboard' }

    resp = requests.post( 'https://event-gateway.stackdriver.com/v1/deployevent',
                          data=json.dumps(deploy_event),
                          headers=headers)

    assert resp.ok, 'Failed to submit code deploy event.'

if __name__ == '__main__':
    submit_code_deploy_event()

但是我遇到了这个错误:

Traceback (most recent call last):
  File "test.py", line 2, in <module>
    import requests
ImportError: No module named requests

所以我执行了:

➜  pip install requests
Downloading/unpacking requests
  Downloading requests-2.3.0-py2.py3-none-any.whl (452kB): 452kB downloaded
Installing collected packages: requests
Successfully installed requests
Cleaning up...

但是错误依然存在:

Traceback (most recent call last):
  File "test.py", line 2, in <module>
    import requests
ImportError: No module named requests

我是在Mac OS上运行,使用的是python 2.75
有什么想法吗?
(感觉自己像个菜鸟)

1 个回答

0

你有没有安装过一个和系统不同的Python解释器?在使用OSX开发时,这个问题很常见。我建议你安装一下virtualenv,把所有的依赖项放在虚拟环境里管理。

以下这些方法也可能对你有帮助:

  • which pip来找到pip的位置,然后查看一下它使用的是哪个Python版本。

  • which python找到你的Python版本,看看它和pip的环境是否一致。

  • 如果你使用的是macports或homebrew,确保把默认的Python解释器换成这些版本(这个不会自动替换)。

撰写回答