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

Discussion in 'Mac Programming' started by Reach9, Jan 16, 2015.

  1. Reach9 macrumors 68020

    Reach9

    Joined:
    Aug 17, 2010
    Location:
    In America
    #1
    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!
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    Congratulations / yay!


    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 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's redundant. You only need one. I recommend Sublime Text 2, but admittedly I don't think I've ever used TextWrangler.


    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.
     
  3. Reach9 thread starter macrumors 68020

    Reach9

    Joined:
    Aug 17, 2010
    Location:
    In America
    #3
    Thanks! :)

    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?

    That makes a lot of sense now, thanks!

    Great, can i use Sublime Text 2 for HTML, CSS, JavaScript, jQuery and AngularJS too? or do I need Dreamweaver?


    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!
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    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
     
  5. D.T. macrumors 603

    D.T.

    Joined:
    Sep 15, 2011
    Location:
    Vilano Beach, FL
    #5
    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.
     
  6. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #6
    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.
     
  7. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #7
    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.
     
  8. Thilus macrumors newbie

    Joined:
    Jan 18, 2015
    Location:
    Bielefeld, Germany
    #8
    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 :) )
     
  9. Reach9 thread starter macrumors 68020

    Reach9

    Joined:
    Aug 17, 2010
    Location:
    In America
    #9
    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?

    I'll keep that in mind, thanks

    Wow that's such a great resource, thanks! (welcome to the forums! :) )
     
  10. lukeg01 macrumors newbie

    Joined:
    Jun 23, 2014
    #10
    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
     
  11. Stella macrumors 604

    Stella

    Joined:
    Apr 21, 2003
    Location:
    Canada
    #11
    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.
     
  12. michaelsviews macrumors 65816

    Joined:
    Sep 25, 2007
    Location:
    Upstate NY
    #12
    has anyone ever used pycharm ? if you have I'd like to know opinions

    Thank you
     
  13. wrldwzrd89 macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #13
    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.
     
  14. michaelsviews macrumors 65816

    Joined:
    Sep 25, 2007
    Location:
    Upstate NY
    #14
    Thank you for the information, is JDK dependency a "bad" thing ? What IDE do you use for Python?
     
  15. wrldwzrd89 macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #15
    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 ;)
     
  16. foobarbaz macrumors 6502

    Joined:
    Nov 29, 2007
    #16
    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.
     
  17. wrldwzrd89 macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #17
    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:
     
  18. Dranix macrumors 6502a

    Dranix

    Joined:
    Feb 26, 2011
    Location:
    Gelnhausen, Germany
    #18
    Well I would recommend getting CodeRunner2 (https://coderunnerapp.com). PyCharm is in my opinion as bad as any gui program written in Java - Slow like hell and ugly.
     
  19. wrldwzrd89 macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #19
    Thanks, I'll look into CodeRunner 2.
     
  20. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #20
    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.
     
  21. michaelsviews macrumors 65816

    Joined:
    Sep 25, 2007
    Location:
    Upstate NY
    #21
    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.
     
  22. burne macrumors 6502

    burne

    Joined:
    Jul 4, 2007
    Location:
    Haarlem, the Netherlands
    #22
    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.
     
  23. michaelsviews macrumors 65816

    Joined:
    Sep 25, 2007
    Location:
    Upstate NY
    #23
    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
     
  24. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #24
    Definitely a plugin. Sublime is a text editor - not an IDE.
     
  25. michaelsviews, Jun 8, 2015
    Last edited: Jun 9, 2015

    michaelsviews macrumors 65816

    Joined:
    Sep 25, 2007
    Location:
    Upstate NY
    #25
    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
     

Share This Page