PyCharm-like console in Visual Code Studio - python

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.

Related

quickly try line of code and work with variables in visual studio code

I am more used to Spyder and very new in vsc. In Spyder you have a python console where you can quickly try a codesnipped like:
print(len(a))
without having to run the entire code.
Is that also possible in vsc? I have not seen anything.
In addition, I can only see the variables in the debug mode. In Spyder I have all variables after one run at hand and can try and experiment on the fly with them. Is there another Variable-viewer plugin or how could that be enabled?
You can use this extensions of python. It has a Jupyter Notebook feature that comes with a variable explorer. It is recommended to also add the Jupyter extension as well (they work together just fine), you can find it here.
You can read this documentary about how to use Jupyter with visual studio code, you can see that it shows how to display the variables the way you wanted.

iPython automatically at launch in Visual Studio Code on Os X

I would like that iPython run automatically when I launch VSC instead of typing ipython and press enter in the terminal. The answer here How to set ipython/jupyter as the default python terminal for vscode? doesn't work as it is for windows but it shouldn't be really different. Also, is there something similar to the 'Execute' button in Spyder instead of typing %run filename ? Thanks !
I presume you mean you want to run the "Python Interactive Window" and not just an iPython console on startup
There is currently no way to run it on startup. At least no way without writing another extension that would run a command when opening a workspace. It would be simple for us to add one though. Probably a workspace setting. Can you log an issue here:
https://github.com/Microsoft/vscode-python/issues/new
For you second question, 'Execute' in spyder, we have 'Run Current File in Python Interactive Window'. This works on any python file. You can get to it through the context menu on a file or through the command palette.
Sadly the nice workflow of spyder is not provided by any official extension at the moment (as far as I know).
But you can implement the basics easily on your own by writing an extension. Even with no experience in TypeScript you can quickly build an extension which starts an IPython console as soon as you open a python file. I also managed to execute a startup script which implements the runfile method. VS Code also allows keybindings for your functions, so that you can almost work like you can with spyder.
Spyder modified the IPython terminal quite a bit though, so it won't feel exactly the same. But after all, everything there is open source so you could implement it yourself, which is what I'm trying to do in my free time.

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

Visual Studio Python Shell

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.

notepad++ run selected code in python console seamlessly

I often use R to analyze data, and really enjoy Notepad++ along with NppToR. Especially, NppToR enables to run a part of code without much hassle. I just highlight a snippet of R code to run and press F8. Then the code magically runs in R console.
Now, I am required to use python to analyze data. I know that ipython is great to work interactively, but it is always very annoying to copy a snippet of python code and manually paste that into ipython console. Also, indentation is often mixed and thus the entire lines are failed to run. Note that I want to run 'selected' lines of codes, not the entire file.
So I am looking for a program/plugin/macro similar to NppToR, but working with python/ipython console. I have searched the web, but couldn't find such one. Some plugins are nice, but not exactly what I want. For example, Python Script enables extending Notepad++ itself, but not outside. Various other 'Run' extensions enables the entire file to be run in python.
You can customize the editor IPython uses for the edit command, and configure IPython to use Notepad++. IPython 0.11 creates a .ipython/profile_default folder in your user folder (in my case C:/Users/zk/.ipython/profile_default). To configure IPython to use Notepad++ create .ipython/profile_default/ipython_config.py with the following:
c = get_config()
# use DOS style path, C:/PROGRA~2 on my 64-bit system points to C:/Program Files (x86)
c.TerminalInteractiveShell.editor = 'C:\PROGRA~2\NOTEPA~1\NOTEPA~1.exe'
# set editor for Qt Console, if you wish to use that
c.IPythonWidget.editor = 'C:\PROGRA~2\NOTEPA~1\NOTEPA~1.exe'
You can then start up IPython and use the edit command to run Notepad++ from IPython, saving and closing Notepad++ will execute the file in IPython.
If you don't mind installing PyQt4 and pyzmq (and I believe pygments, at least for IPython 0.12-dev), IPython's Qt console works really well (frankly the nicest interactive environment you can get for Python). You can paste directly into IPython and it'll adjust indentation for you (shifting over padded code).

Categories

Resources