[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
要把Python代码封装成可执行的程序可以通过以下步骤完成:
首先将代码保存为.py文件
然后在代码中添加适当的命令行参数解析器(如argparse),使得代码可以通过命令行接受输入参数
之后再在代码的开头添加#!/usr/bin/env python,这将允许脚本在Unix/Linux/Mac系统中以可执行文件的形式运行
然后将代码打包为一个可执行文件。可以使用PyInstaller、py2exe、cx_Freeze等第三方库来完成这一步骤。这些库将Python代码打包成可执行文件,同时将Python解释器和所有依赖库打包到一个文件中。
最后,保存可执行文件并运行。用户现在可以在终端中运行可执行文件,并在命令行中提供所需的参数。
以下是一个简单的示例代码,可以通过命令行接受一个字符串参数并将其打印到屏幕上:
#!/usr/bin/env python
import argparse
def main(input_string):
print(input_string)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('input_string', help='an input string to be printed')
args = parser.parse_args()
main(args.input_string)
使用以下命令将此代码打包成可执行文件:
$ pyinstaller my_script.py
这将在当前目录中创建一个名为“dist”的目录,并在其中生成一个可执行文件“my_script”。运行该文件可以通过以下命令:
$ ./my_script hello
就可以将输出“hello”到终端中了。