I know that spaces are preferred over tabs in Python, so is there a way to easily convert tabs to spaces in IDLE or does it automatically do that?
From the IDLE documentation:
Tab inserts 1-4 spaces (in the Python Shell window one tab).
You can also use Edit > Untabify Region to convert tabs to spaces (for instance if you copy/pasted some code into the edit window that uses tabs).
Of course, the best solution is to go download a real IDE. There are plenty of free editors that are much better at being an IDE than IDLE is. By this I mean that they're (IMO) more user-friendly, more customizable, and better at supporting all the things you'd want in a full-featured IDE.
Unfortunately IDLE does not have this functionality. I recommend you check out IdleX, which is an improved IDLE with tons of added functionality.
In IDLE 3.6.5, it's Format > Untabify Region
Related
I've been working with python for a while, and understand how to use tabs correctly, but recently in a project I have started working across Jupyter notebooks and Sublime. When I copy and paste code over, the indentation gets completely messed up. Usually it goes from tabs to 4 spaces and it doesn't like that so I have to manually change every line. It happens on occasion when I copy from one python file to another in just Sublime as well.
Is there some sort of general way, or just setting within those applications, that can prevent this?
Jupyter Notebooks indent with 4 spaces by default. Not sure where you'd be getting tabs unless you originally copy-pasted tabs.
If you want to keep the tabs in SublimeText, select a Python file, go to "Preferences" --> "Settings - Syntax Specific" and add the line:
"translate_tabs_to_spaces": false
Although, if your sources were already mixed (some cells in jupyter use tabs, some use spaces) then it's better to standardise on paste in sublime, and use:
"translate_tabs_to_spaces": true
Also, when you're pasting in SublimeText, instead of just Paste with Ctrl+V, use Paste & Indent: Ctrl+Shift+V
Btw, Python3 recommends 4 spaces by default, almost as a standard. So maybe set that in your editor. Most of them have settings for "Tabs & Indents" somewhere with code formatting.
I am coding Python3 in Vim and would like to enable autocompletion.
I must use different computers without internet access. Every computer is running Linux with Vim preinstalled.
I dont want to have something to install, I just want the simplest way to enable python3 completion (even if it is not the best completion), just something easy to enable from scratch on a new Linux computer.
Many thanks
Unfortunately Vim, by default doesn't do what you want, you are pretty much limited to the Ctrl-P style, which is better than it seems once you get used to it.
However i also often find myself working on machines that are not allowed to access the internet or have other files placed on them and When i find myself in this situation and i am using an unfamiliar language i sometimes use Vim's dictionary completion: http://vim.wikia.com/wiki/VimTip91
To populate this dictionary i cat / trim / filter the man pages for the language to get a variety of keywords. I ram these into a filetype specific dictionary: au FileType * execute 'setlocal dict+=~/.vim/words/'.&filetype.'.txt'
Obviously this isn't the greatest solution and it is a bit heavy handed but it does provide a certain degree of "What is that function called" type stuff.
If you have compiled vim with +python3, you can try omnifunc.
Add following to your ~/.vimrc:
au FileType python setl ofu=python3complete#Complete
Then in insert mode, just type CtrlX + CtrlO.
See :help omnifunc.
by default, Ctrl-P in insert mode do an autocompletion with all the words already present in the file you're editing
So I wrote a program in python using NotePad++ in Windows, but then when I opened the file in Mac computer using TextWrangler or any text editor in it and after compiling it, there was an error message regarding indentation. How can I easily fix this?
Here is something to do with your file via Notepad++:
Edit -> Blank Operations -> TAB to Space
If this won't help (and most likely it won't) you will need to check indents manually. I can suggest View -> Show Symbol -> Show Indent Guide for convenience.
It is a good and safe style to use only spaces. Not to face this problem in your future projects configure Notepad++: Settings -> Tab Settings -> Replace by space. You will still be able to use tabs, but they will be changed to defined number of spaces (4 for me). Hope this helps.
In Python, is there a mean to enforce the use of spaces or tabs indentation with a per file basis ?
Well, perhaps "enforce" is too strong, more like a "recommendation".
I keep receiving patch files with mixed indentation and this is annoying... (to say the least) Python itself can tell when there is a problem, but I am searching something to do that at the editor level, like it exists for the charset.
Edit : Ok, my question wasn't clear, I am asking this because I keep receiving corrections and patches in any mix of tab/space you can imagine. I am using Mercurial as a DVCS, perhaps something exists at this level ?
Tim Peters has written a nifty script called reindent.py which converts .py files to use 4-space indents and no tabs. It is available here, but check your distribution first -- it may have come bundled in an Examples or Tools directory. (On the latest LTS Ubuntu, it is provided by the python2.7-examples package.)
If you can set up a Mercurial hook you should be able to run all files through reindent.py.
By the way, if you are using unix, then your system may also have the expand (and unexpand) command, which convert all tabs to spaces (and spaces to tabs). However, in this case, I think reindent.py is the right tool.
Look at the tabnanny module: — Detection of ambiguous indentation.
This is something your editor should do for you. Most editors (try Notepad++ for example, it's free) will let you set whether hitting tab enters a tab character or a number of spaces. I'd recommend using two spaces instead of tab in all files (I find 4 is too much). Using spaces instead of tabs is better as it means that you can indent stuff using both the space & tab keys without worrying about messing up your files.
If you have files that have a mix it isn't hard to write your own script to convert tabs to spaces
As explicited in PEP 8, never mix tabs and space. However, a file with both may just run...
As it says there:
The most popular way of indenting Python is with spaces only. The
second-most popular way is with tabs only. Code indented with a mixture
of tabs and spaces should be converted to using spaces exclusively.
When invoking the Python command line interpreter with the -t option, it issues
warnings about code that illegally mixes tabs and spaces. When using -tt
these warnings become errors. These options are highly recommended!
the solution is therefore to use as a default:
python -t my_mixed_code.py
To answer at the editor level, this depends on the editor, please specify!
Does anyone know of an already-written Python script, tool, or editor that will check for unbalanced multi-line tokens? (parentheses, quotes, {}, [], etc.)
I've been writing Python code in IDLE, and every so often I'll get "EOF token in multi-line statement" and start swearing, because it means that somewhere in about 200 lines of code I forgot a closing parenthesis or quote and IDLE can't tell me where. This seems like a fairly straightforward thing, I just don't really have the time or headspace to work it out myself right now.
Much thanks
I use Eclipse with PyDev. It's very good for this sort of thing, and lots more.
emacs will automatically highlight matching pairs of parentheses/brackets/quotes/etc. as you type them, and it will inform you immediately if you mismatch them (e.g. if you type a [ followed by a )). I'm sure vim also does this, but since I don't use vim, I can't say with certainty.
PyDev is the best IDE to develop in Python. Has this feature and a lot of more.
If you use the vim text editor, there is a syntax highlighter for Python that might be of some help. Vim's python indenting rules also line up a new line with an unmatched open parenthesis from the previous line. That's been my visual cue that something is unbalanced.