1

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

I'm using Ubuntu 18.04.1 LTS.

e.g. 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.

Soroosh
  • 85
  • 8
  • 1
    you can use IPython => https://ipython.org/ – Mojtaba Kamyabi Mar 13 '19 at 11:18
  • you can copy paste or import the code is that what yoiu mean?, if so type python in the terminal then from FileName import * should load a and b in your session – E.Serra Mar 13 '19 at 11:19
  • 1
    Possible duplicate of [How to step through Python code to help debug issues?](https://stackoverflow.com/q/4929251/608639) Also see [Python debugging tips](https://stackoverflow.com/q/1623039/608639) – jww Mar 13 '19 at 11:22

3 Answers3

0

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
Usman
  • 1,983
  • 15
  • 28
0

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.

W Barreto
  • 438
  • 4
  • 11
  • I see >>>, but I want to use my code from python script. I have a=1, b=2 in my python script, now by typing in a+b in Terminal I want to execute a+b. – Soroosh Mar 13 '19 at 11:36
  • Basically, kinda debugging – Soroosh Mar 13 '19 at 11:36
  • Type "a=1" then Enter. Type "b=2" then Enter. Type "a + b" then Enter. Alternatively you can paste the lines from you script or try Jupyter Notebooks. – W Barreto Mar 13 '19 at 12:10
0

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
>>> 
Vlad
  • 8,225
  • 5
  • 33
  • 45