How to run code in Pycharm - python

If I go to "tools" and select "python console", and enter several lines of code, how do I execute this? If my cursor is at the end of the script, I can just hit enter. But how can I run the code using keyboard shortcuts if the cursor is not at the end? In Spyder this is done using shift+enter, but I can't figure out how to do it here. I've seen places say control+enter, but that doesn't work. Thanks!

If you use Win 10, 64Bits. Run your codes using Ctrl + Shift + F10 or simply right click on the workspace and click Run from the options.

in mac.
you can use fn+shift+f10 and happy coding with python

From Jetbrains' official documentation:
PyCharm suggests several ways to run a script opened in the editor:
First, you can use the keyboard shortcut ⌃⇧R
Second, you can use the context menu command (here Run Solver), invoked by right-clicking the editor background
Use the main menu Run | Run, Run | Run 'Solver'
Finally, it is possible to run a script from the main toolbar, using the temporary run/debug configuration Solver

Right click on project name / select New / select Python File
Pycharm needs to know you're running a Python file before option to run is available

Related

How to run a few selected lines of code in vscode?

How do I execute run Shift + Enter only the selection of highlighted lines of code in vscode?
Edit: Maybe I should be clearer.
when hitting Shift + Enter the following error :
Unable to initialize device PRN
but when I right click > run python file in terminal the code works.
Select one or more lines, then press Shift+Enter or right-click and
select Run Selection/Line in Python Terminal. This command is
convenient for testing just a part of a file.
That is what is on the VS Code documentation for Python and is what works for me. It is Shift + Enter not Ctrl
Note that this needs the Python 3 extension to be installed.
You can just select the lines and then right click > Run Selection/Line in Python Terminal.
Visual Studio Code supports working with Jupyter Notebooks natively, as well as through Python code files
read this article for any
https://code.visualstudio.com/docs/python/jupyter-support
You could use the jupyter extension, then run cells together rather than whole files
https://code.visualstudio.com/docs/python/jupyter-support

PyCharm focus Python Console for input upon run

In PyCharm 2018.2.5 (Community Edition), I have bound a keyboard shortcut to "Run File in Console". This launches opens the console and runs the file, but does not focus the console for input and anything I type goes into my code. (Sometimes when I use the switcher to focus the Python console it will try to search console variables when I type.)
Is there a way to automatically focus the Python console for input when it is launched?
No for PyCharm there is no way to automatically focus the Python console for input when it is launched.
In Notepad ++ i can do the following.
Enter my code
press shift+a to execute a macro which saves the file and execute it with Python 2.7 (or 3). I also made this macro a menu item.
When the console runs and the script asks for input you are already in focus and can directly put in our entry without having to click and highlight the window or so as it is necessary in PyCharm.
So the step between writing the code and executing it to see what it does exactly one hotkey. Its not one hotkey and a mouse click or what ever no.... its exactly one press of a button. This makes learning very effective because there are no detours.
I looked long in PyCHarm but i could not find this functionality or a workaround. So the answer to your question is "With PyCharm this is not possible."
Here is how to do it in Notepad ++.
Download Notepad ++
In N++ install the NppExec Plugin.
Create the "save and run in python" macro: In NppExec create a script as shown here:
NPP_CONSOLE 0
npp_save
npp_run cmd /K C:\Python27\python.exe "$(FULL_CURRENT_PATH)"
Save your macro script as say "Save and run in Python Selfmade"
In the hotykeys menu assign your macro to a new hotkey.
In the Npp_Exec menu click on Advanced Options and create a menu entry for your macro. To do this select the script you created under "Associated Script" and then click on "Add/Modify"
Another editor that offers you this quick and direct run functionality is "Atom" with its many plugins.
The shortcut to switch between the code and console is Alt+4; by clicking on the console or using this shortcut, future consoles you open will be selected by the input.
Yes it IS possible
in the run console change the view mode to Window (click the cog)
add the following lines to the benning of your source code
import pygetwindow as gw
win = gw.getWindowsWithTitle('Run')[0]
win.activate()
now it will automatically give focus to the run window... see my youtube demo.. https://www.youtube.com/watch?v=MhmltlUjU3k
i have just posted something related to this. It is needed to distinguish between "Python console" and "python or debug console" within the keymap options. The first one focus at the console but without automatically move caret to it, the later does it.
See more detailes in this answer.
It's very easy! Just make macros with two actions:
1 - Run command,
2 - Activate run tool window. And with keymap in settings create a shortcut. For example, I do this with shift + f10 .

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.
:-)

Running code in PyCharm's console

Are there any smooth way to run Python scripts in the PyCharm's console?
My previous IDE - PyScripter - provides me with that nice little feature. As far as I know PyCharm has 2 ways of running script in console:
1) Select a bunch of code and press Ctrl+Alt+E.
2) Save the code in a file and import it from the Console.
Are the any way to do it by pressing "Run" or "Debug" buttons? I need to see the result of my script in the console and all variables available to manipulate.
In the Run/Debug Configuration, add -i to the interpreter options. This will stop it from closing a python session even after a successful run. I.e. you will be able to see all the variable contents
Run -> Edit configuration -> select the script you want to run and give it a display name -> OK
Now you can run it with the green "Run" button. The green bug button (next to the run button) will run it in debug mode.
Remark: next to the run button you can monitor the script/configuration you run by selecting it's display name.
If you create run-time configuration then after pressing the run button (green "play" icon) you will get the desired result: your code will give you output in a console which opens atomatically at the bottom.
You can create many different configuraions for run and debug within a single project.
Here is a link from PyCharm's web help which covers the run/debug configurations:
http://www.jetbrains.com/pycharm/webhelp/run-debug-configuration-python.html
A possible way of manipulating the variables using debug mode and evaluate expression window is added in comment but I missed one detail: to see the result of your interactive code execution you have to switch from Debugger to Console output; mode tabs are on the top-left side of the bottom pane.
The way I do it is a create my script in the editor, call it myfirst.py, eg
print "Hello"
a= 1234
then in the console I run:
reload (myfirst)
To check on the variables use:
>>> myfirst.a
Not perfect but that's pyCharm for you. If you want a pyScripter like experience which I think is more useful you can try spyder2.
It may also be a good option for you to use the magic command
%run scriptname.py
in the ipython console (including the %-character). Compared to Cntrl + Alt + E you also get clean information on errors in your code.
Due to autocomplete it's also typed in a second and you can use this in any environment with an ipython shell. For me as a scientist who often uses python to explore data, this is a good option.
With your program in the editor:
Run - Edit Configurations
Then under Execution check the box for "Run with Python console".
When you run the program from the green arrow it will run in a console. After the run all your objects are available to examine and play with.

how do I launch IDLE, the development environment for Python, on Mac OS 10.7?

I am running python 2.7.1. I can't figure out how to launch the IDLE IDE. I am told it comes already installed with python, but I can't find it using spotlight.
In the stock Mac OS X python installation, idle is found in /usr/bin, which is not (easily) accessible from Finder and not indexed by Spotlight. The quickest option is to open the Terminal utility and type 'idle' at the prompt. For a more Mac-like way of opening it, you'll have to create a small app or shortcut to launch /usr/bin/idle for you (an exercise left to the reader).
When you open up a new terminal window, just type in
idle
Then you will see a little rocket icon show up as IDLE loads
Then the Python shell opens up for you to edit
I think the shell command is
python -m idlelib.idle
but i am not a mac user so i can't test.
One way to run IDLE from spotlight or an icon in the Applications folder is to build a quick Automation for it. As mentioned by other commentators, this probably isn't necessary for Python 3, as it creates a shortcut automatically, and some hand-installed versions have tools to do this automatically. But if you want to roll your own:
You'll need to know the terminal command to open your version of IDLE. On my Mac right now (early 2016), running python 2.7.10, it is "idle2.7"
Using spotlight, or in the Utilities folder, open "Automator"
Choose an "Application" type document.
Make sure "Actions" is selected in the gray bar, upper left.
In the actions column, find "Run Shell Script" and double-click it, or drag it to the workflow area on the right.
Enter the terminal command in the parameters box that appears.
Save your automation (I called mine "IDLE" and put it in the Applications folder, to make it easy).
It's now available (as soon as spotlight indexes it) via all the normal methods. The only side-effect will be that while it's running, your menu bar will have a spinning gear over in the tray area next to the clock. This indicates an automation workflow is running. Once you close IDLE, it will go away.
first to launch the terminal CMD+space
second to input idle3
the idle will be activated automatically.
After you launch idle from the command line (make sure idle shell window has focus), click File, click "New File". A new window opens; this is your editor.
Type your program in the editor. Click "File", click "Save As...". Save your file somewhere with any name you choose, and a ".py" extension to the file name.
Click "Run", click "Run Module" (or, F5). Assuming no errors, the results will appear in the Shell window. Edit your file & repeat as necessary.
The answer of Matthewm1970 works like a charm!
And if you add an & to your shell command, the automation script will end immediately. There is no spinning gear. Like so:
/usr/local/bin/idle3.5&
Note the ampersand.
Cheers.
-melle
open Terminal
type "idle" and press enter
right click on "Python" icon > Options > Keep in Dock
so for python 3.4.3 in applications a folder named "python 3.4" click that and click IDLE.
for python 2.7.9 go here https://www.python.org/downloads/ and get 2.7.9 and a folder named "python 2.7" click that and click IDLE.
As to the earlier questions about starting IDLE: you can certainly start it from the command line. Also, if you installed Python using Homebrew, you can run 'brew linkapps' (from the command line); that will place an app for IDLE (among other things) in Launchpad (Applications folder).

Categories

Resources