0

I have simple python file main.py:

import pygame
print("Hi There")
input()

I have installed pygame, and the default app to open .py files is set to python.exe, but when i double click the file it won't open. If i try the same without the import line it can run with no problem.

I tried to create run.bat file to open the python file:

python main.py

This is the console output:

pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Hi There

Everything runs without problem when using run.bat file.

So my question is why i cannot open the main.py file by just double-clicking, and how to make it work?

Bonny4
  • 271
  • 3
  • 6
  • that's because file association points to a different python version than the one you have in your path. That's the "magic" of windows. create a simple python script containing `import sys` then `print(sys.executable)` then `input()` and run by clicking or with python prefix you'll see I'm right – Jean-François Fabre Mar 16 '20 at 10:54
  • You can add a shebang notation on the top. Check this out https://stackoverflow.com/questions/7574453/shebang-notation-python-scripts-on-windows-and-linux – Hussain Bohra Mar 16 '20 at 11:38
  • @HussainBohra - Does a shebang do anything on a Windows platform? – lit Mar 16 '20 at 15:21
  • @Jean-FrançoisFabre Thanks – Bonny4 Mar 16 '20 at 16:34
  • @lit it does, in python 3 only. But I don't recommend it – Jean-François Fabre Mar 16 '20 at 17:02

1 Answers1

0

From comment by @Jean-FrançoisFabre. The problem was that when I was just double-clicking the python file was running from different version of python than when I run it from cmd. By using of simple python script containing import sys and then print(sys.executable) I was able to change the default python version for opening .py files in windows.

Bonny4
  • 271
  • 3
  • 6