Python for .NET:导入错误:没有名为警告的模块

发布于 2024-10-12 12:46:26 字数 1086 浏览 2 评论 0原文

我正在尝试将 python 2.6 嵌入到 .NET 4.0 中。

按照“Python for .NET”中非常简单的文档,我编写了一个相当简单的代码,如下所示:

 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);

即使 test.py 为空,我也会收到一条导入错误,提示“没有名为警告的模块”。

'import site' failed; use -v for traceback

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

当您使用“python -v”(详细模式)运行 test.py 时,原因就显而易见了。请注意,python 调用 #cleanup[2] 警告。

为什么 Python .NET 无法解决警告?该模块位于 Lib 目录中。

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南街九尾狐 2024-10-19 12:46:26

我认为默认情况下 Python 引擎不会自动设置您需要的路径。

http://www.voidspace.org.uk/ironpython/custom_executable.shtml有一个嵌入ironpython 的示例。看起来你缺少类似的东西

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

除非我知道 Ironpython 安装在哪里以及是否安装,否则我更喜欢找到我需要的所有标准模块,将它们编译为 IPyStdLibDLL,然后从我的代码中执行以下操作

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

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
梦过后 2024-10-19 12:46:26

@SilentGhost我不知道你是否明白这一点,但你需要设置 PYTHONPATH 环境变量而不是 PYTHONHOME 变量。我也遇到了问题,使用 ol'Environment.SetVariable("PYTHONPATH", ...) 设置环境变量,最终一切顺利。

祝 Python.NET 好运。

@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.

一梦等七年七年为一梦 2024-10-19 12:46:26

我正在使用 Python.NET 2.4 版本和 Python 3.6,并且遇到了类似的问题。我在运行时设置环境变量的运气不佳,但我发现以下方法有效。

我发现 Python.NET 正在获取静态 PythonEngine.PythonPath 变量中定义的文件夹中的所有包。如果您在 PYTHONPATH 系统变量中设置了其他目录,它也将包含这些目录。

要在运行时包含模块目录,您可以执行类似的操作

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

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

,确保在调用 python 引擎之前设置 PythonEngine.PythonPath 变量。

调试时还需要重新启动 Visual Studio 才能看到任何系统变量更改生效。

作为旁注,我还发现我的 \Python36\Lib\site-packages 文件夹路径需要添加到 PYTHONPATH 才能获取通过 pip 安装的任何内容。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文