I am trying to test some code and have created the following variable: 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
Select the code, use Shift and Enter to run the code in the interactive terminal
Type
print(data)
to print out the data
Here is another way
Select the code, right click and select Run Selection/Line in Interactive Window
This will open an interactive window and run the selected code
Type
print(data)
in the box below the interactive windowPress Shift and Enter to run
print(data)
, and print out the data
Tips: The window opened by the second way supports IntelliSense