Visual Studio Python Shell - python

Quick question. I'm scratching my head as to why the latest version of visual studio offers tabs with interactive python terminals. As well as debugging terminals all built into the tabs that can be snapped. But when I attach & test run something it just opens a normal python terminal in CMD rendering the built terminals in VS useless. I'm just thinking why lol. They never get used. Is there a way to test run script in the built-in interactive terminals instead of it opening a Python shell?

You can learn more about this feature here, it's just another repl with the convenience of being within Visual Studio. There's also some interesting connections to the editor that you can send selected code lines to the interactive window. I'm not sure how variable scoping works between the two but sometimes you just want to experiment in the repl before integrating it into a working python file.
I mostly put the Immediate Window to use for some interactive exploratory coding when I develop in Visual Studio. This is scoped to where the current breakpoint is set which is good for manipulating things on the fly and getting feedback about my variables and what not.

Related

PyCharm-like console in Visual Code Studio

I wanted to give VSC a try for developing some Python programs, where I only used PyCharm before. One of the most helpful features for me in Pycharm was the PyDev Console, where I can quickly try small snippets of code (think 3-10 lines), and adjust it to work the way I want it to.
I see VSC has a console, but it's much more like the regular IDLE console, where it's kind of hard to write these snippets of code (fixing something 2 lines prior for example is pretty much impossible).
I've been searching for an extension that'll give me a PyCharm-like console experience in VSC, but have been unable to find one. Is is out there? Or is there another way to get to the same result (like setting up a custom console based on the same PyDev console)?
Have you tried Jupyter Notebook and Interactive? There are provided by the Jupyter Extension which is bound with Python Extension.
Open the Command Palette (Ctrl+Shift+P), with the command of:
Jupyter: Create New Blank Notebook
Jupyter: Create Interactive Window
You can refer to the official docs for more details.

What's the difference between executing python code via a task and "Run Python file in Terminal"?

I am just getting my feet wet with Python in VScode. I'd like to know what the difference is between using an extension like the Code Runner Extension (or creating a custom task) to execute Python code and just right-clicking the Python file in the editor pane and selecting, "Run Python file in Terminal or Run current file in Python Interactive Window? What are the pros and cons between the two methods?
its about performance hit
creating a custom task or Run Python file in Terminal
is equal to running python my_code.py in a terminal, and has almost no performance hit
any interactive and second layer for running python has some performance hits, e.g.
Run current file in Python Interactive Window or using an extension like the Code Runner Extension
but don't forget the good things that come along with interactive running and extensions that make these performance hits bearable
The actions you see when right-clicking your file are an implementation of a VSCode extension that you have installed (Python base extension). What ultimately differs depends on the implementation of the specific extension (which you can check only by looking at their implementation) but I think that the main difference is in where your code is getting executed for you, which may be a new interactive window, an integrated terminal or vscode output window.
Run Python file in Terminal simply opens up terminal in vsc in which the python script runs.
The interactive terminal has a ton of fuctions im to afraid of too explain but i have found this neat documentation about that.
https://code.visualstudio.com/docs/python/jupyter-support#_python-interactive-window

How to run my Python module in Visual Studio Code in interactive mode?

I need something similar to "-i" option of original Python interpreter, but to be able to watch variables state, use code completion and other nice IDE features as well.
I've installed appropriate Python extension for Visual Studio Code, but so far by pressing F5 it just runs module once and exit without letting me do anything, pretty much in the same way as C/C++ Visual Studio debugger work. I can use break points, but I want to use power of Python interactive mode.
There is stopOnEntry which starts the debugger when the program stops, but there's no equivalent stopOnExit. If you would like that then please file a feature request at https://github.com/microsoft/vscode-python.

In Visual Studio Code, how do I load my python code to a read-print-eval loop?

I am teaching a class that uses VScode.
I am used to teaching using IDLE, and it is very nice for the students to be able to call their defined functions and run snippets of code in a python terminal, for debugging purposes.
In VScode, they I have been unable to do the same in a satisfactory way.
Option1: I can select all code, right click and run selection/line on terminal. This works for small snippets, but I cannot do it for the whole file (even after selecting the whole file with ctrl-A). On linux, this works, but on windows, it does not, unfortunately (and my students use windows)
Option2: I can use the debug console. This requires adding a breakpoint in one of the last lines of the file, and does not offer tab completion. It works, but is less convenient than IDLE.
Option 3: I can also add the commands to run to the bottom of the file (which is a least preferred alternative, given that is forgoes the interativity of the read-print-eval loop).
Is there any better solution? Installing a VScode extension would not be a problem.
Visual Code is just a text editor like your traditional notepad. to run and debug any kind program you need to install the particular extension for the programming language.
In your case you are using python so you need to install the extension of it. the best one is the "Python" which is developed by microsoft itself. go to your extensions manager and install this extension. right click and click "run python file in terminal" and you are all set.
this will run exactly as they run from the idle(which is default IDE provided by python itself) you can enter the arguments from the console itself. according to me this is the best way to run and debug python programs in VScode.
another way is that VScode shows which python version is installed on your computer on the left bottom side, click on it and the programs will use this interpreter.
out of all the ways listed here and many others, the best method is to run the program in the terminal which is the recommend by python itself and many other programmers.
this method is very simple. what you have to do is open up your command prompt and type the path where python.exe is installed and the type the path of the your program as the argument and press enter. you are done !
ex : C:\Python27\python.exe C:\Users\Username\Desktop\my_python_script.py
You can also pass your arguments of your program in the command prompt itself.
if you do not want to type all this and then just use the solution mentioned above.
hope that your query is solved.
regards

how can I configure PyCharm so that running a Python script file is visible for Python Console

I am writing Python scripts in Pycharm with IPython installed. So I can use Python Console in Pycharm to type Python commands and check the immediate output of the codes. However, when I run a script file after pressing 'Run' button (Shift+F10), all the variables and functions are not visible to the Python Console. This is, however, the feature of Spyder, another popular Python IDE. So here is my question: how can I configure Pycharm so that running a Python script file is visible for Python Console? Thanks
You could also run the part of your code you want to test/check in the console by selecting it and then right clicking and clicking on "Execute Selection in Console Alt-Shift-E". That's what I use sometimes when the debugger is not helpful. After running the code (you can also just "run" functions or classes) the console knows the functions and you can use the same features that Spyder has. However, be aware that when you change the code you need to run it in the console once to update the console definitions!
You can not. But you can use pdb (which will break code execution where you need it and you will be able to do the same things, as in the Python Console).
And, which is better and more powerful, you can use PyCharm's debugger. It represents all available variables in tree-like structures and is really handy.

Categories

Resources