cmblake
cmblake

Reputation: 21

Python shebang not forcing other version [Mac]

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"

Upvotes: 2

Views: 7764

Answers (5)

Marcos
Marcos

Reputation: 866

For example, in a python script file called script.py, in my case I have a 3.7 Python at the following path:

#! /Library/Frameworks/Python.framework/Versions/3.7/bin/python3

import sys

print("--- Python version ---")
print(sys.version)
print("--- Python version info ---")
print(sys.version_info)
print("--- Python path executable ---")
print(sys.executable)

As others say, give permissions and do a proper execution so that shebang can be applied:

  1. In the terminal type the following to make script.py a executable file for your user, that is:
    $ chmod u+x script.py
  1. In the terminal script.py is run with ./ at the beginning (not using python):
    $ ./script.py

In my case I've got:

--- Python version ---
3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21) 
[Clang 6.0 (clang-600.0.57)]
--- Python version info ---
sys.version_info(major=3, minor=7, micro=3, releaselevel='final', serial=0)
--- Python path executable ---
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3

Considerations:

  • shebang path cannot contain spaces.
  • "Python path executable" not necessarily match with shebang path as long as the python at the shebang path can be an "alias". Aliases in Mac point to other file, you can see it with right click on a file and select "Get info" to check the "Original" value.

Upvotes: 1

Ismael Luceno
Ismael Luceno

Reputation: 2119

The shebang path is only used by the OS when you make the file executable and run it directly, like this:

chmod 755 pyscript.py
./pyscript.py

For python, the shebang is only a comment. The only way to force it even when calling the interpreter directly on the CLI is to compare the version and if it's less re-launch it using os.execv or similar. Something like this should do (not tested though):

#!/usr/local/bin/python3.4
import sys
import os
if (sys.hexversion < 0x3040000):
     sys.argv.insert(0, '/usr/local/bin/python3.4')
     os.execv(sys.argv[0], sys.argv)

print(sys.version)
print("Hello, World!")

You might want to use env on the shebang to avoid specifying the path, and avoid hardcoding the path on the python code too...

Upvotes: 1

user289086
user289086

Reputation:

Depending on your installation, python can be installed in a multitude of places on OS X.

Without any other additional changes, you are likely running the python located in /usr/bin/python. This can be verified by typing which python

~ $ which python
/usr/bin/python

Note that while /usr/local/bin/python3.4 may be the correct path to python 3.4, when you type python script.py you are not invoking the command you found when you did which python3.4.

To fix this you can do one of:

  • Invoke the script directly. Type script.py (with the executable bit) rather than as python script.py. You may need to change the permissions on the script to executable with chmod u+x script.py.
  • Change the version in /usr/bin/python to the version you want. Note that this may be very dangerous in that other things expecting the base install python of 2.7.5 can become very unhappy
  • Change your $PATH to put /usr/local/bin/ before /usr/bin and have python in /usr/local/bin/python point to the 3.4 version.

Upvotes: 1

ojblass
ojblass

Reputation: 21620

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.

Upvotes: 4

Vlad
Vlad

Reputation: 9481

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

Upvotes: 2

Related Questions