- How to turn on line numbers in IDLE?
- Python Solutions
- Solution 1 — Python
- Version 3.8 or newer:
- Version 3.7 or older:
- Solution 2 — Python
- Solution 3 — Python
- Solution 4 — Python
- Solution 5 — Python
- Solution 6 — Python
- Solution 7 — Python
- How to show line numbers in python idle
- How to turn on line numbers in IDLE?
- Version 3.8 or newer:
- Version 3.7 or older:
- How to get line numbers in my python 2.7 script mode? [duplicate]
- Filename and line number of Python script
- How to display line numbers in tkinter.Text widget?
How to turn on line numbers in IDLE?
In the main shell of IDLE, errors always return a line number but the development environment doesn’t even have line numbers. Is there anyway to turn on line numbers?
Python Solutions
Solution 1 — Python
Version 3.8 or newer:
To show line numbers in the current window, go to Options and click Show Line Numbers .
To show them automatically, go to Options > Configure IDLE > General and check the Show line numbers in new windows box.
Version 3.7 or older:
Unfortunately there is not an option to display line numbers in IDLE although there is an enhancement request open for this.
However, there are a couple of ways to work around this:
- Under the edit menu there is a go to line option (there is a default shortcut of Alt+G for this).
- There is a display at the bottom right which tells you your current line number / position on the line:
Solution 2 — Python
There’s a set of useful extensions to IDLE called IDLEX that works with MacOS and Windows http://idlex.sourceforge.net/
It includes line numbering and I find it quite handy & free.
Otherwise there are a bunch of other IDEs some of which are free: https://wiki.python.org/moin/IntegratedDevelopmentEnvironments
Solution 3 — Python
If you are trying to track down which line caused an error, if you right-click in the Python shell where the line error is displayed it will come up with a «Go to file/line» which takes you directly to the line in question.
Solution 4 — Python
As it was mentioned by Davos you can use the IDLEX
It happens that I’m using Linux version and from all extensions I needed only LineNumbers. So I’ve downloaded IDLEX archive, took LineNumbers.py from it, copied it to Python’s lib folder ( in my case its /usr/lib/python3.5/idlelib ) and added following lines to configuration file in my home folder which is ~/.idlerc/config-extensions.cfg:
[LineNumbers] enable = 1 enable_shell = 0 visible = True [LineNumbers_cfgBindings] linenumbers-show =
Solution 5 — Python
Line numbers were added to the IDLE editor two days ago and will appear in the upcoming 3.8.0a3 and later 3.7.5. For new windows, they are off by default, but this can be reversed on the Setting dialog, General tab, Editor section. For existing windows, there is a new Show (Hide) Line Numbers entry on the Options menu. There is currently no hotkey. One can select a line or bloc of lines by clicking on a line or clicking and dragging.
Some people may have missed Edit / Go to Line. The right-click context menu Goto File/Line works on grep (Find in Files) output as well as on trackbacks.
Solution 6 — Python
As @StahlRat already answered. I would like to add another method for it. There is extension pack for Python Default idle editor Python Extensions Package.
Solution 7 — Python
As mentioned above (a quick way to do this) :
Then I create a shortcut on Desktop (Win10) like this:
C:\Python\Python37\pythonw.exe "C:\Python\Python37\Scripts\idlex.pyw"
The paths may be different and need to be changed:
(Thanks for the great answers above)
How to show line numbers in python idle
There is a display at the bottom right which tells you your current line number / position on the line: Solution 2: There’s a set of useful extensions to IDLE called IDLEX that works with MacOS and Windows http://idlex.sourceforge.net/ It includes line numbering and I find it quite handy & free. Otherwise there are a bunch of other IDEs some of which are free: https://wiki.python.org/moin/IntegratedDevelopmentEnvironments Solution 3: If you are trying to track down which line caused an error, if you right-click in the Python shell where the line error is displayed it will come up with a «Go to file/line» which takes you directly to the line in question.
How to turn on line numbers in IDLE?
Version 3.8 or newer:
To show line numbers in the current window, go to Options and click Show Line Numbers .
To show them automatically, go to Options > Configure IDLE > General and check the Show line numbers in new windows box.
Version 3.7 or older:
Unfortunately there is not an option to display line numbers in IDLE although there is an enhancement request open for this.
However, there are a couple of ways to work around this:
- Under the edit menu there is a go to line option (there is a default shortcut of Alt+G for this).
- There is a display at the bottom right which tells you your current line number / position on the line:
There’s a set of useful extensions to IDLE called IDLEX that works with MacOS and Windows http://idlex.sourceforge.net/
It includes line numbering and I find it quite handy & free.
Otherwise there are a bunch of other IDEs some of which are free: https://wiki.python.org/moin/IntegratedDevelopmentEnvironments
If you are trying to track down which line caused an error, if you right-click in the Python shell where the line error is displayed it will come up with a «Go to file/line» which takes you directly to the line in question.
Any limit to how many characters Python’s IDE can take, 0. To answer your question, no, there’s no limit on the length of the dictionary or list (or any other object). I’ve stored all the words of an entire book in a list. They are just streams of input. As far as how your code is written, it’s not Pythonic to write beyond 80 characters/columns per line, because, ultimately, …
How to get line numbers in my python 2.7 script mode? [duplicate]
it seems like you’re using the IDLE editor which comes with Python.
From taking a quick look at it, I didn’t see how to display line numbers. But what you can do is to look at the bottom line of the window which shows the current line of the cursor.
Another option would be to use another editor to write your python programs. There’s a wide variety of choices. You can google for «python editor» and find good comparisons. Popular editors include Atom , Visual Studio Code , PyCharm , .
Python — How to determine file, function and line, 0. To get the line number in Python without importing the whole sys module First import the _getframe submodule: from sys import _getframe. Then call the _getframe function and use its’ f_lineno property whenever you want to know the line number: print (_getframe ().f_lineno) # prints the line number.
Filename and line number of Python script
Thanks to mcandre, the answer is:
#python3 from inspect import currentframe, getframeinfo frameinfo = getframeinfo(currentframe()) print(frameinfo.filename, frameinfo.lineno)
Whether you use currentframe().f_back depends on whether you are using a function or not.
from inspect import currentframe, getframeinfo cf = currentframe() filename = getframeinfo(cf).filename print "This is line 5, python says line ", cf.f_lineno print "The filename is ", filename
Calling a function that does it for you:
from inspect import currentframe def get_linenumber(): cf = currentframe() return cf.f_back.f_lineno print "This is line 7, python says line ", get_linenumber()
Handy if used in a common file — prints file name, line number and function of the caller:
import inspect def getLineInfo(): print(inspect.stack()[1][1],":",inspect.stack()[1][2],":", inspect.stack()[1][3])
Printing out each line (from file) with line number python, open_file = open (‘something.txt’, ‘r’) lines = open_file.readlines () for line in lines: line.strip () print (line) open_file.close () I am aware that I could print out the index that each word is at however, unless I am mistaken, that would start the row number from 0 not 1. python list file loops Share Improve this question
How to display line numbers in tkinter.Text widget?
The root problem is that you’re calling dlineinfo before returning to the runloop, so the text hasn’t been laid out yet.
This method only works if the text widget is updated. To make sure this is the case, you can call the update_idletasks method first.
As usual, to get more information, you have to turn to the Tcl docs for the underlying object, which basically tell you that the Text widget may not be correct about which characters are and are not visible until it’s updated, in which case it may be returning None not because of any problem, but just because, as far as it’s concerned, you’re asking for the bbox of something that’s off-screen.
A good way to test whether this is the problem is to call self.__text.see(i) before calling dlineinfo(i) . If it changes the result of dlineinfo , this was the problem. (Or, if not that, at least something related to that—for whatever reason, Tk thinks everything after line 1 is off-screen.)
But in this case, even calling update_idletasks doesn’t work, because it’s not just updating the line info that needs to happen, but laying out the text in the first place. What you need to do is explicitly defer this call. For example, add this line to the bottom of load_from_file and now it works:
self.__text.after(0, self.__update_line_numbers)
You could also call self.__text.update() before calling self.__update_line_numbers() inline, and I think that should work.
As a side note, it would really help you to either run this under the debugger, or add a print(i, dline) at the top of the loop, so you can see what you’re getting, instead of just guessing.
Also wouldn’t it be easier to just increment a linenumber and use ‘<>.0′.format(linenumber) instead of creating complex indexes like @0,0+1line+1line+1line that (at least for me) don’t work. You can call Text.index() to convert any index to canonical format, but why make it so difficult? You know that what you want is 1.0 , 2.0 , 3.0 , etc., right?
The root cause of the problem is that the text hasn’t been drawn on the screen yet, so the call to dlineinfo will not return anything useful.
If you add a call to self.update() before drawing the line numbers, your code will work a little better. It won’t work perfectly, because you have other bugs. Even better, call the function when the GUI goes idle, or on a Visibility event or something like that. A good rule of thumb is to never call update unless you understand why you should never call update() . In this case, however, it’s relatively harmless.
Another problem is that you keep appending to i, but always use i[0] when writing to the canvas. When you get to line 2, i will be «1.0+1line». For line three it will be «1.0+1line+1line», and so on. The first character will always be «1».
What you should be doing is asking tkinter to convert your modified i to a canonical index, and using that for the line number. For example:
i = self.__text.index('+1line'.format(i))
This will convert «1.0+1line» to «2.0», and «2.0+1line» to «3.0» and so on.
Break a long line into multiple lines in Python, Method 1: Break a long line into multiple lines using backslash. A backslash (\) can be put between the line to make it appear separate as shown below. Also, notice that all three cases produce exactly the same output only difference is in the way they are presented in the code: