简体   繁体   中英

How to execute line by line python commands from Linux Terminal?

I searched over the website, but I couldn't exactly find the answer to my question.

I'm using Ubuntu 18.04.1 LTS.

eg in my python script (FileName.py):

#!/usr/bin/env python
a = 1
b = 2

Now, I want to execute it line by line (MATLAB-like) from Terminal. For example when I press a + b , it should return the value 3 .

In the terminal, you type python & press enter key then it shows you >>>> character in the new line. After that, type

//variable declaration,
a = 2
b = 3

//add the numbers,
a + b

Output :

It show you the output like ,

>>>>> 5

If Python is in your environment variables, type "Python" in your terminal screen. If not, type "Python" within Python's folder and you will see ">>>". It works the same way as Idle. Alternatively you can use Jupyter notebooks.

Another option would be to run python with -i flag. From python --help :

-i : inspect interactively after running script;

After the script is finished running, it will take you directly to the interpreter preserving declared global variables.

python -i FileName.py

>>> a + b
3
>>> 

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