Page 1 of 1

File saved in Python 3, it opens in Python 2

Posted: Tue Mar 08, 2016 11:56 pm
by MacsandaPi
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.

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

Posted: Wed Mar 09, 2016 12:12 am
by Douglas6
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.

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

Posted: Wed Mar 09, 2016 6:10 am
by stevech
Or, as I do, avoid python 3. Keep it simple.

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

Posted: Wed Mar 09, 2016 6:24 am
by stderr
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?

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

Posted: Wed Mar 09, 2016 12:29 pm
by scotty101
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.

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

Posted: Wed Mar 09, 2016 7:17 pm
by MacsandaPi
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.

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

Posted: Wed Mar 09, 2016 7:19 pm
by MacsandaPi
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.

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

Posted: Thu Mar 10, 2016 8:03 am
by DougieLawson
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.

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

Posted: Sat Mar 12, 2016 10:08 pm
by dgordon42
@ 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.

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

Posted: Sun Mar 13, 2016 2:23 am
by MacsandaPi
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.