Cambridge-ICCS / ONEFlux

Open Network-Enabled Flux processing pipeline
Other
0 stars 0 forks source link

python_version_interface #5

Open j-emberton opened 1 month ago

j-emberton commented 1 month ago

Need to be able to run python 3 code within a python 2 environment.

Propose creating a abstract python 2.7 function that calls the python 3 code as a subprocess.

Requirements:

j-emberton commented 1 month ago

make file ideas:

Define paths for virtual environments

PY27_ENV = py27env PY3_ENV = py3env

Define the Python versions

PY27 = python2.7 PY3 = python3

Define paths for scripts

MAIN_SCRIPT = main.py EXAMPLE_SCRIPT = example.py

Rule to create the Python 2.7 virtual environment

$(PY27_ENV)/bin/activate: requirements-py27.txt virtualenv -p $(PY27) $(PY27_ENV) $(PY27_ENV)/bin/pip install -r requirements-py27.txt

Rule to create the Python 3.x virtual environment

$(PY3_ENV)/bin/activate: requirements-py3.txt $(PY3) -m venv $(PY3_ENV) $(PY3_ENV)/bin/pip install -r requirements-py3.txt

Rule to run the Python 2.7 script

run_py27: $(PY27_ENV)/bin/activate $(PY27_ENV)/bin/python $(MAIN_SCRIPT)

Rule to run the Python 3.x script

run_py3: $(PY3_ENV)/bin/activate $(PY3_ENV)/bin/python $(EXAMPLE_SCRIPT) 10

Rule to clean up the virtual environments

clean: rm -rf $(PY27_ENV) $(PY3_ENV)

Rule to run everything

all: run_py27 run_py3

j-emberton commented 1 month ago

Example of python2.7 script

main.py (Python 2.7 script)

import subprocess import os

def run_python3_script(venv_path, script_path, *args): python3_interpreter = os.path.join(venv_path, "bin", "python3") command = [python3_interpreter, script_path] + list(args) result = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = result.communicate() if result.returncode != 0: raise RuntimeError("Subprocess returned an error: " + err.decode()) return out.strip().decode()

if name == "main": try: venv_path = "/path/to/py3env" # Path to your Python 3.x virtual environment output = run_python3_script(venv_path, "example.py", "10") print("Output from Python 3 script:", output) except Exception as e: print("An error occurred:", str(e))

j-emberton commented 1 month ago

Example of python3 script

example.py (Python 3.x script)

import sys import numpy as np

def py3_function(x): arr = np.array([x]) print(f"Running in Python 3.x with argument {x}") return arr[0] * 2

if name == "main": x = int(sys.argv[1]) result = py3_function(x) print(result)