问题
To run python script with F#, got error,
if just run the python script, it do not have error, however put it in F#, it has error or run a very long time
In order to solve error Unicode_escape_decode() takes no arguments
I convert to ASCII and got the same error
Then add code
import codecs
def my_unicode_escape_decode(x):
return x
codecs.unicode_escape_decode = my_unicode_escape_decode
In the script to be run, it runs a very long time, have not ever stoppped since run let scoperesult = script.Execute(scope)
let engine = Python.CreateEngine();
let paths = engine.GetSearchPaths();
paths.Add(@"C:\Python27\Lib\site-packages");
paths.Add(@"C:\Python27");
paths.Add(@"C:\Program Files\IronPython 2.7\Lib");
engine.SetSearchPaths(paths);
let scope = engine.CreateScope();
let MatrixString = ""
let numberofvariables = 2
let numberofvariablestring = numberofvariables.ToString()
let unicodeString = String.Format("import codecs
def my_unicode_escape_decode(x):
return x
codecs.unicode_escape_decode = my_unicode_escape_decode
import sys
from sympy import *
from sympy import Matrix
from sympy.abc import x, y, z, f, a, b
from sympy import *
f = Symbol('f')
x = Symbol('x')
y = Symbol('y')
z = Symbol('z')
varlist = [x,y,z,a,b]
A = Matrix([[1,0],[0,1]])
result = A")
let ascii : Encoding = Encoding.ASCII;
let unicode : Encoding = Encoding.Unicode;
let unicodeBytes = unicode.GetBytes(unicodeString);
let asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);
let asciiString = String(asciiBytes |> ascii.GetChars)
let script = engine.CreateScriptSourceFromString(asciiString)
let scoperesult = script.Execute(scope)
Console.WriteLine(scope.GetVariable("result"))
update:
if run the script with python, it do not have error.
After add async, it has error when run in F#
Additional information: 'ScopeStorage' object has no attribute 'my_unicode_escape_decode'
After run a long time, it has error finally.
An unhandled exception of type 'System.MissingMemberException' occurred in Microsoft.Dynamic.dll
Additional information: 'module' object has no attribute '_getframe'
to see this , need to delete async
[<EntryPoint>]
let main argv =
let engine = Python.CreateEngine();
let paths = engine.GetSearchPaths();
paths.Add(@"C:\Python27\Lib\site-packages");
paths.Add(@"C:\Python27");
paths.Add(@"C:\Program Files\IronPython 2.7\Lib");
engine.SetSearchPaths(paths);
let scope = engine.CreateScope();
let MatrixString = ""
let numberofvariables = 2
let numberofvariablestring = numberofvariables.ToString()
let unicodeString = String.Format("import codecs
def my_unicode_escape_decode(x):
return x
codecs.unicode_escape_decode = my_unicode_escape_decode
import sys
from sympy import *
from sympy import Matrix
from sympy.abc import x, y, z, f, a, b
from sympy import *")
let ascii : Encoding = Encoding.ASCII;
let unicode : Encoding = Encoding.Unicode;
let unicodeBytes = unicode.GetBytes(unicodeString);
let asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);
let asciiString = String(asciiBytes |> ascii.GetChars)
let script = engine.CreateScriptSourceFromString(asciiString)
let scoperesult = script.Execute(scope)
System.Diagnostics.Debug.WriteLine(scope.GetVariable("my_unicode_escape_decode"))
来源:https://stackoverflow.com/questions/22472669/unicode-escape-decode-takes-no-arguments-error-when-run-python-in-f