Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

MacOS users using Terminal to run scripts

I just came across very simple yet very annoying situation when trying to use external code editor (Brackets) and then running the script in Terminal (because MacOS has Python installed by default). Not many people will go THAT rogue in Python basics but in case somebody struggles with password_check.py I'd rather highlight it here.

When I was running script I got NameError which annoyed the hell out of me for a while. The reason is that default installed version of Python on MacOS is 2.7 and it doesn't automatically treat "input" as a string (it expects legacy call raw_input).

Solution is to install Python3 from official repository and use python3 instead of python to run your scripts.

Hope it helps!

1 Answer

This is definitely good advice. The version of Python that ships with macOS is super old, like you said, and all the Treehouse videos are in Python 3, which is much newer. That being said, I've found it much easier to install Python with Homebrew than with the installer from the official website. This way, you just have to brew install python, and whenever you wanna update it, it's as simple as running brew update and then brew upgrade instead of having to download another GUI installer and overwrite your current installation that way. Package managers like Homebrew make software easier to maintain over time. I think you'll like it ?

Thanks Michael, yes indeed homebrew makes life a bit easier in this matter on MacOS.

Additional advice is to make aliases for faster execution between both Python versions. What I did is basically ran: alias p2=python Then another command: alias p3=python3

Which effectively allows to run scripts faster with e.g.:

p2 script_name.py (for Python 2.7) or p3 script_name.py (for Python 3.x)

Instead of writing every time:

python script_name.py or python3 script_name.py