1

Python being the new fashionable language, especially in Finance industry, I started learning it. I downloaded it from the Python website - Python version 3.5.2 - and it successfully installed... in my Application folder. Just so you know a little bit more about myself, I did an IT engineering school in France, so I have an IT culture, but I never had the soul of a hacker so some things might be more difficult than others.

Started to code on Python IDE, then I created an executable python file and when I tried to execute it ... error!!

This is what I got when I executed my file (bissextile.py - file is supposed to ask the user to enter a year and tell him if this year is bissextile):

    Last login: Tue Aug  9 23:24:02 on ttys000
    MacBook-Pro-de-Tebah:~ tebahsaboun$ cd '/Users/tebahsaboun/Desktop/' && '/usr/bin/pythonw' -d -v  '/Users/tebahsaboun/Desktop/bissextile.py'  && echo Exit status: $? && exit 1
    # installing zipimport hook
    import zipimport # builtin
    # installed zipimport hook
    # /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py
    import site # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.pyc
    # /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py
    import os # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc
    import errno # builtin
    import posix # builtin
    # /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.py
    import posixpath # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc
    # /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/stat.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/stat.py
    import stat # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/stat.pyc
    # /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/genericpath.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/genericpath.py
    import genericpath # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/genericpath.pyc
    # /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/warnings.pyc matches 
    [...] A LOT OF STUFF THAT'S THE SAME AS BEFORE
    import encodings.aliases # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/aliases.pyc
    # /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.py
    import encodings.utf_8 # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.pyc
    Python 2.7.10 (default, Oct 23 2015, 19:19:21) 
    [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    File "/Users/tebahsaboun/Desktop/bissextile.py", line 1
        {\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
                                                          ^
    SyntaxError: unexpected character after line continuation character
    # A lot of cleaning up files after that...

So, I saw here two issues: 1) my file doesn't work ... 2) the shell is not using Python 3.5 but Python 2.7 which is the problem I need help for.

Lots of research on internet, I was about to follow that tutorial: https://wolfpaulus.com/journal/mac/installing_python_osx/comment-page-3/#comment-101285 ...and about to delete some system files from my mac :) which it didn't let me do...decided that I didn't really know what I was doing and that I should dig a little more in the internet. For the record, here is what I have in /System/Library/Frameworks/python.framework/Versions/ :

So I found something called "Homebrew" that is suppose to install Python for you and I followed this great tutorial: http://blog.manbolo.com/2014/09/27/use-python-effectively-on-os-x#p1 Asking Homebrew to install Python 3.5 instead of Python 2.7 (steps are absolutely the same) which I apparently did.

So I did the verification suggested in the tutorial to be sure that I was using the right version of Python but when I asked the shell I still got Python 2.7. I checked my /etc/paths file which is as follows:

    /usr/local/bin
    /usr/bin
    /bin
    /usr/bin
    /bin

/usr/local/bin is the first binary in the file and I checked the folder it contains Python 3.5 indeed. But no matter what is the first line is in that file I get : MacBook-Pro-de-Tebah:~ tebahsaboun$ which python /usr/bin/python and :

    **MacBook-Pro-de-Tebah:~ tebahsaboun$ python
    Python 2.7.10 (default, Oct 23 2015, 19:19:21) 
    [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.**

I can't put all the links that I have researched so far ( especially stack overflow articles by the way ;) ) cause I don't have enough "reputation", but I did a lot of research and no article seems to respond to my problem. And I don't understand what is wrong.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ms.Jaffe
  • 11
  • 4
  • Did you try running `python3`? What do you get when you run `which python3` in the shell? – zarak Aug 10 '16 at 17:04
  • MacBook-Pro-de-Tebah:Desktop tebahsaboun$ which python3 /Library/Frameworks/Python.framework/Versions/3.5/bin/python3 --> so it is the Python that is in my "user" folder but still when I execute my file, it doesn't use this one. – Ms.Jaffe Aug 10 '16 at 17:10
  • Well, I just used the command python3 with my file, it's still returning an error that I don't understand. And my main concern is to have the latest version of Python installed and used by default by the OS. – Ms.Jaffe Aug 10 '16 at 17:16
  • Please show the error after you tried to run python3 – Anoop saju Aug 10 '16 at 17:20
  • MacBook-Pro-de-Tebah:Desktop tebahsaboun$ python3 ./bissextile.py File "./bissextile.py", line 1 {\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470 ^ SyntaxError: unexpected character after line continuation character – Ms.Jaffe Aug 10 '16 at 17:39
  • It's [not recommended](http://stackoverflow.com/questions/5846167/how-to-change-default-python-version) to have python3 as your default system python as it will probably break a lot of programs that expect a `python2` binary. Your best bet is to use an alias or a [virtual environment](http://docs.python-guide.org/en/latest/dev/virtualenvs/). – zarak Aug 10 '16 at 17:55
  • 1
    ok, thank you for your answer @zarak. – Ms.Jaffe Aug 10 '16 at 18:01
  • ok, thank you for your answer @zarak. I found a solution, overriding the path of the interpreter at the beginning of my python file. and i think i had a problem with the encoding too. Now, it almost works. I still get an other error at the line "os.system("pause")" is not recognized (i.e. "command not found) even though I imported the module "os". – Ms.Jaffe Aug 10 '16 at 18:09

1 Answers1

0

Run the program from command line using python3 my_script.py or add shebang #!/urs/bin/python3

warownia1
  • 2,771
  • 1
  • 22
  • 30