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

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

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.

python 3.6.2 not giving me option to create or run files or anything of the sort

I know some basics of Java and C++, and am looking to learn Python
I am trying to develop some random stuffs to get a good feel of how it works, but i can only make 1 line scripts that run every time i press enter to go to the next line.
I've seen tutorial videos where they can just open up files from a menu and type away until they eventually run the program.
I'm using IDLE, and i don't see options to open up new stuffs; I can only make one or two line programs. When i tried to make a calculator program, i didnt know how to run it because it ran every line of code i typed in unless there were ...'s under the >>>'s.
I think it's because i am in interactive mode, whatever that is.
How do i turn it off, if that's the problem?
There are many different options for writing python scripts. The simplest to use is Idle, it come with the Python download. Within Idle, create a new document to write a script. Once finished, save it as a .py file, and you can run it within Idle. For my personal setup, I use the text editor Atom. I can create documents easily, and run them through the terminal on my computer. Hope this helps.
To exit out of the "interactive mode" that you mentioned (the included REPL shell in IDLE) and write a script, you will have to create a new file by either selecting the option from the top navigation bar or pressing Control-N. As for running the file, there's also that option on the navigation bar; alternatively, you can press F5 to run the program.

How to save the output of an IPython console to a file in Spyder?

I have a bug in my program :(
The problem is that:
My .py code is long, and takes ages to run
I don't know where the bug is
The good news is that I have a lot of print() in my py file, so I can potentially know where the bug lives.
The bad news is that my bug makes my computer crash, so there is no way for me to look at the output of the ipython console and see what went wrong.
How can I have the output be written to disk while the program runs? So that I can still open the file after reboot to understand what happened before the crash?
This question is different from Redirect stdout to a file in Python?, because I need
continuous writing to file
something to use from within Spyder
Many thanks!
Very interesting question! Fortunately IPython has the right magic for you. It's called %logstart (please follow the link for the full documentation).
To start using it and save the input and output of all your commands, just type in an IPython console
In[1]: %logstart -o
and that will record your session from that moment on into a file called ipython_log.py placed in your current directory.
%logstart is very flexible, so you can select a different file to save to, and also how you save your session (either pure Python or as IPython commands).

Python 3.3.3 IDLE closes on save and new file creation

I've had Python 3.3.3 installed on my computer for nearly a year now and used it very frequently. This morning I have been having trouble with my USB stick with the PY file I was working on (USB stick was unplugged without ejecting it and now it needs formatting followed by some other errors).
So I am forced to use a PY file from a few days ago that I made as a backup. The problem is that I can no longer right click my PY file and "edit with IDLE".
I opened IDLE up separately and that worked.
I tried opening my PY file from IDLE, this opened my file for a brief second then closed.
The opening then closing happens when I try to save a new file in IDLE and when I try to create a new file.
This is rather odd as I have not edited any of the inner workings of my Python for a number of weeks, I've just been editing and running programs. This leads me to believe that it all stems back to my USB problem.
I hope someone can suggest some ideas for me.
Fixed
Method used to fix it:
Go for dinner.
Come back.
Fixed.
Seriously though, i restarted my computer, still the same. I left the computer while i showered and had dinner and when i came back and tried again i was able to open IDLE without it crashing. Part 1 Fixed!
The second fix was to change the default program that IDLE opens in, somehow it had changed to PythonIcon or something that i have never seen before. I changed it back to Python Console and the "edit with Idle" returned.
Strange how things change on their own but all is good now, happy coding.
Ben

Issue with Python Batch file to run Python through Notepad++

EDIT: The code I wrote in my Python file was just this:
print "foo"
I'm using Windows XP Home Premium on this tiny little HP Mini 1000, and I want to run Python files, since we're learning it in school. I am aware of this topic, so I tried to run Python files using a batch file (python.bat), and I'm getting an error that says, "Can't find 'main' module in ''" whenever I run the batch file. I followed the instructions given here. All I did was change "Python26" to "Python33" because of the difference in versions.
Any idea what's wrong here? I really want to run Python files from Notepad++, so I don't want any alternative ways to run them.
This sounds like you don't have PYTHONPATH set up correctly. I suggest you review the documentation here:
http://docs.python.org/2/using/windows.html
Instead of calling Python, call cmd.exe and then use the set command to inspect which variables are set and how they are set. Run the exit command to leave the command shell. When you think you have the variables set up correctly, try again to run Python.
Good luck and have fun!
I use the command line interpreter or IDLE mostly (Win 8.1 now, but I've done so since Win XP SP2), but NPP is my main text editor, so I was curious about this issue.
When I was reproducing this, I was able to generate several errors, but the only one I got that was an exact match was when I failed to configure the Run option correctly.
You need to make sure to follow this step exactly in the instructions you were following. When you navigate to Run -> Run in Notepad++, you have to enter this exactly:
C:\Python33\python.bat "$(FULL_CURRENT_PATH)"
I am pretty sure you left out the "$(FULL_CURRENT_PATH)", or otherwise didn't add it correctly, as failing to do so causes exactly the same error on my end. Failing to include this means that when you run the batch script, you get the wrong input to the Python interpreter, causing the error.

Categories