Home > Back-end >  Run python Variable in Terminal
Run python Variable in Terminal

Time:09-02

I am trying to test some code and have created the following variable: enter image description here data = numpy.array([[1, -1], [0, 1], [-1.5, -1]])

now, I want to test how this variable is printed by typing 'print(data)' in the terminal. however, I receive error, saying name 'data' not defined.

Doesn't the python terminal recognize variables created in the code editor above? I am using Visual Studio Code.

CodePudding user response:

The terminal won't just recognize variables in the editor, you must execute the code in a python terminal. You can do this by selecting the code you want to execute then pressing command shift p and searching for Python: Run Selection/Line in Python Terminal

CodePudding user response:

The python terminal in VS Code is like you just running the python command in a normal terminal.

If you want an interactive prompt you need to run the file manually with the -i option like this.

python -i path/to/your/script/here

CodePudding user response:

You will need to define data in the python terminal separately, it won't find it from your code unless you run your code.

If you want to see how it will be printed in your terminal by itself then you can just define data the line before in the terminal and then print it.

>>> import numpy
>>> data = numpy.array([[1, -1], [0, 1], [-1.5, -1]])
>>> print(data)
[[ 1.  -1. ]
 [ 0.   1. ]
 [-1.5 -1. ]]

CodePudding user response:

The vscode Terminal uses the Power Shell built into windows. If you just type python in the terminal and hit Enter, you get the same effect as running python in an external cmd window, it just opens the python interactive window and does not run the current python script.

If you want to implement your idea, please follow the steps below.

code

import numpy as np
data = np.array([[1,-1],[0,1],[-1.5,-1]])

Steps

  • Shift and Enter open python interactive terminal

    enter image description here

  • Select the code, use Shift and Enter to run the code in the interactive terminal

    enter image description here

  • Type print(data) to print out the data

    enter image description here

Here is another way

  • Select the code, right click and select Run Selection/Line in Interactive Window

    enter image description here

  • This will open an interactive window and run the selected code

    enter image description here

  • Type print(data) in the box below the interactive window

    enter image description here

  • Press Shift and Enter to run print(data), and print out the data

    enter image description here

Tips: The window opened by the second way supports IntelliSense

enter image description here

  • Related