Tag Archives: C# call Python error

C# call Python error no module named OS [How to Solve]

C # calling Python

Environment: Windows 8.1, has installed Python 2.7 (C: Python 27), has configured environment variables. Has installed vs2013, vs2017

1. Install ironpython

Download address http://ironpython.net/download/ I downloaded ironpython-2.7.4.3261515540.msi

The installation directory is: F:// program files (x86) – ironpython 2.7

2. Open vs2013 and create a new console program csharpcallpython

3. Add reference

Add a reference to csharpcallpython, ironpython.dll and microsoft.scripting.dll under F: (program files (x86)  ironpython 2.7  platforms  net40

4. Add the PY script file python1.py in csharpcallpython, and select to copy it locally. The code is as follows

def add(num1,num2):
    return num1+num2;

Add code in csharpcallpython

5

ScriptRuntime pyRuntime = Python.CreateRuntime(); //Create a runtime environment

        dynamic obj = pyRuntime.UseFile("Python1.py"); // call a Python file

        int a = 1, b = 2;

        int sum = obj.add(a,b); //call the sum function in the Python file
        Console.Write("Sum:");
        Console.WriteLine(sum);

The running result is sum = 3

This is the end of the call. Thank you. If this is possible, then it is impossible. Please see below

How to reference the third party Library

How to use the Library under OS?Let’s continue to add the PY script file python2.py

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import shutil


def ClearDirect(path):#

    for root, dirs, files in os.walk(path,topdown=False):
        print 'root',root
        for dir in dirs:

            print 'delete:',os.path.join(root, dir)
            #os.removedirs(os.path.join(root, dir))
            shutil.rmtree(os.path.join(root, dir))


    return 'OK'

The purpose of the script is to delete the subdirectory of the incoming directory and add C code

     ScriptRuntime pyRuntime2 = Python.CreateRuntime(); //Create a runtime environment
        dynamic obj2 = pyRuntime.UseFile("Python2.py"); // call a Python file
        obj2.ClearDirect(@"D:\Data\KkImage\2017"); //
        Console.Write("result");
        Console.WriteLine(sum); 

After running, I’m very sorry for the error

Unhandled exception of type "IronPython.Runtime.Exceptions.ImportException" in Microsoft.Dynamic.dll 

Additional information: No module named os  

The OS module was not found

7. How can I use the OS library

Add in python2.py

import sys
sys.path.append("F:\Program Files (x86)\IronPython 2.7\lib")

Code becomes

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
sys.path.append("F:\Program Files (x86)\IronPython 2.7\lib")
import os
import shutil


def ClearDirect(path):#delete

    for root, dirs, files in os.walk(path,topdown=False):
        print 'root',root
        for dir in dirs:

            print 'delete:',os.path.join(root, dir)
            #os.removedirs(os.path.join(root, dir))
            shutil.rmtree(os.path.join(root, dir))


    return 'OK'

Run again, found subdirectory deleted successfully. Congratulations

8. In the Python code above, add

  sys.path.append("F:\Program Files (x86)\IronPython 2.7\lib")

When the program is released, the path on the server will change. If there are dozens of Python files, do you have to modify the path one by one?It’s horrible to think about it

So, you can use the following method. (first, remove the sentence “sys. Path. Append” in the python file (F: program files (x86) – ironpython 2.7 – lib “)

ScriptRuntime pyRuntime3 = Python.CreateRuntime(); //Create a runtime environment
            var engine = pyRuntime3.GetEngine("python");
            var pyScope = engine.CreateScope();
            var paths = engine.GetSearchPaths();
            paths.Add(@"F:\Program Files (x86)\IronPython 2.7\lib");
            engine.SetSearchPaths(paths);
            dynamic obj3 = pyRuntime3.UseFile("Python2.py"); //Calling a Python file
            obj3.ClearDirect(@"D:\Data\KkImage\2017"); //

If you specify the location of the library in the engine, you don’t need to specify it in each Python file