Mac How do you run Python code on Mac? (I am a noob learning programming, please help)

Reach9

macrumors 68020
Original poster
Aug 17, 2010
2,417
224
In America
Hey all please bear with me..

So my goal for 2015 is learning how to code. I want to learn Front-end Web Development first, and then later on i plan to move onto Ruby and then Objective-C/Swift

I am a total noob at programming. I'm starting with the MIT Computer Science course on Python on iTunes U, because I heard that it's the best language to learn first for beginners. (should i do HTML and CSS instead?)

I downloaded TextWrangler and Sublime Text 2. I was wondering how does one run Python code? I understand Yosemite already has a version of Python, but how do I actually execute on the code i write? Do i type it out on Sublime Text 2 and hit run?

Any clarification and tips would be great, thanks!
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
8,750
4,440
So my goal for 2015 is learning how to code.
Congratulations / yay!


I want to learn Front-end Web Development first, and then later on i plan to move onto Ruby and then Objective-C/Swift.
I feel like you're getting a bit ahead of yourself. Front end web development is generally done in javascript. I hardly have any experience with Ruby (just enough to realize it's the polar opposite of Python and that I hate it), but my understanding is that it's used mostly for web backend work.

Obj-C/Swift are for working on native apps for OS X and iOS and nothing else.

I am a total noob at programming. I'm starting with the MIT Computer Science course on Python on iTunes U, because I heard that it's the best language to learn first for beginners. (should i do HTML and CSS instead?)
I agree that Python is a great language to learn for beginners. HTML and CSS aren't programming languages. HTML is for dictating what the content on a website will be. CSS is for saying how it will look. JavaScript is for saying how it will behave (the programming portion of front end web development).

I downloaded TextWrangler and Sublime Text 2.
That's redundant. You only need one. I recommend Sublime Text 2, but admittedly I don't think I've ever used TextWrangler.


I was wondering how does one run Python code? I understand Yosemite already has a version of Python, but how do I actually execute on the code i write? Do i type it out on Sublime Text 2 and hit run?
Yep, Yosemite has a version of Python. You might be able to run it straight from Sublime but I don't think you can. You write your code in Sublime Text 2, then you save the file somewhere on your computer, then you open up the program Terminal (search for it via Spotlight.)

I'm going to take a guess you're not familiar with a command line right now, which is okay because there's not a lot you need to know for running your code.

You use a command line by typing in a command and hitting enter. The command line then runs your command and displays the results.

Here are some commands:

Code:
pwd
This prints out the path where the command line is currently looking. It normally starts in your user folder. It stands for Present Working Directory.

Code:
ls
(That's lowercase L and S). It'll list all the files and folders in the current path that the command line is currently looking at. I think it's short for list but I'm not really sure.

Code:
cd "[i]name of a folder in the path the command line is currently looking[/i]"
This will change where the command line is currently looking to be looking inside that folder. It stands for Change Directory. If the name of the folder has spaces in it, you need to put quotes around it like I have above. If there's no spaces in the name, you can leave off the quotes. After you change your directory, you'll get different results for ls and pwd. If you need to go to the parent folder instead, you type:

Code:
cd ..
Code:
python "[i]name of the file in the path the command line is currently looking that you want to run[/i]"
That's how you actually run your python file. Don't forget to include the extension (normally .py). You need the quotes if the name has spaces in it. You can leave off the quotes if the name doesn't have spaces in it.

There's a good deal more than this that you can do with the command line, but I think I've told you enough for you to get your Python code running now.
 
  • Like
Reactions: bumblebritches5

Reach9

macrumors 68020
Original poster
Aug 17, 2010
2,417
224
In America
Congratulations / yay!
Thanks! :)

I feel like you're getting a bit ahead of yourself. Front end web development is generally done in javascript. I hardly have any experience with Ruby (just enough to realize it's the polar opposite of Python and that I hate it), but my understanding is that it's used mostly for web backend work.

Obj-C/Swift are for working on native apps for OS X and iOS and nothing else.
You're right, I was getting a little excited. I want to learn Front end web development and then Back end web development afterwards. In the future i want to learn iOS app development, but now I'm gonna focus on the now. ;)

Why do you hate Ruby, and can I use Python for back end web development?

Do you have any suggestions for what my progression should be? Should i jump into Codecademy learning HTML and CSS after I learn Python?

I agree that Python is a great language to learn for beginners. HTML and CSS aren't programming languages. HTML is for dictating what the content on a website will be. CSS is for saying how it will look. JavaScript is for saying how it will behave (the programming portion of front end web development).
That makes a lot of sense now, thanks!

That's redundant. You only need one. I recommend Sublime Text 2, but admittedly I don't think I've ever used TextWrangler.
Great, can i use Sublime Text 2 for HTML, CSS, JavaScript, jQuery and AngularJS too? or do I need Dreamweaver?


Yep, Yosemite has a version of Python. You might be able to run it straight from Sublime but I don't think you can. You write your code in Sublime Text 2, then you save the file somewhere on your computer, then you open up the program Terminal (search for it via Spotlight.)

I'm going to take a guess you're not familiar with a command line right now, which is okay because there's not a lot you need to know for running your code.

You use a command line by typing in a command and hitting enter. The command line then runs your command and displays the results.

Here are some commands:

Code:
pwd
This prints out the path where the command line is currently looking. It normally starts in your user folder. It stands for Present Working Directory.

Code:
ls
(That's lowercase L and S). It'll list all the files and folders in the current path that the command line is currently looking at. I think it's short for list but I'm not really sure.

Code:
cd "[i]name of a folder in the path the command line is currently looking[/i]"
This will change where the command line is currently looking to be looking inside that folder. It stands for Change Directory. If the name of the folder has spaces in it, you need to put quotes around it like I have above. If there's no spaces in the name, you can leave off the quotes. After you change your directory, you'll get different results for ls and pwd. If you need to go to the parent folder instead, you type:

Code:
cd ..
Code:
python "[i]name of the file in the path the command line is currently looking that you want to run[/i]"
That's how you actually run your python file. Don't forget to include the extension (normally .py). You need the quotes if the name has spaces in it. You can leave off the quotes if the name doesn't have spaces in it.

There's a good deal more than this that you can do with the command line, but I think I've told you enough for you to get your Python code running now.
I'm bookmarking this, i had no idea how to use command line but this is exactly what I was looking for. I can't wait to get started now!

Thank you so much!
 

chown33

Moderator
Staff member
Aug 9, 2009
8,815
5,218
vertical
It turns out you can also run Python from within Sublime Text itself. I was pretty sure this was possible, but I didn't know exactly how. Here's what I did...

First, I googled sublime text run python.

One of the top results is this:
http://stackoverflow.com/questions/8551735/how-do-i-run-python-code-from-sublime-text-2

One of the answers to that question is:
On Mac OS X, save your file with a .py extension. Press ⌘+B. It runs in a window below.

Other search terms worth trying:
sublime text run python on mac
 

D.T.

macrumors G4
Sep 15, 2011
10,713
10,839
Vilano Beach, FL
You're right, I was getting a little excited. I want to learn Front end web development and then Back end web development afterwards. In the future i want to learn iOS app development, but now I'm gonna focus on the now. ;)

Why do you hate Ruby, and can I use Python for back end web development?

Do you have any suggestions for what my progression should be? Should i jump into Codecademy learning HTML and CSS after I learn Python?
Ruby and Python (I use both, I equally love and hate both...) are general purpose languages. They have things like control structures (if/then, case,), ways to define classes, etc. When you talk about using them in the context of backend web development, that means using some of kind of DSL for the web, a framework.

I find Ruby/Rails has a beautiful simplicity, but Python is more explicit, less magic, more nut-and-bolts configuration that’s clear, less convention vs. Ruby (that I sometimes really don’t agree with), but some language design decisions I think are dumbfounding. Again, from starting out, it’s a “6 of one...” consideration, when you get to making a choice, you can’t go wring with either, and one might agree with you more.

FYI, I also use C#, that’s just a language, but combine it with the ASP.NET framework and it can service up dynamic web pages via a web server like IIS (on Windows). PHP (which you’ve probably heard of), is like a language and web framework kind of rolled up into a single solution.

Same with Ruby, the web framework that popularized the language so much is Rails. It provide a way to serve up content, access backend data, create dynamic templates and implement an MVC pattern (for the web). The popular web framework for Python is Django, but there are dozens of options.

I just started a new project a few moths ago and I’m using Node.js, which is an asynchronous, non-blocking server platform that uses Javascript (yes, on the server side). Again, it has some general use (though clearly attenuated toward networking apps), but benefits greatly from using a framework layer to serve up content, handle routing, templates, etc. (FWIW, I’m using Express and Locomotive on the [Node] server side, Angular on the client [with a number of other Javascript libs]).

Like AoW said, HTML is markup for a client browser, CSS is the styling “language” for HTML. Javascript on the client side can be used to provide nice user experience (UX) like popup dialogs, dynamic page control (like only updating part of the page).

Where it’s probably a little confusing for a newbie, is the ambiguous line between server and client. A language with a web framework can serve up a page to a browser, and that page might have content that’s generated from a dynamic source (like a database), but it gets rendered into HTML/CSS and sent to the browser (that’s the only thing a browser can consume).

I would say get a little familiar with HTML/CSS, then maybe learn a language/framework and see how it works to generate that HTML with dynamic server code (some will still be simple static markup, some will use templates to generate markup/content, often times returned from a database).

Good luck.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
8,750
4,440
It turns out you can also run Python from within Sublime Text itself. I was pretty sure this was possible, but I didn't know exactly how. Here's what I did...

First, I googled sublime text run python.

One of the top results is this:
http://stackoverflow.com/questions/8551735/how-do-i-run-python-code-from-sublime-text-2

One of the answers to that question is:
On Mac OS X, save your file with a .py extension. Press ⌘+B. It runs in a window below.

Other search terms worth trying:
sublime text run python on mac
I put

Code:
print("Hello world")
In a .py file in Sublime and hit that keyboard shortcut. It spat out a cryptic error message. It seems like I've tried this multiple times before and I've had a variety of issues that I always need to fix first. I've never had problems just running it from a command line. So I just run it from a command line.

I also feel like learning about and using the command line is useful. It takes away some of the mystery of how the computer works. Without it I've noticed beginners tend to think that the file they've written code into is something more than a text file (I thought that myself. I was surprised the first time I saw someone open a C file in TextEdit. I thought there was something special about a C file where it wasn't just text.)

For those reasons, I provided directions on using the command line rather than just telling them how to run it directly from Sublime.
 

Red Menace

macrumors 6502
May 29, 2011
440
78
Fruita, Colorado, USA
TextWrangler (and BBEdit) will run Ruby and Python scripts directly from the editor (or in the Terminal), using the languages installed with the system. Just select the language from the drop down menu at the bottom of the editor window or from the Edit > Text Options... menu item, and then use one of the Run items in the shebang (#!) menu.
 

Reach9

macrumors 68020
Original poster
Aug 17, 2010
2,417
224
In America
Ruby and Python (I use both, I equally love and hate both...) are general purpose languages. They have things like control structures (if/then, case,), ways to define classes, etc. When you talk about using them in the context of backend web development, that means using some of kind of DSL for the web, a framework.

I find Ruby/Rails has a beautiful simplicity, but Python is more explicit, less magic, more nut-and-bolts configuration that’s clear, less convention vs. Ruby (that I sometimes really don’t agree with), but some language design decisions I think are dumbfounding. Again, from starting out, it’s a “6 of one...” consideration, when you get to making a choice, you can’t go wring with either, and one might agree with you more.

FYI, I also use C#, that’s just a language, but combine it with the ASP.NET framework and it can service up dynamic web pages via a web server like IIS (on Windows). PHP (which you’ve probably heard of), is like a language and web framework kind of rolled up into a single solution.

Same with Ruby, the web framework that popularized the language so much is Rails. It provide a way to serve up content, access backend data, create dynamic templates and implement an MVC pattern (for the web). The popular web framework for Python is Django, but there are dozens of options.

I just started a new project a few moths ago and I’m using Node.js, which is an asynchronous, non-blocking server platform that uses Javascript (yes, on the server side). Again, it has some general use (though clearly attenuated toward networking apps), but benefits greatly from using a framework layer to serve up content, handle routing, templates, etc. (FWIW, I’m using Express and Locomotive on the [Node] server side, Angular on the client [with a number of other Javascript libs]).

Like AoW said, HTML is markup for a client browser, CSS is the styling “language” for HTML. Javascript on the client side can be used to provide nice user experience (UX) like popup dialogs, dynamic page control (like only updating part of the page).

Where it’s probably a little confusing for a newbie, is the ambiguous line between server and client. A language with a web framework can serve up a page to a browser, and that page might have content that’s generated from a dynamic source (like a database), but it gets rendered into HTML/CSS and sent to the browser (that’s the only thing a browser can consume).

I would say get a little familiar with HTML/CSS, then maybe learn a language/framework and see how it works to generate that HTML with dynamic server code (some will still be simple static markup, some will use templates to generate markup/content, often times returned from a database).

Good luck.
I want to thank you for the thought out post, i found it really insightful and I have a better understanding of the differences between both languages. I want to learn both, but i'm going to start with Python.

Do you have any tips for a beginner? Any resources you suggest i look into?

TextWrangler (and BBEdit) will run Ruby and Python scripts directly from the editor (or in the Terminal), using the languages installed with the system. Just select the language from the drop down menu at the bottom of the editor window or from the Edit > Text Options... menu item, and then use one of the Run items in the shebang (#!) menu.
I'll keep that in mind, thanks

There is a website which helps people to learn python (and other languages) in a very nice way:

http://learnpythonthehardway.org


I made this online course on my own and think it was really good.
There is an instruction for the usage of the terminal as well.
(yay first post in this forum :) )
Wow that's such a great resource, thanks! (welcome to the forums! :) )
 

lukeg01

macrumors newbie
Jun 23, 2014
18
0
Hello
I recommend starting with python because HTML isn't a programming language but a markup language, to run a python script from command line go to the folder its in (using cd pathToFolder):
Code:
python ./filename.py
But if you give the script a python header like:
Code:
#!/usr/bin/env python
you can make it executable by doing:
Code:
 sudo chmod +x ./filename.py
and then execute it with
Code:
 ./filename.py
 

Stella

macrumors G3
Apr 21, 2003
8,422
5,044
Canada
I downloaded TextWrangler and Sublime Text 2. I was wondering how does one run Python code? I understand Yosemite already has a version of Python, but how do I actually execute on the code i write? Do i type it out on Sublime Text 2 and hit run?
After you've got a bit familiar with Python, I'd recommend PyCharm as an IDE if your planning to do a bit more than just learn in the future - there's a free community version. Before I'd stick with an editor - Sublime text is good.
 

wrldwzrd89

macrumors G5
Jun 6, 2003
12,107
75
Solon, OH
has anyone ever used pycharm ? if you have I'd like to know opinions

Thank you
Do keep in mind that PyCharm, like JetBrains' other IDEs, requires the legacy JDK 6 to run, still. The stated reason is multiple bugs in the Oracle JDK that JetBrains considers show-stoppers. You can get Apple JDK 6 here, if you need it: https://support.apple.com/kb/DL1572

That being said, I've heard good things about PyCharm, as Python IDEs go. It's not the one I use, though.
 

michaelsviews

macrumors 65816
Sep 25, 2007
1,245
263
New England
Do keep in mind that PyCharm, like JetBrains' other IDEs, requires the legacy JDK 6 to run, still. The stated reason is multiple bugs in the Oracle JDK that JetBrains considers show-stoppers. You can get Apple JDK 6 here, if you need it: https://support.apple.com/kb/DL1572

That being said, I've heard good things about PyCharm, as Python IDEs go. It's not the one I use, though.
Thank you for the information, is JDK dependency a "bad" thing ? What IDE do you use for Python?
 

wrldwzrd89

macrumors G5
Jun 6, 2003
12,107
75
Solon, OH
Thank you for the information, is JDK dependency a "bad" thing ? What IDE do you use for Python?
JDK 6 is no longer supported by Apple or Oracle. I use IEP as my Python IDE, because it has excellent support for multiple interpreters and integrates popular GUI framework debugging. Plus, it's written in Python ;)
 

foobarbaz

macrumors 6502a
Nov 29, 2007
548
610
I'll chime in with some advice: Don't worry so much and start coding already. :)

Developers can argue without ends about programming languages and tools. You can learn the basics with whatever is handy for you. (If you have programming friends, use whatever they use. If you find an online tutorial you enjoy, use that. Any old editor will get you started.)

You can pick the languages and tools you prefer later. Just learn about variables, loops and functions for now.
 

wrldwzrd89

macrumors G5
Jun 6, 2003
12,107
75
Solon, OH
I'll chime in with some advice: Don't worry so much and start coding already. :)

Developers can argue without ends about programming languages and tools. You can learn the basics with whatever is handy for you. (If you have programming friends, use whatever they use. If you find an online tutorial you enjoy, use that. Any old editor will get you started.)

You can pick the languages and tools you prefer later. Just learn about variables, loops and functions for now.
I could not agree more. When I was a new developer I too was a bit lost with all the tools out there, and it took me a while before I found my favorites for each language I ended up developing in. I focused on learning the language before trying to find a favorite tool to use with it, which I'm very glad I did: I think I am a better developer for listening to that advice.

Mind you, I'm still not where I want to be, with regard to testing. I tend to release stuff before it's ready. :oops:
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
8,750
4,440
I just go with Sublime for editing and command line tools for everything else when developing in C, C++, JavaScript, or Python.

I use Xcode for Obj-C/Swift because it ties in well with Interface Builder and I find it to be pure madness to try to design Cocoa UIs without IB.

I use IntelliJ for Java... Mostly because I don't know how to use the Java debuggers from the command line.
 

michaelsviews

macrumors 65816
Sep 25, 2007
1,245
263
New England
I just go with Sublime for editing and command line tools for everything else when developing in C, C++, JavaScript, or Python.

I use Xcode for Obj-C/Swift because it ties in well with Interface Builder and I find it to be pure madness to try to design Cocoa UIs without IB.

I use IntelliJ for Java... Mostly because I don't know how to use the Java debuggers from the command line.
IntelliJ will do python also by adding a plugin not sure about the other languages. Everyone has there own opinion and experience. For me sublime is ok but I must be doing something wrong and or the path is not right in the setup because I'm getting errors no matter what I do

"Error trying to parse settings: No data in ~/Library/Application Support/Sublime Text 2/Packages/User/Plain text.sublime-settings:1:1"

A friend of mine said to use iterm also. I'm still at the basic entry level, so for now I'm not concerned too much about it. I have pycharm the educational version on my system. There are so many different listings / recommendations on the web it'll make your head spin.
 

burne

macrumors 6502
Jul 4, 2007
301
42
Haarlem, the Netherlands
I put

Code:
print("Hello world")
In a .py file in Sublime and hit that keyboard shortcut. It spat out a cryptic error message.
You didn't save the file before running it.

/usr/bin/python: can't find '__main__' module in ''
[Finished in 0.2s with exit code 1]


tells you you tried to interpret and run an empty file.
 

michaelsviews

macrumors 65816
Sep 25, 2007
1,245
263
New England
In using sublime 2. I was told by a family member that there is setting that shows syntax error as your typing, but there not sure how to enable this ? Is this part of the program or is a plugin needed?

Thank you
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
8,750
4,440
In using sublime 2. I was told by a family member that there is setting that shows syntax error as your typing, but there not sure how to enable this ? Is this part of the program or is a plugin needed?

Thank you
Definitely a plugin. Sublime is a text editor - not an IDE.
 

michaelsviews

macrumors 65816
Sep 25, 2007
1,245
263
New England
Definitely a plugin. Sublime is a text editor - not an IDE.
Now when sublime2 start's I'm getting the following:

Error trying to parse settings: No data in ~/Library/Application Support/Sublime Text 2/Packages/User/Plain text.sublime-settings:1:1


Reinstalled and problem is gone, seems to be a problem from time to time though
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.