Blank python IDLE window when opening python file on macOS - python

For some reason, whenever I try to open a python script that has around ~450 lines of code, IDLE's windows simply appears empty and when I try to run it doesn't run anything either... However, if I try to run a smaller program it works for some reason. Does anybody know how I am able to run a python script on my Mac besides IDLE or how to fix the IDLE window being blank whenever I open a file?

To learn how to run Python Python programs on a Mac, read Chapters 1 and 4 of Python Setup and Usage. Running from an IDLE editor is just one way.
IDLE uses the tkinter module which wraps the tcl/tk GUI framework. Did you read and follow the instructions of how to replace the buggy tcl/tk that Apple supplies? Read the entire page before starting! Ignoring this page is the most common reason people have problems with IDLE on the Mac. There are a few others, but I do not remember one that matches your description.

Related

VS Code had a problem in running the code

I tried to run a python program in VS Code. But my program didn't run. The terminal opened and a weird arrow was there in the terminal. This is the screenshot of that.
This is the weird arrow and the program is not running. Any ideas why this is happening and how to fix it?
Thanks in advance.
Firstly, the arrows are included in the default python IDE means that VScode ran the command to execute your code. Give your pc a restart. Now, let us check if python is working or not or VS code is having some trouble. Type the following command in cmd to execute the code-
python "$PATH"
Rember to replace $PATH to the path of the file i.e where your file is stored. For eg. I've my python files stored in D drive in a python folder, so I'll use-
python "D:\Python\Hello.py"
If this works, python is working fine and if not, try reinstalling python and check the box which says Add python to Path or Environment variables. Then open VS code try to run the program again. But click the button only once and be patient because clicking it multiple times causes execute the same command again and cause a problem. It's my personal experience. Wait 5 minutes. Not works. Don't worry, there's a problem with the run extension you are using. I'll recommend the Code runner by Jun han. I personally use it. Type this in the extension search box-
formulahendry.code-runner
Install it and then try again.
Kill the terminal, and retry. If not work, restart the VSCode.

What is the difference between running python in terminal and in IDE?

I'm new in Python. I'm using Windows 7.
When I install pip on my computer, I need to use the installing program: get-pip.py.
I found 3 ways to run this .py file:
In cmd, type "python get-pip.py"
Open it with IDLE and F5
Double click get-pip.py
I have two questions:
The only way can install it is "1. In cmd, type "python get-pip.py". I wonder what's the difference between 1 2 and 3?
Usually, when I double click .py file, it is usually opened by idle(default). But this time, when I double click get-pip.py, it begins running as if a .exe file. Why?
Thank you.
Answer to question 1.
If you run python from within a command window (cmd) you will be able to see the output if any. As opposed to double clicking it which will run the program and close it unless the program is supposed to do otherwise. And Idle is mimicking the open command window.
One thing to watch out for is having 2 python interpreters on the system. If you are just starting out I would avoid installing a 2nd one. Removing old python interpreters is as easy as removing the folder they are in. You said it "runs differently" which makes me think this is a concern.
Answer to question 2
You can change what program uses the file. You can have idle read it, or the python interpreter run it. check out http://www.thewindowsclub.com/change-file-associations-windows for info on how to change that behavior.

Python curses Redirection is not supported

I am trying to use Curses in PyDev in Eclipse in Win7.
I have installed Python 3.2 (64bit) and curses-2.2.win-amd64-py3.2. When I input the following testing codes into PyDev:
import curses
myscreen = curses.initscr()
myscreen.border(0)
myscreen.addstr(12, 25, "Python curses in action!")
myscreen.refresh()
myscreen.getch()
curses.endwin()
It did not show any syntax error, so I think the curses was installed correctly.
However, when I ran it as Python Run, the output showed: Redirection is not supported. I do not know where this problem comes from. I googled a lot but can't find related information.
Recent PyCharm versions (I am currently running 2017.2, not sure when this option was added, or if it has been there the entire time) have the option "Emulate terminal in output console". Curses works with this option checked.
You cannot expect to use curses with a non-terminal.
Probably you get this because you are running the script from inside an IDE, like PyCharm or any other.
All IDEs do provide consoles that are not terminals, so that's where the problem comes from.
For a Pycharm user the solution given by codeape works fine :
Snapshot
You can't use any IDE to run python files with the curses package. I used to run in pycharm and naturally couldn't run.
Change to the command line to run:
for testing follow my following steps
on desktop open notepad and copy paste the code and save it as filename.py
open command line change directory to desktop use below command cd Desktop and hit enter type python example.py and hit enter, your program will definitely run
My workaround is to create a Run Configuration that calls a curses script. The little overhead is worth not having to switch to the terminal and manually run the script hundreds of times a session. I use Intellij but I imagine the process should be similar in PyCharm.
The desired result is the convenience of a button to run the script:
First create a script that calls the entry script, for instance:
ptyhon name-of-script.py
Then, to create a configuration for each script:
Go to Edit configuration.
Click the plus button and add a Shell Script.
Enter the path to a shell script.
Here is a picture of a directory with a couple of sample scripts.
I use this process to view my progress. My curses scripts are very modest so fortunately I can live without a debugger.

I'm new to Python and managed to crash IDLE after adding a line of code to my program

I'm working on a small game and, in the interest of full disclosure, I've learned some other languages before but this is only my second day learning Python.
What I was trying to do was simple enough: I was trying to generate a random integer between two integer values (e.g. random(a,b)). I looked around to see if there was an existing function that I could use, and I found information about a function called "randint". So I added a line of code to my program that looked something like:
value = randint(1,15)
I received an error that randint was undefined. So then I looked further and saw someone state that I needed to import the random library using the following line of code:
import random
The moment I refreshed (F5) IDLE crashed. So then I figured "welp, that was wrong" and I went to open IDLE and make the change. I was able to open IDLE, but I can't open the file. In fact, I can't open any of the (three) files that I've made. I opened the file in Notepad++ and removed the offending lines (both randint and import random), but IDLE still crashes whenever I try to open a file. I rebooted my laptop for lack of a better idea, but there was no change in behavior.
Details: Windows 7 x64 w/ all updates, Python 3.2.3.
...help? Also, what did I do? :-/
Received a solution from another forum:
"If you want to try diagnosing the problem, run the Python command line and then enter the line:
from idlelib import idle
That will launch idle, and you should get a traceback in the command prompt if something goes wrong."
When I did this it opened IDLE and produced an error (displayed in terminal) when I tried to open the file. Rather than crashing, I received a prompt (pop-up, not in terminal) to save the file. I found this odd because I had just opened it and hadn't made any changes. I let it save the file and then success! Now I can use IDLE to open any of my files again. Hope this helps someone else who encounters this issue :)
I'm not sure if this is helpful, but based on my experience, IDLE was never reliable enough for me. Practice using the command-line Python instead.
Go to the command prompt by running cmd
cd into your file's directory
type python yourfilename.py

Cleanest way to run/debug python programs in windows

Python for Windows by default comes with IDLE, which is the barest-bones IDE I've ever encountered. For editing files, I'll stick to emacs, thank you very much.
However, I want to run programs in some other shell than the crappy windows command prompt, which can't be widened to more than 80 characters.
IDLE lets me run programs in it if I open the file, then hit F5 (to go Run-> Run Module). I would rather like to just "run" the command, rather than going through the rigmarole of closing the emacs file, loading the IDLE file, etc. A scan of google and the IDLE docs doesn't seem to give much help about using IDLE's shell but not it's IDE.
Any advice from the stack overflow guys? Ideally I'd either like
advice on running programs using IDLE's shell
advice on other ways to run python programs in windows outside of IDLE or "cmd".
Thanks,
/YGA
For an interactive interpreter, nothing beats IPython. It's superb. It's also free and open source. On Windows, you'll want to install the readline library. Instructions for that are on the IPython installation documentation.
Winpdb is my Python debugger of choice. It's free, open source, and cross platform (using wxWidgets for the GUI). I wrote a tutorial on how to use Winpdb to help get people started on using graphical debuggers.
You can easily widen the Windows console by doing the following:
click the icon for the console window in the upper right
select Properties from the menu
click the Layout tab
change the Window Size > Width to 140
This can also be saved universally by changing the Defaults on the menu.
Year 2017-2022 Answer:
Try Visual Studio Code, it has great support for Python debugging, auto completion and more!
See this link for details:
https://code.visualstudio.com/docs/languages/python#_debugging
However, I want to run programs in
some other shell than the crappy
windows command prompt, which can't be
widened to more than 80 characters.
Click on the system box (top-left) in the command prompt and click properties. In the layout tab you can set the width and height of the window and the width and height of the screen buffer. I recommend setting the screen buffer height to 9999 so you can scroll back through a long output.
I use eclipse with pydev. Eclipse can be sluggish, but I've become attached to integrated svn/cvs, block indent/unindent and run as unittest features. (Also has F5 run)
If your comfortable in emacs though I don't see any reason to make such a major change.
I suggest instead that you replace your 'crappy command prompt' with powershell. It's not as crappy.
(As mentioned by Soviut and The Dark - you can increase the buffer width/window size to more than 80 by title-bar>right-click>Properties>Buffer Width/Window Size edit even in crappy cmd)
Wing IDE is awesome. They also have a free version.
If you ever graduate to vim, you can just run the following command to start the program you're currently editing in an interactive shell:
:!python -i my_script.py
I edit my Python programs in EditPlus. I've configured a user tool that allows me to run my program and have its output appear in a frame below my editor window. My configuration will even let you double click on an exception lines in the output to jump directly to the file and line where the error occurred. You can grab the user tool configuration I use from here on the EditPlus wiki.
Perhaps there is similar functionality you can configure that allows you to run your program and display its output in your Emacs editor!?
I replaced cmd with Cygwin and Poderosa. May be a little overkill though, if the only problem you have with cmd is that it's a pain to resize.
Although you use Emacs instead of Vim, so I guess you're into overkill... ;-)

Categories