Python interactive (Terminal) doesn't recognize tabs as whitespace

I have a MacOS devbox using Terminal running Python 3.10 that doesn't recognize the tab character as indentation. What is wrong? Is my Python misconfigured? Is this an Apple or zsh thing?


I also have a Raspberry Pi running Python 3.7.3 that behaves normally (tabs are recognized). What is the difference between them?


Strangely I searched extensively but found no mention of this problem.

MacBook Pro 14″

Posted on Nov 24, 2023 11:11 PM

Reply
12 replies

Nov 25, 2023 1:58 PM in response to Gnarlodious

Gnarlodious wrote:

I guess I don't understand. I am using Xcode and in it tab characters are literally tabs, not spaces. It doesn't translate tabs into spaces.

Go to Xcode > Settings > Text Editing > Indentation and make sure "Prefer Indent Using" is set to "Space". Never use tabs. At best that will just annoy people when the tab setting-du-jour scrambles your code when some indents are 4 spaces and some are 4, 8-space tabs. It's an even bigger problem in Python where whitespace is significant.

I don't have any idle3 app, and there is no such app on the App Store. In the past I always used Terminal, and it recognized tabs as indentation. If I update to Python 3.12 do you think it will fix it? I would hate to have to change all my scripting to spaces indentation.

I eventually found python 3.12.0 installed at:
/Library/Frameworks/Python.framework/Versions/3.12/bin/python3

That's Apple's Python. It isn't meant for you to use. You should install your own Python. That way, if you want some cool new module, you can just install it. You don't want to have to constantly fight with Apple.

Nov 25, 2023 5:49 AM in response to Gnarlodious

Python expects 4 space indents per PEP-8 Style Guide for Python Code. If you are using SSH into macOS from Raspberry PI, your tab size may not be set correctly in macOS.


Launch the Python idle3 application in the background. It will perform automatic indents where it needs to (e.g. inside a function or code block). Control+d will quit the idle3 app. I don't use this as a substitute for a proper syntax aware programmer's editor, but as a debugging tool for code that I incorporate into that editor.


idle3 2> /dev/null &


I never use Python interactively in the Terminal since idle3 fulfills that role, but the interactive Python 3.12 does understand tabs in the Sonoma 14.1.1 Terminal application. You enter it after the ellipsis (…) as shown below and an extra return to end the function definition:




Nov 25, 2023 5:42 AM in response to Gnarlodious

Sounds like a Python problem. You can't change your indentation style in Python. If you indent one line 4 spaces, you have to keep indenting 4 spaces. You can't switch to tabs. It will just get confused. Same in reverse.


I recommend looking at your source file in BBEdit with invisibles turned on. You should be able to see where the whitespace style is changing.


Note that I despise Python. All of the above may or may not be correct.

Nov 25, 2023 11:21 AM in response to Gnarlodious

Sublime Text is an open-ended trial that periodically will ask you to purchase it, but otherwise you can continue to use it. I finally paid for it after using it for years. If you evaluate it, you will want to install the complementary package manager too.


https://packagecontrol.io/


It takes TextMate theme bundles too.


One of the obscure tools in the Python.org 3.11 or later distribution (it may be in earlier versions) is the script reindent.py that standardizes tabs and space indents in code that may be messed up. It has well documented.


cp share/doc/python3.11/examples/Tools/scripts/reindent.py ~/bin

./reindent.py -h


Nov 25, 2023 6:59 AM in response to VikingOSX

I guess I don't understand. I am using Xcode and in it tab characters are literally tabs, not spaces. It doesn't translate tabs into spaces.


I don't have any idle3 app, and there is no such app on the App Store. In the past I always used Terminal, and it recognized tabs as indentation. If I update to Python 3.12 do you think it will fix it? I would hate to have to change all my scripting to spaces indentation.



Nov 25, 2023 2:10 PM in response to VikingOSX

VikingOSX wrote:

And here I was led to believe you hated AppleScript. Now, its become a hierarchy, probably with homebrew at the top… 😉

Always has been a hierarchy. I hate AppleScript, but I merely despise Python. There's a difference.


Homebrew is different. I neither hate it nor despise it. I don't use it at all. It just annoys me how it has become this defacto installation that everyone pushes on the internet. I don't get it. Supposedly all those internet people are updating constantly to protect themselves from the Communist Menace. Their homebrew doesn't break with every update like it does for people in this forum?

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Python interactive (Terminal) doesn't recognize tabs as whitespace

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.