Initializes the Python runtime, including redirecting sys.

 

   

Syntax
 

[C#]
bool AutoInitializeRuntime(PythonScope scope, string venvPath)

 

Throws Exceptions ArgumentNullException: Thrown when scope is null.
Throws Exceptions ArgumentException: Thrown when the environment of scope is different from the Python environment of this PythonRuntime.
Throws Exceptions ObjectDisposedException: Thrown when this PythonRuntime has been disposed of.

 

   

Params
 
Name Description
scope The Python scope for running initialization code.
venvPath The directory path for a Python virtual environment if not null/empty. It is ignored if is not . It should be null/empty for all calls except (possibly) for the first call. Please see for details.
return Whether initialization is performed. Initialization is performed if is Uninitialized or AutoInitialized.

 

   

Notes
 

Initializes the Python runtime, including redirecting sys.stdout and sys.stderr< and setting up the virtual environment.

If initialization is performed, PythonEnv.RuntimeIsInitialized is set to RuntimeInitializationState.AutoInitialized.

 

   

Example
 

To use a virtual environment, you need to initialize the Python engine with PythonOptions.SetNoSiteFlag true and to provide the path to. the directory of the virtual environment. This example shows how you might do this.

 

string venvPath = @"C:\MyVenv"; var env = PythonEnvironment.Current; var options = new PythonOptions { SetNoSiteFlag = true }; options.Initialize(env, null); using var rt = env.GetRuntime(); using var scope = rt.CreateScope(); rt.StdOutWriter = Console.Out; rt.StdErrWriter = Console.Error; rt.AutoInitializeRuntime(scope, venvPath); // ... do work