简体   繁体   English

如何在终端中运行一行 Python 命令?

[英]How to run one line Python command in terminal?

I want to get result of a Python function in terminal.我想在终端中获得 Python function 的结果。

I tried to run the command:我试图运行命令:

$ python3 -m uuid uuid.uuid4().hex

And I expect to see the output be something like: '78cbf0fadaa34ff7ac3f7b965965e207'我希望看到 output 类似于: '78cbf0fadaa34ff7ac3f7b965965e207'

Unfortunately I get error:不幸的是我得到错误:

 -bash: syntax error near unexpected token `('

You were close.你很亲密。

  • The flag to run a single command is -c and not -m .运行单个命令的标志是-c而不是-m
  • You also need to import uuid so you can use it.您还需要import uuid以便使用它。
  • You also need to use print() to actually see some output.您还需要使用print()来实际看到一些 output。
  • Finally the whole passed command has to be in quotes.最后,整个传递的命令必须用引号引起来。
$ python3 -c "import uuid; print(uuid.uuid4().hex)"
8e79508445db4aca91bb0990529fdd89

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM