Click here to Skip to main content
15,886,780 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all,
I am trying to make calls to methods in a python script from within my C# form app. I have installed IronPython 2.7, and I am using visual studio 2010. (FYI, I tried a simple version of this with no imports from my script, and it worked, I was able to make calls to the method)
The application compiles just fine, however when I try to run it, I get a IronPyhton runtime error like this: No module named optparse. This is a module I am importing from my python script file. It appears that there is a missing python dll somewhere, or the wrong dlls, because if I comment out the import optparse line, then the error moves to the next module which is the "os" module. If I comment out that line, the error moves to the next import, and so on,,,, I even tried re-arranging the order of the imported modules.
I have added references in my VS project to the ipy, ironpython, ironpython.module, and several Microsoft suggested DLLs, like Microsoft.Dynamic.
To no avail. Could there be a path issue? Could I be referencing the wrong DLLs?
I found these DLLs under the C:\WINDOWS\Microsoft.NET\assembly\GAC_MSIL
so this is where they are referenced to.

Anybody else have a similar issue like this before?

Here is what I am including in my form file:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IronPython.Hosting;
using IronPython.Modules;
using IronPython.Runtime;
using System.IO;


Here is the little snippet of code where I implement this,
public Form1()
{
    InitializeComponent();

    var ipy = Python.CreateRuntime();
    try
    {
        dynamic logfile = ipy.UseFile("logparser.py");
    }
    catch (FileNotFoundException)<pre>
    {
        MessageBox.Show("unable to open logparser.py");

    }
 }

And here are the modules I am importing in my script:
import System
import os
from datetime import datetime, timedelta
import sys
import re
import thread 
import time
from optparse import OptionParser
Posted
Updated 14-Mar-11 8:19am
v2
Comments
Wendelius 14-Mar-11 14:20pm    
Edited formatting

1 solution

I had a similar issue and found a workaround by putting appending the directory with the imported module to the path.

i.e.
sys.path.append(r'c:\python\mylibrary')


I had to do this even if the directory was the same directory that the main script file lived in.

I also found that the issue went away if I copied the the file with the imported module to the working directory of the C# program that is loading and running the script.

I don't like either solution particularly well, however, and am still trying to find a better one.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900