Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python_version_interface #5

Open
j-emberton opened this issue Jul 19, 2024 · 3 comments · May be fixed by #6
Open

python_version_interface #5

j-emberton opened this issue Jul 19, 2024 · 3 comments · May be fixed by #6
Assignees

Comments

@j-emberton
Copy link
Member

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:

  • environments
    • python 2 and 3 installed on machine, with environments with all dependencies loaded
    • environment manager must be python 2.7 compatible e.g. virtualenv?
    • requirements.txt for each env
    • make file to create env with dependencies after code clone
  • subprocess function
    • arguments
      • path to function/class
      • list of arguments
      • environment to run python 3 function/class in
    • returns
      • function output
    • error handling
      • must detect if subprocess failed to run and alert user
@j-emberton
Copy link
Member Author

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
Copy link
Member Author

#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
Copy link
Member Author

#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)

@j-emberton j-emberton linked a pull request Jul 19, 2024 that will close this issue
@j-emberton j-emberton linked a pull request Jul 19, 2024 that will close this issue
@j-emberton j-emberton self-assigned this Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant