我有一个 C#
应用程序,我希望通过该应用程序运行python脚本。 python脚本的作用是获取doc文件并创建其他3个doc文件。
我从这段代码中收到错误:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Project_new
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Button Welcome = new Button();
Welcome.Click += new RoutedEventHandler(Welcome_Click);
ComboBoxItem cbi = (ComboBoxItem)(sender as ComboBox).SelectedItem;
}
private void Welcome_Click(object sender, RoutedEventArgs e)
{
var ComboItem = new1.SelectedValue;// Item;// (ComboBoxItem)cmbDeviceDefinitionId.SelectedItem;
string s = ((System.Windows.FrameworkElement)ComboItem).Name;
//string name = ComboItem. Name;
if (new1.SelectedValue == cbi1)
{
Window1 win1 = new Window1();
win1.Show();
this.Close();
// python script to be loaded
var ipy = Python.CreateRuntime();
string importScript = "import sys" + Environment.NewLine + "sys.path.append(r\"{0}\")" + Environment.NewLine + "from {1} import *";
string fullPath = @"C:\Users\gpathak\Documents\new scripts\Automation Scripts (Packet Range)\Document_creator\document creator track to train.py";
var engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
// import the module//
string scrpitStr = string.Format(importScript,
System.IO.Path.GetDirectoryName(fullPath),
System.IO.Path.GetFileNameWithoutExtension(fullPath));
var importSrc = engine.CreateScriptSourceFromString(scrpitStr, Microsoft.Scripting.SourceCodeKind.File);
importSrc.Execute(scope);
string expr = "functionOfMyModule()";
var result = engine.Execute(expr, scope);
}
else if (new1.SelectedValue == cbi2)
{
Window2 win2 = new Window2();
win2.Show();
this.Close();
}
else
{
Window3 win3 = new Window3();
win3.Show();
this.Close();
}
}
}
}
我收到了这个错误:
Microsoft.Scripting.SyntaxErrorException occurred
HResult=0x80131500
Message=unexpected token 'creator'
Source=<Cannot evaluate the exception source>
StackTrace:`enter code here
at Microsoft.Scripting.ErrorSink.Add(SourceUnit source, String message, SourceSpan span, Int32 errorCode, Severity severity)
at IronPython.Compiler.Parser.ReportSyntaxError(Token t, SourceSpan span, Int32 errorCode, Boolean allowIncomplete)
at IronPython.Compiler.Parser.ParseFromImportStmt()
at IronPython.Compiler.Parser.ParseSmallStmt()
at IronPython.Compiler.Parser.ParseSimpleStmt()
at IronPython.Compiler.Parser.ParseStmt()
at IronPython.Compiler.Parser.ParseFileWorker(Boolean makeModule, Boolean returnValue)
at IronPython.Compiler.Parser.ParseFile(Boolean makeModule, Boolean returnValue)
at IronPython.Runtime.PythonContext.ParseAndBindAst(CompilerContext context)
at IronPython.Runtime.PythonContext.CompilePythonCode(Nullable`1 compilationMode, SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
at IronPython.Runtime.PythonContext.CompileSourceCode(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
at Microsoft.Scripting.SourceUnit.Compile(CompilerOptions options, ErrorSink errorSink)
at Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink errorSink)
at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope)
at Project_new.MainWindow.Welcome_Click(Object sender, RoutedEventArgs e) in C:\Users\gpathak\Documents\Visual Studio 2017\Projects\Project new\Project new\MainWindow.xaml.cs:line 65
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at System.Windows.Application.Run()
at Project_new.App.Main()
这是我得到的错误。我想知道为什么会出现这个错误。我对python脚本没有任何问题。如果我在python
IDE上运行它,它运行正常,但是当我尝试运行C#
时,这个错误就会出现。
答案 0 :(得分:0)
您的文件名将空格更改为下划线,导入文件名中不允许使用空格。
您的python代码更改为如下
document creator track to train.py
将您的document_creator_track_to_train.py
文件名更改为Callback URL: http://example.com/
Callback URL Locked: No
Sign in with Twitter: Yes
,并在代码中使用相同的名称来解决问题。