python3 on mac

Discussion in 'Mac Programming' started by JonnyFrond, Feb 28, 2012.

  1. macrumors newbie

    Joined:
    Jan 15, 2011
    #1
    Hi guys,

    Has anyone successfully got python3 to work on a mac?

    I am trying to learn python, and have a book that teaches python3. my Mac only have python 2.7.2. So I install python3, yet every time I try to run a program I have written, it tries to run in python 2 and stops with syntax errors.

    I know my code is good, it is a tiny program the problem is with
    'print (end=" ")'
    Which works with python3 but not python2.

    How can I get this to work, so that I can learn this language easier? oh, and without compromising my mac.

    Python3 doesn't have to be default, but I need to know how to run executables with it

    Kind regards

    Jonny
     
  2. macrumors regular

    Joined:
    Apr 7, 2011
    #2
    I'm using the installer from python.org with no trouble whatsoever.
     
  3. macrumors 68030

    Joined:
    Oct 19, 2011
    Location:
    Switzerland
    #3
    How do you launch the program?

    You could try to launch it using the full path to the python 3 executable, wherever it is located. Example:
    Code:
    /usr/bin/python2.6 myprogram.py
    I don't have a python 3 installation, so I use an example of running an older version instead of python 2.7 which is default on my system.

    Depending on what you want to do eventually, learning python 2 might be more useful. Many useful libraries are still based on python 2.X. For GUI programming, it seems that at least TKinter is compatible with python 3 now.
     
  4. macrumors 603

    Joined:
    Aug 9, 2009
    #4
    Post the actual command-line you used. The one that you wanted to run python3, but that ran the builtin python instead.

    Post the exact title, author, and edition of the book you're using.

    Also, run the following command and post the complete output:
    Code:
    echo $PATH
    
     
  5. macrumors regular

    Joined:
    Jun 29, 2010
    Location:
    The Netherlands
    #5
    I think the problem is in the print statement, if I understand you correctly. One of the differences between python 2 and 3 is that 'print' is a function in python 3 instead of the thing it is in python 2. (I don't how to call it in python 2)

    So print( "Hello, world!") in python 3 has to be written as print "Hello, world!" in python 2. (so without the parentheses.) I think you will be fine with Python 2.7.2 on a beginner level as long as you remember this.
     
  6. macrumors 6502

    Joined:
    Apr 29, 2010
    #6
    After I built and installed Python 3, I had more than one version of Python in /usr/local/bin. Maybe OP just needs to look there for that version of Python?
     
  7. macrumors regular

    Joined:
    Apr 20, 2012
    #7
    When you run a python script in terminal you need to use the python3 command instead of python. Even after you install python 3, if you say "python foo.py" it will run with python 2. You need to say "python3 foo.py"
     

Share This Page