Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 虽然路径设置正确,但IronPython找不到模块_C#_Python_.net_Ironpython_Python.net - Fatal编程技术网

C# 虽然路径设置正确,但IronPython找不到模块

C# 虽然路径设置正确,但IronPython找不到模块,c#,python,.net,ironpython,python.net,C#,Python,.net,Ironpython,Python.net,在我的C#-应用程序(x64)中,我使用IronPython(通过nuget安装的v2.7.7)来执行python代码。我手动安装了python2.7x64,并在其中安装了包spacy。我可以在C:\Python27x64\Lib\site packages\spacy找到它 在执行C代码时,我从IronPython.Runtime中获得了ImportException“没有名为spacy的模块”。我在网上找到的资源声称,应该通过添加正确的路径来解决这个问题,然而,在我的例子中,这似乎还不够 p

在我的C#-应用程序(x64)中,我使用
IronPython
(通过nuget安装的v2.7.7)来执行python代码。我手动安装了
python2.7x64
,并在其中安装了包
spacy
。我可以在
C:\Python27x64\Lib\site packages\spacy
找到它

在执行C代码时,我从
IronPython.Runtime
中获得了
ImportException
“没有名为spacy的模块”。我在网上找到的资源声称,应该通过添加正确的路径来解决这个问题,然而,在我的例子中,这似乎还不够

python代码(正如您在下面C代码中的字符串变量中所看到的)是

C#代码:

stringcode=@”
导入系统
打印系统版本
sys.path.append(r'C:\Python27x64\Lib')
进口空间”;
var setup=IronPython.Hosting.Python.CreateRuntimeSetup(null);
var runtime=新脚本运行时(设置);
var engine=IronPython.Hosting.Python.GetEngine(运行时);
var path=engine.getSearchPath();
Add(@“C:\Python27x64\Lib”);
引擎。设置搜索路径(路径);
var scope=engine.CreateScope();
var executable_code=engine.CreateScriptSourceFromString(代码,Microsoft.Scripting.SourceCodeKind.Statements);
可执行代码。执行(范围);
输出是

2.7.7(在.NET 4.0.30319.42000(64位)上的IronPython 2.7.7(2.7.7.0))

后跟
IronPython.Runtime.Exceptions.ImportException
:没有名为spacy的模块

因此,无论是在C#中添加路径,还是在python代码中添加路径,似乎都不会起到神奇的作用。我还尝试添加路径“C:\Python27x64\Lib\site packages”,但由于某些原因,这是一个非法路径

更新:我尝试将spacy目录复制到C:\Python27x64\Lib,这似乎有效(它抱怨pathlib),但在我也复制了pathlib之后,它再次失败,出现“ArgumentException:路径不是合法形式”

堆栈跟踪(我发现在本例中完全没有帮助)是:

结果消息: 测试方法Backend.python.Tests.ClassName.Test引发异常: System.ArgumentException:路径不是合法形式


我在这里遗漏了什么?

Spacy是用Cython编写的,只适用于pythonnet,但不适用于IronPython。@denfromufa谢谢,这非常有用。pythonnet有nuget软件包吗?有,但最近没有更新。让我今天尝试更新。同时安装CPython,然后执行pip安装pythonnet@denfromufa我已经尝试了现有的pythonnetnuget包,但它似乎只有32位。您有关于发布时间表的信息吗?您需要从Python.org安装CPython运行时,就像IronPython安装一样。然后使用pip或conda安装Spacy和pythonnet。由于以下问题,所有pythonnet版本的Nuget将不会很快发布:
import sys
print sys.version
sys.path.append(r'C:\Python27x64\Lib')
import spacy
at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength, Boolean expandShortPaths)
at System.IO.Path.GetDirectoryName(String path)
at IronPython.Runtime.Types.DocBuilder.GetXPathDocument(Assembly asm)
at IronPython.Runtime.Types.DocBuilder.GetXmlDoc(Type type, String& summary)
at IronPython.Runtime.Types.DocBuilder.CreateAutoDoc(Type t)
at IronPython.Runtime.Operations.PythonTypeOps.GetDocumentation(Type type)
at IronPython.Runtime.Types.PythonTypeInfo.DocumentationDescriptor.TryGetValue(CodeContext context, Object instance, PythonType owner, Object& value)
at IronPython.Runtime.Binding.MetaPythonType.FastGetBinderHelper.SlotAccessDelegate.Target(CodeContext context, Object self, Object& result)
at IronPython.Runtime.Types.TypeGetBase.RunDelegatesNoOptimize(Object self, CodeContext context)
at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
at IronPython.Compiler.Ast.DynamicGetMemberExpression.GetMemberInstruction.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run1[T0,TRet](T0 arg0)
at IronPython.Runtime.Operations.PythonOps.MakeClass(FunctionCode funcCode, Func`2 body, CodeContext parentContext, String name, Object[] bases, String selfNames)
at Microsoft.Scripting.Interpreter.FuncCallInstruction`7.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run1[T0,TRet](T0 arg0)
at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
at IronPython.Runtime.PythonContext.InitializeModule(String fileName, ModuleContext moduleContext, ScriptCode scriptCode, ModuleOptions options)
at IronPython.Runtime.Importer.LoadModuleFromSource(CodeContext context, String name, String path)
at IronPython.Runtime.Importer.LoadFromDisk(CodeContext context, String name, String fullName, String str)
at IronPython.Runtime.Importer.ImportFromPathHook(CodeContext context, String name, String fullName, List path, Func`5 defaultLoader)
at IronPython.Runtime.Importer.ImportFromPath(CodeContext context, String name, String fullName, List path)
at IronPython.Runtime.Importer.ImportTopAbsolute(CodeContext context, String name)
at IronPython.Runtime.Importer.ImportModule(CodeContext context, Object globals, String modName, Boolean bottom, Int32 level)
at IronPython.Modules.Builtin.__import__(CodeContext context, String name, Object globals, Object locals, Object fromlist, Int32 level)
at Microsoft.Scripting.Interpreter.FuncCallInstruction`7.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run7[T0,T1,T2,T3,T4,T5,T6,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6)
at IronPython.Runtime.Importer.ImportLightThrow(CodeContext context, String fullName, PythonTuple from, Int32 level)
at Microsoft.Scripting.Interpreter.FuncCallInstruction`4.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run1[T0,TRet](T0 arg0)
at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
at IronPython.Runtime.PythonContext.InitializeModule(String fileName, ModuleContext moduleContext, ScriptCode scriptCode, ModuleOptions options)
at IronPython.Runtime.Importer.LoadModuleFromSource(CodeContext context, String name, String path)
at IronPython.Runtime.Importer.LoadFromDisk(CodeContext context, String name, String fullName, String str)
at IronPython.Runtime.Importer.ImportFromPathHook(CodeContext context, String name, String fullName, List path, Func`5 defaultLoader)
at IronPython.Runtime.Importer.ImportFromPath(CodeContext context, String name, String fullName, List path)
at IronPython.Runtime.Importer.ImportTopAbsolute(CodeContext context, String name)
at IronPython.Runtime.Importer.ImportModule(CodeContext context, Object globals, String modName, Boolean bottom, Int32 level)
at IronPython.Modules.Builtin.__import__(CodeContext context, String name, Object globals, Object locals, Object fromlist, Int32 level)
at Microsoft.Scripting.Interpreter.FuncCallInstruction`7.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run7[T0,T1,T2,T3,T4,T5,T6,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6)
at IronPython.Runtime.Importer.ImportLightThrow(CodeContext context, String fullName, PythonTuple from, Int32 level)
at Microsoft.Scripting.Interpreter.FuncCallInstruction`4.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run1[T0,TRet](T0 arg0)
at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
at IronPython.Runtime.PythonContext.InitializeModule(String fileName, ModuleContext moduleContext, ScriptCode scriptCode, ModuleOptions options)
at IronPython.Runtime.Importer.LoadModuleFromSource(CodeContext context, String name, String path)
at IronPython.Runtime.Importer.LoadFromDisk(CodeContext context, String name, String fullName, String str)
at IronPython.Runtime.Importer.ImportFromPathHook(CodeContext context, String name, String fullName, List path, Func`5 defaultLoader)
at IronPython.Runtime.Importer.ImportFromPath(CodeContext context, String name, String fullName, List path)
at IronPython.Runtime.Importer.ImportTopAbsolute(CodeContext context, String name)
at IronPython.Runtime.Importer.ImportModule(CodeContext context, Object globals, String modName, Boolean bottom, Int32 level)
at IronPython.Modules.Builtin.__import__(CodeContext context, String name, Object globals, Object locals, Object fromlist, Int32 level)
at Microsoft.Scripting.Interpreter.FuncCallInstruction`7.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run7[T0,T1,T2,T3,T4,T5,T6,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6)
at IronPython.Runtime.Importer.ImportLightThrow(CodeContext context, String fullName, PythonTuple from, Int32 level)
at Microsoft.Scripting.Interpreter.FuncCallInstruction`4.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope)
at Backend.python.ExternalTransform.Execute(Object input, String code) in C:\Users\user\Documents\Visual Studio 2015\Projects\code.cs:line 20
at Backend.python.ExternalTransform.Test() in C:\Users\user\Documents\Visual Studio 2015\Projects\code.cs:line 32
at Backend.python.Tests.ExternalTransformTests.TestTest() in C:\Users\user\Documents\Visual Studio 2015\Projects\code.cs:line 17