Compiling Python?

Discussion in 'MacBook Pro' started by geekygeek, Nov 21, 2011.

  1. geekygeek macrumors regular

    Joined:
    Jul 12, 2011
    #1
    I'm only in elementary school :p. I have strong interests in coding and computers. I decided to start with Python as other people suggested for me. I have OS 10 Lion and I can't find a good complier for python, can someone find a good complier for me? Also will the python scripts on Mac run on windows as well?:confused:
     
  2. thundersteele macrumors 68030

    Joined:
    Oct 19, 2011
    Location:
    Switzerland
    #2
    http://python.org/

    Everything is available there. Compilers, documentation, tutorials, useful links.

    Python scripts should run on any OS.
     
  3. AdrianK, Nov 21, 2011
    Last edited: Nov 21, 2011

    AdrianK macrumors 68020

    Joined:
    Feb 19, 2011
    #3
    You need the interpreter, which OS X already has.

    Open Terminal, then type 'python', then you can type and run python statements line by line.

    Or to run a .py, cd to the directory of the py in Terminal, then type "python my_file.py".

    I would strongly suggest using a text editor with syntax highlighting if you don't have one already, it will make things a lot easier. A lot of text editors designed for development will let you run your scripts in terminal at the touch of a button too. I use TextMate but there's probably a cheaper/free alternative.

    Not unless they use the 'os' library to issue commands via the terminal/cmd, those commands are system specific, but anyway you probably won't need that for a while.
     
  4. geekygeek thread starter macrumors regular

    Joined:
    Jul 12, 2011
    #4
    I see a lot of people prograrmming you youtube and stuff. They have it on PC and they use notepad for Python and they color the text automatically. Can you help me a find a free alternative for that isntead of using Terminal for python commands? Because I can't save in Terminal.\
     
  5. VMMan macrumors 6502a

    Joined:
    Mar 29, 2009
    #5

    I haven't much used vim or emacs, but they are already on your mac and some programmers seem to like them a lot. You have to be comfortable with the command line, though, and memorize a lot of key bindings.
     
  6. steviem macrumors 68020

    steviem

    Joined:
    May 26, 2006
    Location:
    New York, Baby!
    #6
    Hey, I'm literally just trying this out too. Way past Elementary school though, so it's awesome you are learning it at your age, wish I did!

    I'm on Lion too. I have tried gedit and TextWrangler for the samples and have found that TextWrangler is really nice. It's BBEdit's free brother and you can download it from the App Store. If you save a file as a .py file, it will give you the syntax coloring that you're looking for. gedit seems to crash when I try to resize it.

    Are you reading a book as you learn? I'm reading 'Learn Python the Hard Way' which is really easy to read. There's a lot of typing, copying code from the book (but no copy/pasting) to get yourself setup to think like a programmer.

    Once you write your .py file, you don't need to compile anything, just run 'python myfile.py' in the terminal and the file will run, rather than give you the python prompt.

    Don't bother with vim or emacs. They will just annoy you and distract you from what you are doing at this stage.
     
  7. mape2k macrumors regular

    Joined:
    Apr 18, 2011
    Location:
    Germany
    #7
    I would suggest Eclipse with the Pydev toolkit. Eclipse has so many nice features that text editors usually lack and is just a more professional environment. Comes for free, too :)
     
  8. steviem macrumors 68020

    steviem

    Joined:
    May 26, 2006
    Location:
    New York, Baby!
    #8
    If you're learning Python, you're probably just learning how to code. Using your own brain to debug your code is the main thing. Once I'm writing 'real applications', I'll be going for a better development environment.
     
  9. thundersteele macrumors 68030

    Joined:
    Oct 19, 2011
    Location:
    Switzerland
    #9
    idle is a great editor/console for python. Excellent code completion and highlighting.

    I switched to using ipython as console replacement, since it can be launched with pre-loaded matplotlib libraries, that make plotting quick and easy. I guess this is mostly useful for scientific users.
     
  10. geekygeek thread starter macrumors regular

    Joined:
    Jul 12, 2011
    #10
    Programming intimidates me, not the code itself but the different varieties of it. Like Python, then there's jython, ironpython and others. There is like tens of different C language varieties. HTML even intimidated a little, with HTML 5, Java, CSS etc. I feel like I need to learn them all to be good at programming, thats just makes me not learn anything at all.

    ----------

    Nevermind the above post I made, can I compile python in TextEdit and make the text colored? I have seen people doing it on notepad in Windows so I assume it will work in TextEdit as well.

    ----------

    Where's a good place to learn Python? I don't want it to be too confusing either. Would youtube be a better place to learn since it has audio and visual or would a normal webpage work better?
     
  11. geekygeek thread starter macrumors regular

    Joined:
    Jul 12, 2011
    #11
    I spent days trying to find a good complier and I got TextWrangler from Mac App store and tried my first code
    print "Hello there world!"
    Saved it as a .py and it automatically changes it to color. Now all I need is a good source for learning and I'll be on my way!:)

    EDIT: I made a folder on my desktop called "Python Codes" and I put my first project there. When I open it, it automatically launches Xcode and opens the file. What would happen if I didn't have xCode?
     
  12. thundersteele macrumors 68030

    Joined:
    Oct 19, 2011
    Location:
    Switzerland
    #12
    The programming language is to a large extent secondary. One has to start somewhere.

    I would try one of the many online tutorials for python, available e.g. on the python webpage. As far as I can see, TextEdit does not support syntax highlighting (this is what makes the text colored).

    Again, I recommend "idle", which is part of python.
    http://www.python.org/getit/mac/

    Python code is not compiled.

    From my personal experience, I only managed to learn a language when I had some goal I wanted to achieve with it, i.e. a small project. Otherwise I would end up getting bored with the tutorials, and not progress beyond the initial chapters.
     
  13. calderone macrumors 68040

    calderone

    Joined:
    Aug 28, 2009
    Location:
    Seattle
    #13
    To comment on the intimidation: You need to learn and understand the basics.

    variables
    functions/methods
    classes
    objects
    inheritance
    (this is just scratching the surface).

    You can do this in most any language. Python is a high-level language, so it abstracts you from a lot of the low level programming but for getting your feet wet it is a good language, I use it daily and love it.

    Once you learn the basics those things can be applied to any language, it is just a matter of using the conventions of the language. Think of all programming languages being different dialects of the same fundamental language (the basic concepts of programming).

    Ultimately, you need to decide what you actually want to do with what you learn. It is futile to learn a language if you are not going to have a practical use for it.

    HTML and CSS are not programming languages.

    -------------------------------
    Compiling python...

    Python isn't a generally a compiled language. The Python interpreter is what takes your Python code and translates it to machine code at runtime. At that time it may also create complied versions that are machine code ready (typically custom modules).

    For now, I would think of Python like this: When I run my python code through the Python interpreter translates that code to machine code.

    So Python files (.py) are literally like plain text files. There are style guidelines. PEP 8 is a must read if you plan to write in Python: http://www.python.org/dev/peps/pep-0008/

    Colored text is worthless if you don't know what it means. Colored text is syntax highlighting, in other words visual cues that let you know what you are looking at, be it a function or class.

    My personal favorite is TextMate. The Windows folks are probably using Notepad++. TextEdit does not Syntax Highlight, nor am I aware of any plugins to make that the case. At the end of the day, you have to pick the right tool for you.

    Try out different text editors and IDEs (Integrated Development Environment). I am fairly certain Xcode can do syntax highlighting for Python.

    -------------------------------

    Learning Python resources. This is really going to come down to what you want to do with the language. It is one thing to go through a book and get a bunch of examples that have no real world relevance, but is another to set out to create something and use those resources to assist you.

    My recommendation is to use something generic that gets you into the basics of using Python. I would also recommend that you avoid videos, for a few reasons:

    1. The people you are learning from may be new and teaching you incorrectly
    2. It isn't the proper medium for this kind of material. You will often find yourself having to skip backwards to see a line of code or to get some crucial definition. Unless the video is accompanied with: 1) source code 2) supporting material I would avoid (this rules out most YouTube videos).

    This place will be your best friend:
    http://docs.python.org/

    Learn how to read Python documentation.

    This should set you off in a decent direction:
    http://www.python.org/about/gettingstarted/

    Good luck.
     
  14. geekygeek thread starter macrumors regular

    Joined:
    Jul 12, 2011
    #14
    OMG this is so fun!

    I used text wrangler and THIS IS SO FUN!! :D. I feel so proud and happy. My codes so far are
    print "Hello World"

    And the one I just made after I learned about variables

    computers = 10
    geeks = 5
    each_geek_has = 10 / 5
    print "there are", geeks, "geeks"
    print "and there are", computers, "computers"
    print "so therefore each geek has", each_geek_has, "computers"
    FUN FUN FUN, now I understand why people stay up all night to program! I feel so hyper lol.
     
  15. steviem macrumors 68020

    steviem

    Joined:
    May 26, 2006
    Location:
    New York, Baby!
    #15
    Definitely take a look at the book I'm reading, or even just google it: learning python the hard way. It gets you typing code and learning concepts fairly quickly, it isn't really hard, just is a lot of typing. But if you want to be a developer, you need to like typing at least a little bit!

    You're definitely right though, I'd like to be finishing off the extra credit exercise I'm on now...

    I only started reading the book yesterday and I'm on exercise 31, so breaking into if, elseif and else statements. It spends at long time on the very basics, which makes it nice to start on.
     
  16. geekygeek thread starter macrumors regular

    Joined:
    Jul 12, 2011
    #16
    I'm using that as well! I learning variables right now, I skipped the math part but I'm back to it. Here's my code for the math part

    print "I will count my employees"
    print "Accountants", 3+7
    print "Assitant Managers", 1+3
    print "Web designers", 28/4
    print "Office workers", 30*28
    print "Number of computers", 230+89+60
    print "What is 20 printers + 38 printers in the office place?", 20+38
    print "Are there more web designers than accountants?", 3+7<28/4
    print "I have finished counting my workers"

    This is really FUN! I'm going to start a blog about it.
     
  17. geekygeek thread starter macrumors regular

    Joined:
    Jul 12, 2011
    #17
    My first syntax error and can't figure out the problem, my code is


    computer_brand = Apple
    processor = Intel Core 1.7GHz i5
    ram = 4 GB of 1333MHz DDR3
    operating_system = Mac OS 10 Lion
    battery_life = 7 hours
    screen_size = 13.3 inches
    the_cost = $1300
    stroage_capacity = 128 GB

    print "This is about my %s computer." %s computer_brand
    print "Its a %s computer so it runs %s." %s computer_brand %s operating_system
    print "It has a %s processor with %s of ram." %s processer %s ram
    print "The battery life is very long with %s long battery life, WOW!" %s battery_life
    print "It can store %s of files and has a %s high resolution screen." %s stroage_capacity %s screen_size
    print "And it only costed %s, thats very cheap for a good and high quality %s computer." %s the_cost %s the_computer_brand
    print "I can add the numbers up for fun too, I can add %s, %s, %s, %s, and %s and my answer is." %s ram, battery_life, screen_size, the_cost, storage_capactiy, ram + battery_life +screen_size + the_cost + storage_capacity

    And the error message I'm getting is
    Error: File 5 More variables.py; Line 2:
    SyntaxError: invalid syntax

    I don't see anything wrong with it
     
  18. calderone macrumors 68040

    calderone

    Joined:
    Aug 28, 2009
    Location:
    Seattle
    #18
    You need to quote the values.

    Example.

    computer_brand = 'Apple'
     
  19. geekygeek thread starter macrumors regular

    Joined:
    Jul 12, 2011
    #19
    I fixed line number 2 and put all the others in quotes as well. I then got a another error at line 10 that says something like
    print "This is about my %s computer." %s computer_brand
    I randomnly messed around with it and put parentheses before computer and after brand so its like this
    print "This is about my %s computer." %s (computer_brand)
    And that fixed the problem. What I know is that you do parentheses when you have multiple %s in each print line.

    ----------

    I had to keep adding parentheses to each one until I got this

    computer_brand = 'Apple'
    processor = '1.7 gigahertz i5'
    ram = '4 GB of 1333MHz DDR3'
    operating_system = 'Mac OS 10 Lion'
    battery_life = '7 hours'
    screen_size = '13.3 inches'
    the_cost = '$1300'
    storage_capacity = '128 GB'

    print "This is about my %s computer." %s (computer_brand)
    print "Its a %s computer so it runs %s." %s (computer_brand, operating_system)
    print "It has a %s processor with %s of ram." %s (processer, ram)
    print "The battery life is very long with %s long battery life, WOW!" %s (battery_life)
    print "It can store %s of files and has a %s high resolution screen." %s (storage_capacity, screen_size)
    print "And it only costed %s, thats very cheap for a good and high quality %s computer." %s (the_cost, computer_brand)
    print "I can add the numbers up for fun too, I can add %s, %s, %s, %s, and %s and my answer is." %s (ram, battery_life, screen_size, the_cost, storage_capactiy, ram + battery_life +screen_size + the_cost + storage_capacity)

    Now I have a error in 10 (9th line) and the error is
    NameError: name 's' is not defined.
    (Its the line that says "This is about my %s computer" incase if you were confused)
     
  20. calderone macrumors 68040

    calderone

    Joined:
    Aug 28, 2009
    Location:
    Seattle
    #20
    Well, even the fix has syntax problems.

    It should read:

    Code:
    print "This is about my %s computer." % (computer_brand)
    Notice the difference between the string and the placeholder values. %s is a placeholder. You only need a % after the string and then the placeholder values.

    And yes, when you have multiple placeholders, you must enclose them in parenthesis. For example.

    Code:
    print "It can store %s of files and has a %s high resolution screen." % (storage_capacity,screen_size)
    Based on what you have posted you need to:

    1. Spend more time understanding what you are typing. The errors above could have been prevented if you understood what everything was doing.
    2. Read PEP 8. http://www.python.org/dev/peps/pep-0008/
     
  21. geekygeek, Nov 22, 2011
    Last edited: Nov 22, 2011

    geekygeek thread starter macrumors regular

    Joined:
    Jul 12, 2011
    #21
    I'm getting so frustrated. I'm encountering multiple problems now. First and main problem is that TextWrangler automatically opened a window called "Unix Script Output" and every time I open one of my python programs and run it it builds up. I'll post a video of it ASAP.

    ----------

    Thank you for staying with me on this here's the video http://www.mediafire.com/?6hvoc3di46vh135. I trying to let you see that the things I run build up in the "Unix Script Output".

    ----------

    I do understand what I'm doing. I'm using this website http://learnpythonthehardway.org/book/ex5.html for my latest one for my computer specs and stuff. I still have troubles with the last line for the computer specs. Here's the picture
    http://imgur.com/qSqMK
    And the whole last line's code is
    Code:
    print "I can add the numbers up for fun too, I can add %s, %s, %s, %s, and %s and my answer is." % (ram, battery_life, screen_size, the_cost, storage_capacity, ram + battery_life +screen_size + the_cost + storage_capacity)
    The last line code seems to be fixed because when I try to launch it, it now builds up in the Unix Script Output instead in the error box.
     
  22. steviem macrumors 68020

    steviem

    Joined:
    May 26, 2006
    Location:
    New York, Baby!
    #22
    Are you doing the exercises in that book, in order, typing them out yourself though? You said that you skipped ahead. That's not the point of those exercises. They are there so you get used to typing the code properly. There's no need in writing your own code just yet when you're still learning the very basics (I'm at the same stage too, just not trying to run before I can walk).
     
  23. geekygeek thread starter macrumors regular

    Joined:
    Jul 12, 2011
    #23
    I'm typing them out myself and I did skip lesson 2 and moved on to lesson 3 with no problems but I went back to lesson 2 right after I got number 3 down. Can someone just help me fix the problem?
     
  24. steviem macrumors 68020

    steviem

    Joined:
    May 26, 2006
    Location:
    New York, Baby!
    #24
    Where are the numbers in there? You only have strings.

    Open Python in terminal and try adding the values you gave ram + battery_life. It won't work. You need to give these just numbers.
     
  25. geekygeek thread starter macrumors regular

    Joined:
    Jul 12, 2011
    #25
    I got all the problems fixed but now I have a question. I'm on chapter 5 http://learnpythonthehardway.org/book/ex5.html and I don't get the extra credit. What does %r do? I tried using it instead of using %s but it just does the same thing as %s.
     

Share This Page