是否可以使用允许相对导入的 IronPython 执行 Python 2 脚本?如果不是,是否有技术原因?
我收到此错误:
Error: Attempted relative import in non-package
Traceback (most recent call last):
File "G:\test\test.py", line 1, in <module>
ValueError: Attempted relative import in non-package
这是意料之中的,因为通常需要使用python -m test.test
. 有没有办法-m
使用 IronPython 作为模块复制或执行?
代码信息
执行 Python 脚本的 C# 代码:
var scriptEngine = Python.CreateEngine();
var scope = scriptEngine.CreateScope();
var scriptSource = scriptEngine.CreateScriptSourceFromFile(@"G:\test\test.py");
scriptSource.execute(scope);
测试.py:
from . import utils
utils.helloWorld()
文件夹结构:
G:\
|- test
|- __init__.py
|- utils.py
|- test.py