@@ -422,7 +422,30 @@ public static PyObject ModuleFromString(string name, string code)
422
422
return new PyObject ( m ) ;
423
423
}
424
424
425
- public static PyObject Compile ( string code , string filename = "" , RunFlagType mode = RunFlagType . File )
425
+ /// <summary>
426
+ /// Load module from a file.
427
+ /// </summary>
428
+ /// <param name="moduleName">Module name</param>
429
+ /// <param name="filePath">Path to the file.</param>
430
+ /// <returns>Python module object.</returns>
431
+ /// <remarks>
432
+ /// <para>In most cases the module name should be the file's base name
433
+ /// (Path.GetFileNameWithOutExtension()). However, if you ask for a
434
+ /// module that has already been loaded you will get a reference
435
+ /// to the existing module. If you want to load code from the file
436
+ /// as different modules then create unique module names for each.</para>
437
+ /// </remarks>
438
+ public static PyObject ModuleFromFile ( string moduleName , string filePath )
439
+ {
440
+ string code = File . ReadAllText ( filePath ) ;
441
+ IntPtr c = Runtime . Py_CompileString ( code , filePath , ( IntPtr ) 257 ) ;
442
+ Runtime . CheckExceptionOccurred ( ) ;
443
+ IntPtr m = Runtime . PyImport_ExecCodeModule ( moduleName , c ) ;
444
+ Runtime . CheckExceptionOccurred ( ) ;
445
+ return new PyObject ( m ) ;
446
+ }
447
+
448
+ public static PyObject Compile ( string code , string filename = "" , RunFlagType mode = RunFlagType . File )
426
449
{
427
450
var flag = ( IntPtr ) mode ;
428
451
IntPtr ptr = Runtime . Py_CompileString ( code , filename , flag ) ;
0 commit comments