Skip to content Skip to sidebar Skip to footer

Force Subprocess To Use Python 3

So, I was trying to write a Python script that utilized subprocess to call another Python script in the same directory. This was all going well until an import statement within the

Solution 1:

Here's a way to force a script to be run with Python3:

#! /usr/bin/python3

import sys, subprocess

if sys.version_info[:2] < (3, 0):
    # FORCE PYTHON3
    code = subprocess.check_call(['python3'] + sys.argv)
    raise SystemExit(code)

print("Using Python v%d.%d" % sys.version_info[:2])

Example when run in Bash:

> python3 force_python3.py                                                                                                                         
Using Python v3.7

> python2 force_python3.py                                                                                                                         
Using Python v3.7

Post a Comment for "Force Subprocess To Use Python 3"