Python program written in Notepad++, error in TextWrangler - python

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.

Related

How to manage 'inconsistent use of tabs and spaces in indentation' across editors

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.

Setting different code styles for separate projects in Pycharm

I'm using Pycharm, and it's a great editor, but I have this issue while working on a number of projects at the same time. Unfortunately, the different projects I'm working on have different code styles.
The easiest example is that one uses spaces, while another uses tabs.
While I don't want to go into the whole Tabs vs Spaces debate, I'm wondering if there is a way to set Pycharm to use separate coding style rules per projects.
Thanks
Pycharm's Code Style configuration has built-in scheme setting just for your requirement.
Preference -> Editor -> Code Style -> Python
After setting the style, you can store it with the project, by clicking the Scheme pull-down menu list. Also you can store/export the style by clicking the gear icon next to it.
After reading the halfelf's answer, I found this option:
File -> Settings -> Editor -> Code Style -> Python
In the scheme options, there is a drop down list where you can choose project and set the choices to the relevant project only (the one that is currently chosen.
Hope this helps..

Can I configure IDLE to automatically convert tabs to spaces?

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

Enforce "spaces" or "tabs" only in python files?

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!

Nice copying from Python Interpreter

When I am working with a Python Interpreter, I always find it a pain to try and copy code from it because it inserts all of these >>> and ...
Is there a Python interpreter that will let me copy code, without having to deal with this? Or alternatively, is there a way to clean the output.
Additionally, sometimes I would like to paste code in, but the code is indented. Is there any console that can automatically indent it instead of throwing an error?
Related
Why can I not paste the output of Pythons REPL without manual-editing?
IPython lets you show, save and edit your command history, for example to show the first three commands of your session without line numbers you'd type %hist -n 1 4.
WingIDE from Wingware will let you evaluate any chunk of code in a separate interpreter window.
IPython will let you paste Python code with leading indents without giving you an IndentationError. You can also change your prompts to remove >>> and ... if you wish.
I have a vim macro to "paste while cleaning interpreter prompts and sample output [[==stuff NOT preceded by prompts" and I'll be happy to share it if vim is what you're using. Any editor or IDE worth that name will of course be similarly easy to program for such purposes!
Decent text editors such as Notepad++ can make global search and replace operations that can replace >>> with nothing.

Categories

Resources