sdeveloper
sdeveloper

Reputation: 149

Python for .NET: ImportError: No module named warnings

I am trying to embed python 2.6 in .NET 4.0.

Following the very minimal documentation in "Python for .NET", I wrote a fairly straightforward code as follows:

 const string pythonModulePath = @"C:\Projects\PythonImport\PythonImport\test.py";
 Environment.SetEnvironmentVariable("PYTHONHOME", Path.GetDirectoryName(python
 ModulePath));

 PythonEngine.Initialize();

 var oldWorkingDirectory = Directory.GetCurrentDirectory();
 var currWorkingDirectory = Path.GetDirectoryName(pythonModulePath);

 Directory.SetCurrentDirectory(currWorkingDirectory);

 var pyPlugin = PythonEngine.ImportModule(Path.GetFileNameWithoutExtension(python
 ModulePath));
 if (pyPlugin == null)
 {
      throw new PythonException();
 }

 Directory.SetCurrentDirectory(oldWorkingDirectory);

Even if test.py is empty I get an import error saying "No module named warnings".

'import site' failed; use -v for traceback

 Unhandled Exception: Python.Runtime.PythonException: ImportError : No module nam
 ed warnings

The reason becomes apparent when you run test.py using "python -v" (verbose mode). Notice that python calls #cleanup[2] warnings.

Why is Python .NET not able to resolve warnings? The module is present in Lib directory.

Any ideas?

Upvotes: 8

Views: 7055

Answers (3)

zola25
zola25

Reputation: 1931

I'm working with the 2.4 version of Python.NET with Python 3.6 and I had similar issues. I didn't have much luck setting environment variables at runtime but I found the following approach worked.

I found Python.NET was picking up all packages in the folders defined in the static PythonEngine.PythonPath variable. If you have other directories set in the PYTHONPATH system variable, it will also include these too.

To include module directories at runtime you can do something like

PythonEngine.PythonPath =  PythonEngine.PythonPath + ";" + moduleDirectory;

using (Py.GIL())
{
    dynamic module = Py.Import("moduleName");
    ...etc
}

Make sure you set the PythonEngine.PythonPath variable before making any calls to the python engine.

Also needed to restart visual studio to see any system variable changes take effect when debugging.

As a side note, I also found that my \Python36\Lib\site-packages folder path needed to be added to PYTHONPATH to pick up anything installed through pip.

Upvotes: 2

realistschuckle
realistschuckle

Reputation: 580

@SilentGhost I don't know if you figured this out, but you need to set your PYTHONPATH environment variable rather than your PYTHONHOME variable. I, too, had the problem, set the environment variable with the ol' Environment.SetVariable("PYTHONPATH", ...) and everything worked out well in the end.

Good luck with the Python.NET.

Upvotes: 2

WombatPM
WombatPM

Reputation: 2619

I think by default the Python Engine does not set the paths you need automatically.

http://www.voidspace.org.uk/ironpython/custom_executable.shtml has an example for embedding ironpython. Looks like you are missing something like

PythonEngine engine = new PythonEngine();
engine.AddToPath(Path.GetDirectoryName(Application.ExecutablePath));
engine.AddToPath(MyPathToStdLib);

Unless I know where and if Ironpython is installed, I prefer to find all of the standard modules I need, compile them to a IPyStdLibDLL and then do th following from my code

import clr
clr.addReference("IPyStdLib")
import site

Upvotes: 2

Related Questions