简体   繁体   English

Python.Net:如何在包中执行模块?

[英]Python.Net: how to execute modules in packages?

I'm not a Python programmer so apologies if I don't get some of the terminology right (pacakages, modules)!我不是 Python 程序员,如果我没有正确理解某些术语(包,模块),请道歉!

I have a folder structure that looks something like this:我有一个看起来像这样的文件夹结构:

C:\Test\System\
C:\Test\System\intercepts\
C:\Test\System\intercepts\utils1\
C:\Test\System\intercepts\utils2\

The last three folders each contain an empty __init__.py folder, while the latter two folders (\utils1, \utils2) contain numerous.py modules.最后三个文件夹分别包含一个空的__init__.py文件夹,而后两个文件夹(\utils1、\utils2)包含许多.py 模块。 For the purposes of my question I'm trying to execute a function within a module called "general.py" that resides in the \utils1 folder.出于我的问题的目的,我试图在位于 \utils1 文件夹中的名为“general.py”的模块中执行 function。

The first folder (C:\Test\System) contains a file called "entry.py", which imports the.py modules from all those sub-folders:第一个文件夹(C:\Test\System)包含一个名为“entry.py”的文件,它从所有这些子文件夹中导入 .py 模块:

from intercepts.utils1 import general
from intercepts.utils1 import foobar
from intercepts.utils2 import ...
..etc..

And here is the C# code that executes the above module then attempts to call a function called "startup" in a module called "general.py" in the \utils1 folder:这是执行上述模块的 C# 代码,然后尝试在 \utils1 文件夹中名为“general.py”的模块中调用名为“startup”的 function:

const string EntryModule = @"C:\Test\System\entry.py";

using (Py.GIL())
{
    using (var scope = Py.CreateScope())
    {
        var code = File.ReadAllText(EntryModule);
        var scriptCompiled = PythonEngine.Compile(code, EntryModule);
        scope.Execute(scriptCompiled);

        dynamic func = scope.Get("general.startup");
        func();
    }
}

However I get a PythonException on the scope.Execute(...) line, with the following message:但是,我在scope.Execute(...)行上收到PythonException ,并显示以下消息:

No module named 'intercepts'
File "C:\Test\System\entry.py", line 1, in <module>
   from intercepts.utils1 import general

I'm able to do the equivalent of this using IronPython (Python 2.7), so I assume I'm doing something wrong with Python.Net (rather than changes to how packages work in Python 3.x).我可以使用 IronPython (Python 2.7) 做同样的事情,所以我假设我在 Python.Net 上做错了(而不是更改包在 Python 3.x 中的工作方式)。

I'm using the pythonnet 3.0.0 NuGet package by the way.顺便说一句,我正在使用pythonnet 3.0.0 NuGet package。

Edit I've tried importing my "entry.py" module as follows:编辑我尝试按如下方式导入我的“entry.py”模块:

dynamic os = Py.Import("os"); 
dynamic sys = Py.Import("sys");                   
sys.path.append(os.path.dirname(EntryModule)); 
Py.Import(Path.GetFileNameWithoutExtension(EntryModule));

It now appears to get a little further, however there's a new problem:它现在似乎更进一步,但是有一个新问题:

In the "entry.py" module you can see that it first imports a module called "general", then a module called "foobar".在“entry.py”模块中,您可以看到它首先导入了一个名为“general”的模块,然后是一个名为“foobar”的模块。 "foobar.py" contains the line import general . “foobar.py”包含行import general When I run my C#, the stack trace is now as follows:当我运行 C# 时,堆栈跟踪现在如下:

No module named 'general'
File "C:\Test\System\intercepts\utils1\foobar.py", line 1, in <module>
import general
  File "C:\Test\System\entry.py", line 2, in <module>
from intercepts.utils1 import foobar

Why can't the second imported module ("foobar") "see" the module that was imported immediately before it ("general")?为什么第二个导入的模块(“foobar”)不能“看到”在它之前导入的模块(“general”)? Am I even barking up the right tree by using Py.Import() to solve my original issue?我什至是通过使用Py.Import()来解决我原来的问题吗?

This turned out to be a change in how Python 3 handles imports, compared to 2, and nothing to do with Python.Net.事实证明,与 2 相比,Python 3 处理导入的方式发生了变化,与 Python.Net 无关。

In my "foobar.py" module I had to change import general to from. import general在我的“foobar.py”模块中,我必须将import general更改为from. import general from. import general . from. import general The issue is explained here but I've included the pertinent section below:此处解释了该问题,但我在下面包含了相关部分:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM