Python test.py:第3行:导入:未找到命令test.py:第5行:标题:未找到命令

Python test.py:第3行:导入:未找到命令test.py:第5行:标题:未找到命令,python,Python,我将一个函数从Curl重写为Python。不幸的是,我一直收到以下错误消息: test.py: line 3: import: command not found test.py: line 5: headers: command not found test.py: line 6: Content-Type:: command not found test.py: line 7: syntax error near unexpected token `}' test.py: line 7: `

我将一个函数从Curl重写为Python。不幸的是,我一直收到以下错误消息:

test.py: line 3: import: command not found
test.py: line 5: headers: command not found
test.py: line 6: Content-Type:: command not found
test.py: line 7: syntax error near unexpected token `}'
test.py: line 7: `}'
我通过pip安装了request,还添加了Shebang。不幸的是,我无法检测到问题

代码如下:

#!/usr/bin/env python
import requests

headers = {
    'Content-Type': 'application/json',
}

params = (
    ('api_key', 'xxx'),
    ('application_key', 'xxx'),
)

data = '{\n   "config":{\n      "assertions":[\n         {\n            "operator": "isInMoreThan",\n            "type": "certificate",\n            "target": 10\n         }\n      ],\n      "request":{\n        "host": "test.com",\n        "port": 443\n      }\n   },\n   "locations":[\n      "aws:eu-central-1" \n   ],\n   "message":" @test.test@test.com @test.test@test.com\\nSSL Certificate for test.test@test.com is going to expire in less than 10 days.",\n   "name":"SSL Test python on test.test@test.com",\n   "options":{\n    "min_failure_duration": 0,\n    "tick_every": 86400,\n    "min_location_failed": 1\n   },\n   "tags":[\n    "test",\n    "application:test"\n   ],\n   "type":"api",\n   "subtype": "ssl"\n}'

# response = requests.post('https://api.datadoghq.eu/api/v1/synthetics/tests', headers=headers, params=params, data=data)

response = requests.post('https://api.datadoghq.eu/api/v1/synthetics/tests?api_key=xxx&application_key=xxx', headers=headers, data=data)

@Mango,我已经知道你在做什么了,根据你分享的错误

生成此错误是因为您试图使用
bash
运行python脚本。如下图所示

[root@localhost ~]# bash test.py 
test.py: line 2: import: command not found
test.py: line 4: headers: command not found
test.py: line 5: Content-Type:: command not found
test.py: line 6: syntax error near unexpected token `}'
test.py: line 6: `}'
尝试使用
python
执行它,并将如下图所示完美执行

python test.py
或者您也可以尝试下面的命令(仅供参考)


@Mango,我已经知道你在做什么了,根据你分享的错误

生成此错误是因为您试图使用
bash
运行python脚本。如下图所示

[root@localhost ~]# bash test.py 
test.py: line 2: import: command not found
test.py: line 4: headers: command not found
test.py: line 5: Content-Type:: command not found
test.py: line 6: syntax error near unexpected token `}'
test.py: line 6: `}'
尝试使用
python
执行它,并将如下图所示完美执行

python test.py
或者您也可以尝试下面的命令(仅供参考)


它的test.py你为什么要问?我在我的环境中运行了相同的脚本,它没有给出错误…我认为这是环境问题,而不是代码问题。请确保在environment path.its test.py中安装并添加了python。您为什么要问这个问题?我在我的环境中运行了相同的脚本,它没有给出错误……我认为这是环境问题,而不是代码问题。请确保在环境路径中安装并添加了python。