在没有脚本文件的情况下从命令行执行python程序 [英] Execute python program from command line without script file

查看:45
本文介绍了在没有脚本文件的情况下从命令行执行python程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在远程服务器上执行python程序,而不创建脚本.远程服务器不允许我在文件系统上的任何位置创建任何文件.

I want to execute a python program on a remote server, without creating a script. The remote server does not allow me to create any files anywhere on the file system.

python程序具有以下结构,尽管功能复杂得多

The python program has following structure, though the functions are a lot more complicated

def test2():
  print("test2")

def test_func():
  test2()
  print("test_func")

test_func()

有没有一种方法可以直接从命令行执行该程序?
我已经尝试了这两种方法

Is there a way to execute this program directly from command line?
I have tried these 2 approaches

  1. 使用 python -c 选项传递代码.
  2. 启动python交互模式,然后复制粘贴代码以运行.

在两种情况下我都出错.但是,没有用户定义功能的任何代码都可以使用第二种方法执行.是否可以在不创建本地脚本的情况下使上述代码正常工作?

I get errors in both the cases. However, any code without user defined functions is able to execute with 2nd approach. Is it possible to get the code above working without creating a local script?

推荐答案

我找到了一个解决方案,也许会有所帮助,您可以使用 EOF

i found a solution, maybe it will help, you can use EOF

$ python << EOF
> def test2():
>   print("test2")
> 
> def test_func():
>   test2()
>   print("test_func")
> 
> test_func()
> EOF

# output
test2
test_func

您还可以将 python -c ""

$ python -c """
def test2():
  print("test2")

def test_func():
  test2()
  print("test_func")

test_func()
"""

这篇关于在没有脚本文件的情况下从命令行执行python程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆