Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python shebang not forcing other version [Mac]

Tags:

python

macos

I'm new to Python and programming in general. I'm trying to force my scripts to use Python3.4 as installed using the python installer from python.org.

My script has this.

#!/usr/local/bin/python3.4
import sys
print(sys.version)
print("Hello, World!")

Terminal returns this:

$ python pyscript.py
2.7.5 (default, Aug 25 2013, 00:04:04) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)]
Hello, World!

The shebang path is correct, according to "which python3.4"

like image 658
cmblake Avatar asked Nov 25 '25 16:11

cmblake


2 Answers

By calling python on your script you are using the python in your path. As the comment suggests run the file directly after setting the execution bit.

like image 134
ojblass Avatar answered Nov 28 '25 16:11

ojblass


Shebang is only used when you run your script directly using command like this

$ ./pyscript.py

When you python interpreter from command line, the shell doesn't consult shebang line. It simply runs the first python executable it finds on the command line.

To change the default python executable, adjust your path. Or better yet use python virtual environment. More information on virtual environment is here

like image 27
Vlad Avatar answered Nov 28 '25 16:11

Vlad