简体   繁体   English

test.py:第3行:导入:找不到命令test.py:第5行:标头:找不到命令

[英]test.py: line 3: import: command not found test.py: line 5: headers: command not found

I rewrote a function from Curl to Python. 我将函数从Curl重写为Python。 Unfortunately, I get the following error message all the time: 不幸的是,我一直收到以下错误消息:

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: `}'

I installed request via pip, and I also added Shebang. 我通过pip安装了请求,并且还添加了Shebang。 Unfortunately, I am not able to detect the issue? 不幸的是,我无法检测到该问题?

Here is the code: 这是代码:

#!/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)

@mango5k, I have figured out what you are doing, as per the error what you have shared. @ mango5k,根据您共享的错误,我已经知道您在做什么。

The error is generated because you are trying to run the python script using bash . 生成该错误是因为您尝试使用bash运行python脚本。 As shown below, 如下所示,

[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: `}'

Try executing it using python and will execute perfectly as shown below, 尝试使用python执行它,它将完美执行,如下所示,

python test.py

Or you can also try the below command(Just for reference), 或者,您也可以尝试以下命令(仅供参考),

$ # Assign execution permissions
$ chmod +x test.py
$ # Run the script by using its filename
$ ./test.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ImportError运行Test.py - ImportError Running Test.py 自动完成test.py像git <tab> - autocomplete for test.py like git <tab> System.IO.FileNotFoundException:&#39;在语言的搜索路径:中找不到文件&#39;Test.py&#39;。 - System.IO.FileNotFoundException: 'File 'Test.py' not found in language's search path: .' 语法错误:第2行的文件G:\\ test.py中的非ASCII字符&#39;\\ xe3&#39;,但未声明编码 - SyntaxError: Non-ASCII character '\xe3' in file G:\test.py on line 2, but no encoding declared 如何解决此错误文件“test.py”,第 5 行,在<module> for _ in range(b[0]): TypeError: 'int' object 不可订阅</module> - how to resolve this error File "test.py", line 5, in <module> for _ in range(b[0]): TypeError: 'int' object is not subscriptable ./xx.py:line 1:import:未找到命令 - ./xx.py: line 1: import: command not found Django test.py model object 实例仅限一次测试 - Django test.py model object instance is limited to one test 发生异常:KeyError 'h' File "C:\Users\username\Downloads\test.py", line 19, in dfs, 如何修复? - Exception has occurred: KeyError 'h' File "C:\Users\username\Downloads\test.py", line 19, in dfs, how to fix? &#39;%%file test.py&#39; 在 python 中是什么意思? - what does '%%file test.py' mean in python? 如何在“vim test.py”中编写这种类型的代码 - How to write this type of code in “vim test.py”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM