0

I am aware that this question keeps popping up, but I have tried most of the methods mentioned in the replies that I have seen for other questions and I am still facing this problem. I need my scripts to run with python 3.8.5 that I have installed at the location "C:\Python\Python3.8.5".

When I run py --version in the terminal, the output I get is Python 3.8.5. This is exactly as expected.

But when I make a script called print_sys.py as such,

import sys
print(sys.version)

and then run this by typing .\print_sys.py, I get the output as 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)]

On the other hand, if I run this same script by typing py print_sys.py, I get the output as 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:43:08) [MSC v.1926 32 bit (Intel)]

I want it to run the script with python3.8.5 when I try to execute the script by typing .\print_sys.py

In case it helps this is what my path variable in system variables look like.

System Variables > Path

This is what my PYTHONPATH variable in system variables looks like.

System Variables > PYTHONPATH

And this is what my path variable in user variables looks like

User Variables > Path

I suspect that it's that last python 2.7 from the windows build tools that is the problem but I don't see how that could be since python 3.8.5 comes first.

EDIT: I finally found a solution that works for me. I referred to this youtube video to find out how to change the file association for .py files on my machine. I still don't understand why just changing the order of the path variables didn't work so if anyone has any idea about that your answers will still be appreciated.

Adam Jijo
  • 82
  • 1
  • 7
  • Your system has both `python2` and `python3`. You need to uninstall `python2` or run your scripts using `python3` command such as - `python3 print_sys.py`. – Ajay A Jan 07 '21 at 18:30
  • But I think that ```python2``` is required for windows-build-tools so I don't think I can just uninstall it. I might be wrong about this but since it was installed automatically I think I might break something if I delete it. – Adam Jijo Jan 07 '21 at 18:34
  • Nothing gonna break, Its time to uninstall it, for backup, you have python3 – Ajay A Jan 07 '21 at 18:35
  • You could try changing your environment variable to have python3 supersede python2 by reordering the position. In this case, it looks like lower paths are prioritized. Try moving "C:\Python\Python3.8.5" below "C:\Users\adamj\.windows-build-tools\python27. Note that this may cause system problems if python2 is a dependency. – Ghoti Jan 07 '21 at 19:02
  • did that and still the same problem persists. – Adam Jijo Jan 07 '21 at 19:05
  • Does this answer your question? [Shebang Notation: Python Scripts on Windows and Linux?](https://stackoverflow.com/questions/7574453/shebang-notation-python-scripts-on-windows-and-linux) – rfkortekaas Jan 07 '21 at 19:13
  • not really as this requires me to use the py launcher and as I've said before that already works. What I need is a way to change my file association for .py files – Adam Jijo Jan 07 '21 at 19:25
  • Define a `PY_PYTHON` environment variable to tell `py` what the default python should be — i.e. `PY_PYTHON=3`. – martineau Jan 07 '21 at 19:39
  • did this, it made no difference. – Adam Jijo Jan 07 '21 at 19:43

1 Answers1

0

If you want to run your script as executable you need to tell the system what interpreter it should use - otherwise default interpreter will be used and that might not yield result you expect. Add shebang on top of your script and rerun, both should return the same output.

#!/usr/bin/python3

# your code

I'm not windows user though, shebang might or might not work depending on what environment you use to execute scripts. If you use MinGW or Ubuntu subsystem then you should get the same behavior as on linux.

Also in windows you can associate extension with program which executes it. I'd check what program you have associated with *.py extension. There must be a registry entry for that too.

Greg0ry
  • 931
  • 8
  • 25
  • 1
    OP is on Windows. That shebang line does nothing on Windows. –  Jan 07 '21 at 18:44
  • nope, I already tried that. With the shebang lines as either ```#!usr/bin/env python3``` or ```#!"C:\Python\Python3.8.5\python.exe"``` I'm still getting the output implying that the program is being interpreted by ```python2``` – Adam Jijo Jan 07 '21 at 18:45
  • So you are executing script in cmd? If so then maybe check the default program for *.py extension? – Greg0ry Jan 07 '21 at 18:46
  • okay so i tried the assoc and ftype commands that i found somewhere to find the file association and as suspected that points to ```python2```. Now how do i change that? From what I've found so far, putting ```python3``` higher than ```python2``` should automatically change the filetype association, but i might have misunderstood what i read. – Adam Jijo Jan 07 '21 at 19:06
  • This would be the correct answer, but OP is on Windows, not Linux. –  Jan 07 '21 at 19:30
  • Just try, by changing file sasociation there is nothing you can break (I mean I'm 99% sure). You can also go through the `backend` - I don't know if you have `regedit` on your version of windows, but long ago when I was working on windows I used to find related entries there (beware it's a bit of an epic maze). – Greg0ry Jan 07 '21 at 19:43