使用IronPython导入库时,为什么会出现Microsoft.Scripting.SyntaxErrorException:意外令牌','

时间:2018-08-13 15:32:08

标签: c# twitter oauth ironpython

我在VS 2017中使用IronPython v 2.7.8.1。我已经安装了Python27、36和37。我试图在VS中的各种环境之间切换。我尝试将搜索路径添加到这些安装的库中。如果我在解释器中运行python代码,则它将起作用。试图在VS中运行相同的代码将引发:“ Microsoft.Scripting.SyntaxErrorException:意外的标记','”。如果我测试不包含导入的python脚本,它将起作用吗?是否有必须安装python才能运行IronPython的特定方法?这是C#代码:

class CallPython
{        
    public void PatchParameter(string parameter)
    {            
        var FilePath = (@"C:\Users\Pac\Downloads\MvcAuth\MvcAuth\TwurlPy\TwurlPy\SendDirectMsg.py");
        var engine = Python.CreateEngine(); // Extract Python language engine from their grasp
        ICollection<string> searchPaths = engine.GetSearchPaths();

        searchPaths.Add(@"C:\Users\Pac\AppData\Local\Programs\Python\Python37\Lib");
        searchPaths.Add(@"C:\Users\Pac\AppData\Local\Programs\Python\Python37\Lib\site-packages");

        engine.SetSearchPaths(searchPaths);
        var scope = engine.CreateScope(); // Introduce Python namespace 
        (scope)
        var d = new Dictionary<string, object>
        {
            { "text", text},
            { "userID", userID},                
        }; 
        // Add some sample parameters. Notice that there is no need in 
        // specifically setting the object type, interpreter will do that part for us 
        // in the script properly with high probability

        scope.SetVariable("params", d); // This will be the name of the 
        // dictionary in python script, initialized with previously created .NET 
        // Dictionary
        ScriptSource source = engine.CreateScriptSourceFromFile(FilePath); 
        // Load the script
        object result = source.Execute(scope);
        parameter = scope.GetVariable<string>("parameter"); // To get the 
        // finally set variable 'parameter' from the python script
        return;
    }
}

这是Python脚本。如果我注释掉导入语句,则可以使用IronPython,但是我当然需要它们...

import twitter
import requests
import sys

parameter = "test"

def SendDM(text, userID, access_token, access_secret):
    consumer_key = 'YkopsCQjEXccccccccccccccccccZvA9yy'
    consumer_secret = 'TQVCoccccccccccccccccccct7y8VfmE'
    access_token_key = access_token
    access_token_secret = access_secret

    api = twitter.Api(
        consumer_key=consumer_key,
        consumer_secret=consumer_secret,
        access_token_key=access_token_key,
        access_token_secret=access_token_secret)

    send_msg = api.PostDirectMessage(text, user_id=userID)

    print (send_msg)

    return


SendDM(text, userID, access_token, access_secret)

0 个答案:

没有答案