0

I have installed python 2 after installing python 3.And now when I executing my python file by clicking on file (not by cmd) its run python 2 ,but I want python 3. I have tried script:

import sys
print (sys.version)

output was:

2.7.11

Can someone help me to make python 3 default on my pc. So when i run my file,it execute Python 3. Sorry for bad English.

5
  • Which OS are you using? Commented Aug 31, 2016 at 16:55
  • Windows 10. 64bit Commented Aug 31, 2016 at 17:01
  • 1
    On cmd try: python3 instead of python. The -V option would print the version directly like python -V should give 2.7.11 Commented Aug 31, 2016 at 17:03
  • Can you not right-click - open with python 3? Commented Aug 31, 2016 at 17:06
  • I think It has something common with Python launcher for Windows:PEP 397 @BrendanAbel Commented Aug 31, 2016 at 17:06

3 Answers 3

1

If the current default windows application for .py files is currently python2 (i.e. C:\python27\python.exe) and not the new py.exe launcher, you can just change the default windows application for the file type. Right-click on file -> properties -> click the change button for default application and change it to the python3 executable.

If the default application for the file is the py.exe windows launcher, you can add a shebang line in your scripts to force the python executable and the launcher should respect it. Add this as the first line of your file

#!C:\python3\python.exe

If you're python3 installation path is different, make sure to use that instead.

Sign up to request clarification or add additional context in comments.

1 Comment

The launcher is a bit more magical than you give it credit for; the canonical Linux style shebang #!/usr/bin/env python3 should work, or #!/usr/bin/env python3.5 to specify a minor as well. See PEP 397, section Shebang line parsing. Using the canonical shebang line means a portable script (no OS specific features used) runs on both Windows and Linux unmodified.
0

On cmd you can do py -3 for python 3 and py -2 for 2 but for click-starting the simplest way is to include a line #! python2 or #! python3as first line in file.

You were on the right trach - it is mentioned in PEP 397 in section "Shebang line parsing"

Comments

0

Assuming you have python3 installed, you can us virtual environment mechanisms built into python3 to prevent errors just like this.

I saw in the comments you are using Windows, so the following steps to ensure that you are using the intended version of Python every time.

first navigate to your projects directory and run the command: c:\Temp>c:\Python35\python -m venv myenv. This will create a directory myenv with scripts to create your virtual enviroment.

Next activate your virtual enviroment with the command: C:\> .\myenv\Scripts\activate.bat. This will change your environment to what is set in the virtual environment.

Now run the command python to see that python 3.5 is being run.

to exit the virtual environment just run deactivate.bat

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.