2017-07-31 79 views
0

我有一个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

你的文件名有空间改变它强调,空间不是在导入文件名允许的。

Python代码更改为如下

import sys 
sys.path.append(r"C:\Users\gpathak\Documents\new scripts\Automation Scripts (Packet Range)\Document_creator") 
from document creator track to train import * 

改变你的document creator track to train.py文件名document_creator_track_to_train.py,并在代码中使用相同的名称,这将解决这个问题。

+0

它现在有这个错误... IronPython.Runtime.Exceptions.ImportException:'No module named document_creator_track_to_train' –

+0

我得到这个错误,请帮助。 IronPython.Runtime.Exceptions.ImportException:'No module named docx' –

+0

如果不安装请安装'docx'软件包,对于'document_creator_track_to_train'确保文件名'document_creator_track_to_train.py'存在于路径 –

相关问题