Open j-emberton opened 1 month ago
make file ideas:
PY27_ENV = py27env PY3_ENV = py3env
PY27 = python2.7 PY3 = python3
MAIN_SCRIPT = main.py EXAMPLE_SCRIPT = example.py
$(PY27_ENV)/bin/activate: requirements-py27.txt virtualenv -p $(PY27) $(PY27_ENV) $(PY27_ENV)/bin/pip install -r requirements-py27.txt
$(PY3_ENV)/bin/activate: requirements-py3.txt $(PY3) -m venv $(PY3_ENV) $(PY3_ENV)/bin/pip install -r requirements-py3.txt
run_py27: $(PY27_ENV)/bin/activate $(PY27_ENV)/bin/python $(MAIN_SCRIPT)
run_py3: $(PY3_ENV)/bin/activate $(PY3_ENV)/bin/python $(EXAMPLE_SCRIPT) 10
clean: rm -rf $(PY27_ENV) $(PY3_ENV)
all: run_py27 run_py3
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))
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)
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: