9

I'm using Visual Studio 2010. I have an IronPython console project and a C# console project. This IronPython script works fine when I run it by itself:

import nltk

def Simple():
    baconIpsumFile = open('baconipsum.txt', 'r')
    baconIpsumCorpus = baconIpsumFile.read()

    tokens = nltk.word_tokenize(baconIpsumCorpus)
    text = nltk.Text(tokens)
    print text

Here is the C# console program, which does not work fine:

using IronPython.Hosting;

namespace IronNLTK.CSharp.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            var ipy = Python.CreateRuntime();
            dynamic test = ipy.UseFile("C:\\Path\\To\\Program.py");
            test.Simple();
        }
    }
}

I get an ImportException: No module named nltk. What am I missing?

Matthew Groves
  • 25,181
  • 9
  • 71
  • 121

2 Answers2

3

sounds like you need to update sys.path to point to wherever NLTK lives.

check this out: Importing external module in IronPython

Community
  • 1
  • 1
Oren Mazor
  • 4,437
  • 2
  • 29
  • 28
  • if you mean something like sys.path.append(r"c:\Program Files (x86)\IronPython\Lib") that doesn't work either – Matthew Groves Sep 16 '11 at 16:11
  • 1
    Of course that does not work. Backslash is the string escape character. If you want to put a path in a Python string, then you need to do C:/Progr... or C:\\Progr... – Michael Dillon Oct 01 '11 at 03:46
  • 1
    In addition, it is not unusual for portable software to have a problem with paths that contain spaces in the name. Have you tried installing another instance of IronPython in C:\IronPython so that you don't have any directories with space in the names? – Michael Dillon Oct 01 '11 at 03:48
  • Yes, sorry, I did use escaped backslashes, I'll look into the spaces in the path name though... – Matthew Groves Oct 04 '11 at 23:30
2

Awesome news, Visual Studio 2017 is embedded with Anaconda’s Python distribution which has NTLK and other machine learning packages.

reti
  • 67
  • 2