I used to use Pyzo for Python coding and decided to give VS Code a try because it is more feature-rich. I came across one huge annoyance, however. In Pyzo, I am used to code „interactively“ as Pyzo executes code in an interactive shell (https://pyzo.org/features.html).
I would like to replicate that in VS Code, but so far had no luck. With the Microsoft Python extension installed, the closest I can come is to select the whole code, right click and then click on „Run Selection/Line in Python Terminal“. For long scripts, however, this is very, very slow as it first prints each line to the terminal and then executes it line by line. Pyzo seems to operate silently.
Do you have a solution? I think, VS Code would be much faster if it did not print each line to the terminal first.
Best
I use Python Interactive window and find it very useful. It is not an immediate REPL but gives you the time to write a block of code and execute it with a key stroke.
At the bottom of the interactive pane you have a command line where you can type like a REPL. It is a temporary cell (multi line)
Use Code Runner(extension available in the marketplace of VS Code) or run a bash script in the terminal (python -u [name of the file])
Related
So I am relatively new to programming and used to use Python's own "IDLE". After I run a ".py" file with IDLE, I am used to getting a python shell or a command window, I don't really know the terminological name for it, where I could play around with the objects inside the script.
For example, if I had a list A=[1,2,3] inside the program, after I run it I get a command console that says ">>" and I can say ">>A" which gives me "[1,2,3]" and I can add elements to A etc.
Now, I want to start using VS Code but I can't seem to find that particular thing. I have a terminal where I can run python code if I give the command "python" first, but It doesn't seem to effect anything inside the script.
I want to use that to see if some objects are working fine and to check their types etc. I add more lines to code after I try from there first, if that makes sense.
Sorry for really bad terminology, I really don't know the names but I can try even more if it's not clear.
Thanks a lot in advance.
Are you looking for the Integrated Terminal of VS Code?
Here are some ways to open the terminal:
Use the ⌃` keyboard shortcut with the backtick character.
Use the View > Terminal menu command.
From the Command Palette (⇧⌘P), use the View: Toggle Integrated Terminal command.
In the window that shows up, enter python and you'll get the Python shell you're looking for.
Try using the integrated terminal inside vs code and make sure that python and pip are properly configured. Type python in the command line and make sure the terminal points to the same folder where your program file is located.
Currently, in Visual Studio Code (under Windows 10 64bits), at a Python file called path\myfile.py, if one clicks with mouse right-button for context menu and then chooses 'Run Python File in Terminal', an integrated CMD terminal is open and file is automatically run there with:
python.exe path\myfile.py
After the file stops running, one is naturally left at the integrated CMD cursor.
This behavior is quite different, for instance, from what one has with an IDE like Spyder. There, when you run code (e.g. with F5), at the end one is left still at the Python cursor and can access content of variables created when code was run.
Is there a way to achieve a similar behavior in Visual Studio Code?
You can configure VS Code Python extension to use the -i command line option
Described in https://docs.python.org/3/using/cmdline.html#cmdoption-i
You only have to add the setting bellow (inside settings.json file)
"python.terminal.launchArgs": ["-i"],
This will execute the command python.exe -i path\myfile.py.
I don't know if it is a new feature, but I've been using it for while.
If you would like to use the terminal IPython, like in Spyder, you can use a different set of options, as the following:
"python.terminal.launchArgs": ["-m","IPython","-i"],
With these, VS Code will execute the command python.exe -m IPython -i path\myfile.py.
Then, it will run IPython module as a "script" (with -m option), which will use the options -i path\myfile.py, i.e., IPython will run the file and remain opened.
BTW, another thing is: you can run "cells" in Spyder's integrarted terminal (regions of code with #%%). But in VS Code it seems you can't.
I've made a question with a "work around" to run cells of Python files in VS Code Integrated terminal, which is posted Here
Yes. Open a terminal window and it's like a terminal window on your computer. You can type python filepathandname and the python script will execute like it does from the command line.
The closest you can come is to run the code under the debugger and set a breakpoint at the end to pause the exiting of the execution. Otherwise feel free to file a feature request at https://github.com/microsoft/vscode-python.
I would like to know if there is way to figure out (from the IPython console) the .py script that was used to run/execute the python commands interactively and thus got printed into the Ipython console.
For eg.
From the below screenshot, looking at the 3+3 in the Ipython console, I can see that it came in when command from untitled2.py was executed.
However when the scripts get long, and IPython output gets long and you are often shifting scripts on the left side, it can be hard to keep track.
So i was wondering if there is a way i can quickly execute some command or view some setting on Ipython console that can tell me that the above line came from untitled.py.
(Spyder maintainer here) There's no way to know from which Editor you're executing some portion of code, sorry.
However, you could use dedicated consoles (under Preferences > Run > Execute in a dedicated console) to have one console per file you execute, as long as you use F5 to run each one of them.
You could edit each .py script, so the first thing it does is to print its name and functionality.
untitled2.py:
print('untitled2.py')
...
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
I'm using visual studio code with DonJayamanne python extension. It's working fine but I want to have an interactive session just like the one in Matlab, where after code execution every definition and computational result remains and accessible in the console.
For example after running this code:
a = 1
the python session is terminated and I cannot type in the console something like:
b = a + 1
print(b)
I'm aware that the python session can stay alive with a "-i" flag. But this simply doesn't work.
Also every time I run a code file, a new python process is spawned. Is there a way to run consecutive runs in just one console? Again like Matlab?
This sounds really essential and trivial to me. Am I missing something big here that I can't find a solution for this?
I'm the author of the extension.
There are two options:
Use the integrated terminal window (I guess you already knew this)
Launch the terminal window and type in python.
Every statement executed in the REPL is within the same session.
Next version will add support for Jupyter.
Please have a look here for some samples of what is yet to come:
#303
Screen samples and more
I added the following lines into user setting file, then it works.
Select some lines of python code, then right-click and select Run selected code in python terminal
Solution 1: Will start iPython terminal
"terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",
"terminal.integrated.shellArgs.windows": ["/K ipython"],
Solution 2: Will start a terminal like "python -i"
"python.terminal.launchArgs": ["-i"],
The following line will solve your problem.
"python.terminal.launchArgs": ["-c","\"from IPython import embed; embed()\""]