def pythonnet_init(): """correctly sets-up pythonnet for the typetree generator""" # prepare correct runtime from clr_loader import get_coreclr from pythonnet import set_runtime rt = get_coreclr( os.path.join(TYPETREE_GENERATOR_PATH, "TypeTreeGenerator.runtimeconfig.json")) set_runtime(rt)
def pytest_configure(config): global bin_path if "clr" in sys.modules: # Already loaded (e.g. by the C# test runner), skip build import clr clr.AddReference("Python.Test") return runtime_opt = config.getoption("runtime") test_proj_path = os.path.join(cwd, "..", "src", "testing") if runtime_opt not in ["netcore", "netfx", "mono", "default"]: raise RuntimeError(f"Invalid runtime: {runtime_opt}") bin_path = mkdtemp() # tmpdir_factory.mktemp(f"pythonnet-{runtime_opt}") fw = "net6.0" if runtime_opt == "netcore" else "netstandard2.0" check_call(["dotnet", "publish", "-f", fw, "-o", bin_path, test_proj_path]) sys.path.append(bin_path) if runtime_opt == "default": pass elif runtime_opt == "netfx": from clr_loader import get_netfx runtime = get_netfx() set_runtime(runtime) elif runtime_opt == "mono": from clr_loader import get_mono runtime = get_mono() set_runtime(runtime) elif runtime_opt == "netcore": from clr_loader import get_coreclr rt_config_path = os.path.join(bin_path, "Python.Test.runtimeconfig.json") runtime = get_coreclr(rt_config_path) set_runtime(runtime) import clr clr.AddReference("Python.Test") soft_mode = False try: os.environ['PYTHONNET_SHUTDOWN_MODE'] == 'Soft' except: pass if config.getoption("--runtime") == "netcore" or soft_mode\ : collect_ignore.append("domain_tests/test_domain_reload.py") else: domain_tests_dir = os.path.join(os.path.dirname(__file__), "domain_tests") bin_path = os.path.join(domain_tests_dir, "bin") build_cmd = ["dotnet", "build", domain_tests_dir, "-o", bin_path] is_64bits = sys.maxsize > 2**32 if not is_64bits: build_cmd += ["/p:Prefer32Bit=True"] check_call(build_cmd)
# This Python script can be loaded in a notebook (ipynb file) # in order to reference QuantConnect assemblies # # Usage: # %run "start.py" import clr_loader import os from pythonnet import set_runtime # The runtimeconfig.json is stored alongside start.py, but start.py may be a # symlink and the directory start.py is stored in is not necessarily the # current working directory. We therefore construct the absolute path to the # start.py file, and find the runtimeconfig.json relative to that. set_runtime( clr_loader.get_coreclr( os.path.join(os.path.dirname(os.path.realpath(__file__)), "QuantConnect.Lean.Launcher.runtimeconfig.json"))) from clr import AddReference AddReference("System") AddReference("QuantConnect.Algorithm") AddReference("QuantConnect.Api") AddReference("QuantConnect.Common") AddReference("QuantConnect.Configuration") AddReference("QuantConnect.Research") AddReference("QuantConnect.Indicators") from System import * from QuantConnect import * from QuantConnect.Algorithm import * from QuantConnect.Api import *
from clr_loader import get_coreclr from pythonnet import set_runtime import sys import clr rt = get_coreclr(r"/usr/local/bin/REnv.runtimeconfig.json") set_runtime(rt) sys.path.append(r"/usr/local/bin/") sys.path.append(r"/etc/r_env/library/ggplot/lib/assembly/") clr.AddReference(r"REnv.dll") # test R# engine from REnv import ExportsClr renv = ExportsClr() print(renv.__class__.__name__) result = renv.HelloWorld() print(result)