Search code examples
pythonnode.jschild-process

Change Python version for spawned script in Node


Here in index.ts, I've spawned in the script using child_process and am wondering how I can specify which Python version should be ran with it, since it defaults to 2.7 (the one that came with my mac), rather than Python 3.9.6(64-bit) which is my chosen interpreter within vscode.

import { spawnSync } from 'child_process';

const write_questions = spawnSync('python', ['src/write_questions.py', subject], {stdio: 'inherit'})

This command doesn't work because of the Python version being outdated and not including f-strings, however, it does work on another machine of mine without 2.7 on it at all:

basic_addition_file.write(f"What is {basic_addition_first} + {basic_addition_second}?: \ {basic_addition_answers}")

result of checking python version:

print(sys.version)

2.7.16 (default, Jun 18 2021, 03:23:53)

Solution

  • I just changed my default python version on mac, which was never a problem in the past since I could always just select my preferred Python Interpreter through VSCode but for this, it had to be done. So now when I type Python into my Terminal:

    python
    Python 3.9.6 (default, Jul 28 2021, 13:42:40) 
    [Clang 12.0.5 (clang-1205.0.22.11)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 
    

    Result of sys.version being printed:

    python write_questions.py
    3.9.6 (default, Jul 28 2021, 13:42:40) 
    [Clang 12.0.5 (clang-1205.0.22.11)]