Pycharm Python Console Won't Run Anything from Script - python

I am unable to get my pycharm console to access anything from my script. Likewise, when I tell a single line of code to execute in the console, an an error like the one shown in the image is thrown. I am sure that something is not configured correctly, and would appreciate any assistance.

You have a generic console open that doesn't have your script loaded into in automatically. You can tell because the name of the console is "Python Console".
Right click on the code editor window, then at the bottom, click "Run file in Python Console". That will create a new console with the script loaded.
I made that into a key combination (in the Key Bindings in settings) because it's such a common task for me.

You don't need a python console. Just right click on the script name and select run or in pycharm terminal : python script.py
It depends on your scrip

Related

How to automatically clear terminal in Visual Studio Code when running the file on terminal?

is there a way to configure VS Code to automatically clear the terminal right before I run a Python file, for example? I searched a lot for that but without success. Whenever I try to run a file in terminal, previous runs are still there in the terminal, and it gets kinda confusing. Notice that I want to clear the terminal when I run the code in terminal (i.e. when I click the play button), not when I run with debugging. My idea is to always have the terminal empty when I run a file. Any thoughts?
Currently in VS Code, it does not automatically clear the terminal data settings, but you could use the command "clear" in the VS Code terminal to clear the previous output data, or click the'kill' icon to delete the current terminal and use the shortcut key Ctrl+Shift+` to open a new VS Code terminal.

How can I fix my integrated vscode termnial

So i'm trying to set up my vscode to run python, it works but the issue is I set it up in a way that when I type ./helloworld.py in the integrated terminal, it opens up pycharm and I don't want that. I want it to open up python shell or output "hello world" in the integrated terminal. how can I fix this problem or how do I change my compiler in vscode?
The best way to run your file is to open your files in the editor window and then move your cursor to the editor window and right click. You get a pane that looks like so.
There is an option to run python file in terminal. Select this. The file will excute in the integrated terminal.
You can try:
python3 ./helloworld.py
This tells your os to use the python3 program to run the file instead of using pycharms.
You can use the python command instead for python 2

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 .

Why doesn't my python script run as a Mac app?

I've written a little GUI with tkinter for my own purposes on my Mac. I've written a two-line bash script to run it, and I can launch it from my desktop by double-clicking an icon. This is fine, but it pops up a terminal window, which I find annoying.
I would like to have it run as an app, without opening a terminal window. I followed the instructions in ccpizza's answer to create an app with Automator. I chose /usr/bin/python as the shell, and pasted in my python code. When I click on the desktop icon, I get an error box that says,
The action “Run Shell Script" encountered an error.
The console log has the error,
LaunchServices: Could not store lsd-identifiers file at /private/var/db/lsd/com.apple.lsdschemes.plist
I saw that /private/var/db/lsd is owned by root and I am the owner of the app. I tried
sudo chown root <appname>
and it doesn't give an error, but I am still shown as the owner of the app.
I've also tried choosing usr/local/bin/bash as the shell script, pasting in my bash script. When I run click the icon, I don't even get an error box, but the same error shows up in the console log.
Is there an easy fix for this? I'm just interested in getting this method to work. I'd rather not do anything more elaborate, like installing py2app or platypus.
I ended up getting platypus. It was trivial to do what I wanted. Just pick "None" for the interface.
If you just want to write a python script and run as a app, you can try pyinstaller.

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.

Categories

Resources