Execute a default .py file in PyDev - python

I'm doing a Python project in Eclipse with PyDev. I have a "main" file and some "class" files. I primarily work in the classes, but I need to execute the code via the main file.
At the moment I need to either switch to the main file, or use the drop down menu of the run/debug-button to execute the main file. Pressing the button or using F11, just runs the class file (which is useless)
Is there a way to let PyDev always run a specific file in a project, or perhaps always run the last file executed (which probably will be the best solution).
Thanks in Advance :)

Surely (relaunching the last launch was actually the default in in Eclipse 3.2, never understood why they changed that)...
You can have that back in the preferences at: window > preferences > Run/Debug > Launching and set the Launch Operation to Always launch the previously launched application.
So, Ctrl+F11 will relaunch the last launch and F11 will debug the last launch...
And to launch the file initially you may:
Use a shortcut: F9 to run the current opened file
Go to the menu: Alt + R + S + The number of the Run you wish (It can be Python, Jython, unit-test, etc).
If you are using unit-tests, you could use: Ctrl+F9 to run the unit-tests from the current module (and even selecting which tests should be run).
Relaunch some other launch you did previously: Alt + R + T and choose a previous launch to relaunch.
You may also launch in unit-test mode all files beneath a folder by right clicking the folder > run as > Python unit-test
(reference: http://pydev.org/manual_101_run.html)
And if you use unit-tests, you may also want to take a look at: Continuous unit testing with Pydev (Python and Eclipse)

Related

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 can I run the current file in PyCharm

How can I run the current file in PyCharm? I would like a single hotkey that will execute the current file (whether normal file, scratch file, or scratch buffer) using the project default python interpreter. I would like to NOT have to create a custom run configuration, but just launch with the default Python configuration. Is such a thing possible?
As it turns out, the action I was seeking is "Run context configuration" (or "Debug context configuration" for debugging). The default key binding on Windows is ctrl+shift+f10, or ctrl+option+R on Mac, as Ev. Kounis pointed out, although you can bind it to any key you like.
These settings can be found under the "Other" section in File->Settings->Keymap. The easiest way to find them is to simply use the search box.
Just right click on the tab of your file and click run "file name" or you can press CTRL+shift+F10
The key combination you are looking for is Ctrl + Shift + F10. This will run the current script with current being the one displayed in the viewer.
There are several alternatives:
Run context configuration from editor shortcut:
Mac: ⌃⇧R
Windows/Linux: Ctrl + Shift + F10
You can right click on the file, and then run Run 'filename'.
You can also run the shortcut to Find Action and then type run:
Mac: ⇧⌘A
Windows/Linux: Ctrl + Shift + A
If the current file was just run, you can simply use the Run shortcut:
Mac: ⌃R
Windows/Linux: Shift + F10
References:
PyCharm Shorcuts for Mac OS X
PyCharm Shorcuts for Windows and Linux
Alt+Shift+F10 and then select the script you want to run.
After that Shift+F10 will run the last script that has been run.
Keyboard shortcuts can be different on some machines. So you can just click right key on mouse and then "Run "(also you can select part of code and do the same)
File->Settings->Keymap->Run->Run and see your current keymap
Basically, if you just need to run the current .py file in PyCharm. Right-click inside the file, and you can click the "Run file.py" button, and it also tells you the shortcut which on Mac is Control + Shift + R.
To Run/Execute current python file in Pycharm use following keys in windows machine
Shift+Ctrl+F10
OR
for selected code (specific line that are selected/marked)
Shift+Alt+E
Running file with Ctrl+Shift+F10 or via Run menu has some disadvantages. For example, you cannot use the custom redirect input file.
I have created a project it is that will actually allow you to use currently opened py file when you press run / debug in a standard way. My project will remain actual at least until jetbrains fix non working macros in configurations. See bug report.
As of PyCharm 2022.2 there's a built-in "Current File" configuration.
When intalling on Pycharm select :
Add launchers dir to the PATH: Allows running this PyCharm instance from the Console without specifying the path to it.
:-)

Visual Studio Code - python console

I'm using visual studio code with standard python extension, my issue is that when I run the code the python interpreter instantly closes right after and I only see the output which means that if I create some data structure I have to create it every single time. Is it possible to leave the console open after running the code and maybe running multiple files in the same python interpreter instance?
I used to use spyder which is entirely doing what you want (probably like PyCharm)...
Then I briefly tried VS Code, and it is quite easy to make it behave that way too.
First make sure you have an integrated terminal open (or do Ctrl+`, or View > Integrated Terminal), then in that terminal launch ipython.
Now, when you use the commands from Don Jayamanne's Python extension (Ctrl+Shift+P to open commands palette):
"Run Python File in terminal"
"Run select/line in Terminal"
It will run the line inside the ipython console directly. So running the entire file will call python module.py inside ipython, and thus fails.
So to make this work simply create settings to map which command is executed when "Run select/line in terminal":
Open Language specific settings (Shift+Ctrl+P, search "Configure Language specific Settings...")
Pick up Python
Now I would suggest to make change only in your workspace settings (top-right tab) to keep default behaviour in other cases
so add in WORKSPACE SETTINGS:
(keep in mind it is just a simple/stupid workaround)
{
"python.pythonPath": "run"
}
Now when runing whole file, it will use ipython function run within the ipython terminal that we launched, thus keeping all your workspace variables.
Also, if you run some line of code with "Run Select/Line in Terminal", the ipython session of that terminal keep all variables.
This allows live debugging, without actually going in the debug mode.
When you run a program, it runs until it ends. Then it closes. If you want it to stay live longer, you can make a program which does not stop until told so, e.g.
while True:
something = raw_input('Write something: ')
print('You wrote: %s' % something)
if something == 'bye':
print 'bye.'
break
This will run until user writes "bye".
I'm quite late to this conversation, but a workaround I use is to put a pass statement at the end of my file, then add a breakpoint to it. I then run it in the debugger and can access all of the variables etc.
This allows most of the functionality that I used to use in the PyCharm python terminal, such as exploring data structures, checking out methods, etc. Just remember that, if you want to make a multi-line statement (eg. for a loop), you need to use Shift-Enter to go to the next line otherwise it'll try to evaluate it immediately.

Run IPython by default and show at startup in Eclipse PyDev

As there were no satisfying answers in the following links
Set default directory of Pydev interactive console?
Pydev - startup
pydev console path for the active editor
I'm asking whether there is a way to have IPython as integrated console, opening up at start up and being the console, where the modules are run by default, just like in RStudio.
Well, there's no feature to open it at startup by default, but you can do Ctrl+Alt+Enter to first open it and after it's opened, do Ctrl+Alt+Enter again to execute the current file in that console...
Alternatively, you may also use F2 to send the current line to the console (and jump to the next line, so, it's easy to send a block with multiple F2 keystrokes -- and it should also fix for the indentation required).
See: http://www.pydev.org/manual_adv_interactive_console.html for more details.

Is there such a thing as startup file in PyDev under Eclipse?

I have created a new project in Eclipse, PyDev.
If I were in VS2010, I would mark a project as startup project, and whenever I click F5, it runs.
I want to mark one .py file to be my so called Main, and that each time I click run (F11), it will be the one to run, and not the current that is being edited.
Is it possible?
Go to run -> run configurations
In the menu on the right you should have Python Run option. Create new run configuration by double clicking.
On the right you can select the project you want to run and it's main module. The other tabs can be used to configure arguments, python interpreter used to run the project, etc..
Thanks to #soulcheck, I have found the problem.
It seems that the problem is that Eclipse 3 tries to launch current file instead of the main.
Here is the solution:
Whats the shortcut to Debug in PyDev using Eclipse

Categories

Resources