python .py file execute and debug parts - python

I have a .py file open in Visual Studio Code. However, I'm missing the jupyter notebooks functionality to execute cells/debug. Is there any way I could execute and debug single parts of the whole .py script?

It would be great if you could specify exactly what you mean by missing the jupyter notebook's functionality, because you could use the #%% magic line depending on the kind of limitation you have.
If your need is just to run code line by line or by chunks you can do so using VS Code quite easily. You just need to have a terminal window open, select the part of the code you want to run, and press ⇧ + ↩(Mac). Maybe your key binding is different, but you can check it on Preferences: keyboard shortcuts under Python: Run Selection/Line in Python Terminal

You can bebug the file by placing breakpoints in it, and then copy & paste portions of the file into the DEBUG CONSOLE to execute them (or press continue, etc.).
https://code.visualstudio.com/docs/python/python-tutorial

Related

How to display clear output in terminal (VScode Python)

When I’m executing a highlighted python code in VScode I see the output in terminal along with actual code. Is there a way to make the terminal display output only and not all lines of code associated with it?
No, because it executes the selected code in interactive mode.
In order to get clean output you need to write your code to a .py file and execute it.
You could creat a new .py file and put the code into this file.
For example, I have a .py file namde Helloworld.py and one line in it.
Then you could run this file by click "Run python file"
Here is the terminal shows:
If the above does not work, I think there may be a problem with your default terminal settings.

How can I force VS Code to run Python processes in a fresh terminal?

In VS Code (VSCodium to be specific), when I click on "Run Python file in terminal" - the little green arrow top right - the same terminal is used for multiple processes. I'd like to run multiple Python processes in parallel. Currently, the only way is to open a new terminal inside VS Code and run the desired file manually by entering its path. Is it possible to reconfigure the "Run" button so that a new terminal will be opened where the current file that's open in the editor gets started?
According to your description, if you want to automatically open multiple new terminals, it is recommended that you set the way of result output: set "console": "externalTerminal", in the "launch.json" file in the ".vscode" folder. After setting, the result of each debugging will be automatically output in a new cmd window.
Update:
For setting the python run button ("Run python file in terminal"), so that the code can be automatically synchronized to run in different terminals. Currently, VSCode does not support this function. The available method is to manually enter the path of the script as you said.

How to open a python file in Cmder Terminal Quicker?

I want to open a python file in cmder terminal quickly. Currently, the fastest way i know how is to navigate to the directory of the python file in cmder terminal and then run it by calling "python file.py". This is slow and cumbersome. Is there a way for me to have a file or exe, that, when i run it (or drag the program onto it), automatically makes the program run in cmder straight away.
Windows 10
Clarification: I'm using cmder terminal specifically because it supports text coloring. Windows terminal and powershell do not support this.
On windows you can go to the directory with the file in the explorer and then simply hold shift as you right click at the same time. This will open the menu and there you will have the option to use the command shell/powershell and then you don't have to navigate to the directory inside the shell anymore and can just execute the python file.
I hope that helps.
Answer: The escape codes just weren't properly configured for the windows terminals. You can get around this by using colorama's colorama.init(). It should work after that.

How to run python script without writing command on cmd again and again

I am practicing python code and executing it through command line. each time i have to execute my python file i go to cmd and write
D:\PythonPractice>python myClass.py
Is there any standard and comfortable way available to execute python code quickly?
If you are executing the same command, with no changes to arguments or anything, you can pack it in a .bat file (windows executable)
Name it something like myscript.bat
D:\PythonPractice>python C:\path\to\myClass.py
Put full path inside it. Now double click will do.
Use PyCharm Software by JetBrains (same company who developed Android studio and Kotlin language), it will help you in many ways .
Run Python with single press of a button.
Add modules easily just with some clicks.
Debugging the code as smooth as possible.
It is Awesome, I am using it for past couple of months.
you can change file association which controls what to do when invoking filename in command line. For instance, when you just type text filename in cmd, notepadd will be opened, for word docs Word or other document editor.
Have a look into following link from google or you can fiddle by yourself starting from Control Panel > Control Panel Home > Default Programs > Set Associations. Select a file type in the list and click Change Program.
Alternatively,you can use any of Python IDE (PyCharm,PyDev etc.) so you will be able to run directly scripts from editor.
With Notepad++, you can also create a shortcut to be able to launch your python script :
Menu Run > Run (or press F5)
Choose the python .exe (default in C:\Program Files (x86)\Python36-32\python.exe
Then add this code next to the path : "$(FULL_CURRENT_PATH)"
This will allow to execute the current file
example of command with Python 3.6 :
"C:\Program Files (x86)\Python36-32\python.exe" "$(FULL_CURRENT_PATH)"
Then save the command, by assigning a shorcut key, it's better with a modifier like SHIFT (SHIFT + F10)
Now you can launch any script with this shortcut.

how to output all the lines into python console in vim?

I have set F2 prompt key with map <f2> :w<cr>:! D:\Python34\python %<cr>,when i open an python file in vim and press F2,the python file will be executed .For a simple example,
here is my python file and opened in gvim .
Now i can't input other python lines ,only thing i can do is to see the result and hit any key to close this window.
What i want is :
when i press F2, (the python file was opened in gvim) ,the python console pop up,and all the files in the python file were copied into the python console automatically,and i can go no to input some lines such as Obj().hello in the python console or go on to edit in gvim ,i am a lazy man ,the gvim and python console all opened waiting to serve me , can i write a vim scripts to achieve the target?
The command :!D:\Python34\python -i % works fine ,i got the ouput
There is still a problem remain,
1)when command :!D:\Python34\python -i % works ,the gvim window will be frozen , i can't drag my mouse to see codes in vim.
2)there is no any python codes in the python console wiondow
So if the program is full of many lines ,and i can't remember the previous content ,worse still, the gvim window frozen ,how can i get the codes?
Avoid blocking
To make the call asynchonous (to avoid that GVIM is blocked during the Python session), use the Windows-specific :!start command:
nnoremap <f2> :w<cr>:!start D:\Python34\python -i %<cr>
List teh codez
I don't know whether it is possible to list the passed source code from the interactive Python debugger. But you can print the file contents before starting it:
nnoremap <f2> :w<cr>:!start cmd /c type % && D:\Python34\python -i %<cr>
Additional tips
You should use :noremap; it makes the mapping immune to remapping and recursion.
As your mapping only works correctly from normal mode, use :nnoremap (or extend it to support visual-mode selections, too).
Maybe Vim plugin Conque will solve your problem:
Installation instrucions are here https://code.google.com/p/conque/
To use just type :ConqueTermVSplit python -i test.py (VSplit is for vertical split - you may use horizontal)
There is no blocking of your window with python code - you may escape interactive mode and switch to your window with Ctrl+W twice
You could approach the problem from the Python angle (2.7).
Keep the file where it is (or save it with some unique name to a temporary directory) and have python load the file directly.
Go to that location in your shell and run python interactively (or have vim spin off an interpreter for you)
Import your file import demo
Experiment with what you have implemented demo.SomeModule().meth()
Make some changes in vim
Reload your python module reload(demo)
Experiment with your code again demo.SomeModule().differentMeth()
You can also have vim create a file with shortcut functions for loading/reloading the file you are working on. When vim kicks off the interpreter, you can have it set this file to the PYTHONSTARTUP environment variable, which is a file the interpreter will automatically load when it starts up. For example, you could have a function called r() to automatically reload the file you are working on.
It's also worth mentioning that reloading modules can be a little weird. If you instantiate some modules then reload the file, only new modules will use the new code; the old modules will run with the old code.

Categories

Resources