python -c 妙用
前言
python -c 命令还是有用的哈
正文
python的 -c 可以在命令行中调用 python 代码, 实际上 -c 就是 command 的意思
官方文档中解释为(节选自: python docs):
Execute the Python code in command. command can be one or more statements separated by newlines, with significant leading whitespace as in normal module code.
If this option is given, the first element of
sys.argv
will be"-c"
and the current directory will be added to the start ofsys.path
(allowing modules in that directory to be imported as top level modules).Raises an auditing event
cpython.run_command
with argumentcommand
.
简单来说, 就是 python -c 可以在命令行中执行 python 代码, 跟把代码放置在 .py 文件中然后运行这个文件比无明显差别, 我们来测试一下
python -c "print('TTXT')"
随后正确输出了 TTXT
需要注意的是, python -c 后必须跟一个字符串, 因此必须带上引号, 而且在要执行的代码中也不要重叠, 这样会引发错误, 这是因为 python 认不出该到哪里结尾, 例如
python -c "print("TTXT")"
这样就会报错, 输出
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'TTXT' is not defined
我们一般可以使用三引号来标示需要执行的代码, 例如
python -c '''print("TTXT")'''
这样还有一个好处就是也可以执行多行的代码, 例如
python -c '''
import arrow
print(arrow.now())
'''
这样也是可以的, 也可以定义并调用函数
python -c '''
def a():
print(111)
a()
'''
当然也是可以导入自定义的模块或者已经安装的包的
妙用#
那么 -c 有什么妙用呢? 这个要具体问题具体分析, 比如说我在开发中要使用一个第三方包 patool
, 项目在 github, 这个包作用是根据格式解压压缩文件, 但是在调用时发生总是会报找不到 patoollib 模块的错误
官方给的例子如下
import patoolib
patoolib.extract_archive("archive.zip", outdir="/tmp")
排查发现, 该模块的起始文件最上面写定了python的地址, 导致运行时强制指定了python路径而没有使用自己的虚拟环境, 因为 /usr/bin/python
没有安装 patool 包所以报错找不到
py文件节选
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C) 2010-2015 Bastian Kleineidam
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
patool [global-options] {extract|list|create|diff|search|formats} [sub-command-options] <command-args>
"""
def main():
"""Parse options and execute commands."""
try:
argparser = create_argparser()
args = argparser.parse_args()
if args.command is None:
# Python 3.3.1 under linux allows an empty command somehow
argparser.error("too few arguments")
# run subcommand function
res = globals()["run_%s" % args.command](args)
except KeyboardInterrupt:
log_error("aborted")
res = 1
except Exception:
log_internal_error()
res = 2
return res
if __name__ == '__main__':
sys.exit(main())
似乎只能将包的源代码修改, 实际上只要把第一行删除即可, 但是考虑到Docker部署, 就没有更好的办法了吗? 使用 python -c 即可完美解决
我们使用 python -c 在命令行中使用我们指定的 python 去执行模块, 就会使py文件中指定的不生效, 所以调用改成如下即可
code = f"import patoolib; patoolib.extract_archive('{file}', outdir='{unfile}')"
proc = await asyncio.create_subprocess_exec(sys.executable, '-c', code)
await proc.wait()
这样就可以完美的解决问题了, 不用修改原来包的代码
作者:chnmig
出处:https://www.cnblogs.com/chnmig/p/14207100.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!