MacsandaPi
Posts: 93
Joined: Tue Dec 30, 2014 7:44 pm

File saved in Python 3, it opens in Python 2

Tue Mar 08, 2016 11:56 pm

If I choose Python 3 (IDLE) from the Programming desktop menu, this opens the Python 3.4.2 shell from which I create a new file and enter my code. I then save that file. The next time I open the file and run the code, it opens in the Python 2.7.9 shell. The code runs fine in the Python 2 shell, but I would like to know why it defaults to that version. Why wouldn't it open in the Python 3.4.2 shell, since that was the application used to create the document initially? Is this a bug?

This is on a RPi B+, running the latest version of Jessie, if those details matter.

Thanks for an explanation.

User avatar
Douglas6
Posts: 4739
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: File saved in Python 3, it opens in Python 2

Wed Mar 09, 2016 12:12 am

MacsandaPi wrote:Is this a bug?
Nope. Both Python 2 and Python 3 use the same extension and file type. The OS is configured to use a certain editor to open a particular file type. That happens to be Idle on the Pi. If you want to use Idle3, open it, and use it to open your Python file.

stevech
Posts: 144
Joined: Sun Jul 15, 2012 11:53 pm

Re: File saved in Python 3, it opens in Python 2

Wed Mar 09, 2016 6:10 am

Or, as I do, avoid python 3. Keep it simple.

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: File saved in Python 3, it opens in Python 2

Wed Mar 09, 2016 6:24 am

MacsandaPi wrote:If I choose Python 3 (IDLE) from the Programming desktop menu, this opens the Python 3.4.2 shell from which I create a new file and enter my code. I then save that file. The next time I open the file and run the code, it opens in the Python 2.7.9 shell.
Do you have a shebang in the file telling it what version of python you want and it's not honouring that? Did you trying making it executable and then just running it from the command line?

scotty101
Posts: 3680
Joined: Fri Jun 08, 2012 6:03 pm

Re: File saved in Python 3, it opens in Python 2

Wed Mar 09, 2016 12:29 pm

stevech wrote:Or, as I do, avoid python 3. Keep it simple.
Tut tut tut. Only write in Python 2 if you absolutely can't write it in Python 3.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

MacsandaPi
Posts: 93
Joined: Tue Dec 30, 2014 7:44 pm

Re: File saved in Python 3, it opens in Python 2

Wed Mar 09, 2016 7:17 pm

Douglas6 wrote:
MacsandaPi wrote:Is this a bug?
Nope. Both Python 2 and Python 3 use the same extension and file type. The OS is configured to use a certain editor to open a particular file type. That happens to be Idle on the Pi. If you want to use Idle3, open it, and use it to open your Python file.
Yes, IDLE is configured to open a Python file, but does that mean that the latest version of Jessie is configured to use IDLE 2 as the default editor, regardless of the fact that the Python file was created initially with the IDLE 3 editor? That seems to be what's happening on my RPi.

MacsandaPi
Posts: 93
Joined: Tue Dec 30, 2014 7:44 pm

Re: File saved in Python 3, it opens in Python 2

Wed Mar 09, 2016 7:19 pm

stderr wrote:
MacsandaPi wrote:If I choose Python 3 (IDLE) from the Programming desktop menu, this opens the Python 3.4.2 shell from which I create a new file and enter my code. I then save that file. The next time I open the file and run the code, it opens in the Python 2.7.9 shell.
Do you have a shebang in the file telling it what version of python you want and it's not honouring that? Did you trying making it executable and then just running it from the command line?
No to both questions. I'm a novice trying to work my way through the CamJam EduKits' exercises; their instructions say to use Python IDLE 3. So I was just curious as to why, after starting and saving the file in Python 3, the Python 2 shell is used when I subsequently open the file to run the program again.

User avatar
DougieLawson
Posts: 35811
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: File saved in Python 3, it opens in Python 2

Thu Mar 10, 2016 8:03 am

Try adding this to the top line of your program

Code: Select all

#!/usr/bin/env python3
save the file (that line is called a "shebang" by a whole bunch of Linux and Unix geeks). Open an LXTerminal window and use
chmod 755 mypython3program.py # change the name there to your file name
and
./mypython3program.py # this gets the shell running it.
The first two characters are dot & slash which is just a short hand way of telling the shell to look in the current directory.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

dgordon42
Posts: 760
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: File saved in Python 3, it opens in Python 2

Sat Mar 12, 2016 10:08 pm

@ MacsandaPi,
By default in the GUI, the Pi opens Python files for editing in IDLE, which uses Python2. It does this regardless of what program was used to save the file. If you right click on a Python file in File Manager, and click "Open With...", you can select Python 3 (IDLE), and the Pi will remember to use Python3 IDLE in the future for that file.
If the Python 3 IDLE option does not appear in the "Open With..." menu, click "Open With" on the bottom of the menu, and select Python 3 IDLE from the Programming tab.

Hope this helps,
Dave.

MacsandaPi
Posts: 93
Joined: Tue Dec 30, 2014 7:44 pm

Re: File saved in Python 3, it opens in Python 2

Sun Mar 13, 2016 2:23 am

dgordon42 wrote:@ MacsandaPi,
By default in the GUI, the Pi opens Python files for editing in IDLE, which uses Python2. It does this regardless of what program was used to save the file. If you right click on a Python file in File Manager, and click "Open With...", you can select Python 3 (IDLE), and the Pi will remember to use Python3 IDLE in the future for that file.
If the Python 3 IDLE option does not appear in the "Open With..." menu, click "Open With" on the bottom of the menu, and select Python 3 IDLE from the Programming tab.

Hope this helps,
Dave.
Many thanks for the very helpful explanation. I'll use the "Open With..." option in the future.

Return to “Python”