简体   繁体   中英

Running python directly in terminal

Is it possible to execute short python expressions in one line in terminal, without passing a file?

eg (borrowing from how I would write an awk expression)

python 'print("hello world")'

python3 -c "print('Hello')"

如上所述使用-c标志。

适用于Python3.6

    python -c "print('hello world')"

You can also run in terminal:

$ python

>>> import numpy as np
>>> np.random.randint(0,10,2)
>>> [8,4]
>>> exit()

This way you can run a notebook in terminal.

You can also use argparse to add conditions/inputs to your python command line, in a previous notebook, integer.py :

import numpy as np
import argparse
parser = argparse.ArgumentParser(description='')
parser.add_argument('--size', dest='size', type=int, default=10, help='number of integers')
args = vars(parser.parse_args())
print(np.random.randint(0,10,args['size']))

$ python integer.py --size=3
$ [0,2,8]

为了完整起见,我发现您还可以将here字符串提供给python。

python <<< 'print("hello world")'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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