Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run one line Python command in terminal?

I want to get result of a Python function in terminal.

I tried to run the command:

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

And I expect to see the output be something like: '78cbf0fadaa34ff7ac3f7b965965e207'

Unfortunately I get error:

-bash: syntax error near unexpected token `('
like image 399
akpp Avatar asked Aug 30 '26 20:08

akpp


1 Answers

You were close.

  • The flag to run a single command is -c and not -m.
  • You also need to import uuid so you can use it.
  • You also need to use print() to actually see some output.
  • Finally the whole passed command has to be in quotes.
$ python3 -c "import uuid; print(uuid.uuid4().hex)"
8e79508445db4aca91bb0990529fdd89
like image 149
ruohola Avatar answered Sep 01 '26 09:09

ruohola



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!