Необработанное исключение с IronPython - PullRequest
0 голосов
/ 23 апреля 2020

Я только что установил Iron Python. Когда я открываю его в терминале, он показывает установленный. Когда я ищу .dll в Visual Studio, я могу найти их и добавить их в проект. Я следовал учебнику на сайте Microsoft. Все шло хорошо, пока я не дошел до самого конца ...

Сохраните файл и нажмите CTRL + F5, чтобы создать и запустить приложение.

Что когда я получил ошибку.

Необработанное исключение: System.IO.DirectoryNotFoundException: Не удалось найти часть пути 'C: \ Program> Files (x86) \ Iron Python 2.7 .9 за. NET 4.0 \ Lib '. в System.IO .__ Error.WinIOError (Int32 errorCode, String MaybeFullPath) в System.IO.Directory.SetCurrentDirectory (String path) в DynamicIronPythonSample.Program.Main () в> C: \ Users \ scott \ source \ repos \ DynamicIronPythonSample \ Program.cs: строка 17

Это то, на что я ссылаюсь Пошаговое руководство. Создание и использование объектов Dynami c (C# и Visual Basi c)

Как я могу исправить эту проблему? Строка 17 - это строка в Program.cs

            System.IO.Directory.SetCurrentDirectory(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + @"\IronPython 2.7.9 for .NET 4.0\Lib");

Весь скрипт

using Microsoft.Scripting.Hosting;
using IronPython.Hosting;
using System;
using System.Linq;

namespace DynamicIronPythonSample
{
    class Program
    {
        static void Main()
        {
            // Set the current directory to the IronPython libraries.
            System.IO.Directory.SetCurrentDirectory(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + @"\IronPython 2.7.9 for .NET 4.0\Lib");

            // Create an instance of the random.py IronPython library.
            Console.WriteLine("Loading random.py");
            ScriptRuntime py = Python.CreateRuntime();
            dynamic random = py.UseFile("random.py");
            Console.WriteLine("random.py loaded.");

            //  Initialize an enumerable set of integers
            int[] items = Enumerable.Range(1, 7).ToArray();

            //  Randomly shuffle the array of integers by using IronPython.
            for (int i = 0; i < 5; i++)
            {
                random.shuffle(items);

                foreach (int item in items)
                {
                    Console.WriteLine(item);
                }
                Console.WriteLine("-------------------");
            }
        }
    }
}

Железо Python 2.7.9 (2.7.9.0) вкл. NET 4.0 .30319.42000 (64-разрядная версия) Для получения дополнительной информации введите «помощь», «авторское право», «кредиты» или «лицензия».

. NET v4.0.30319

1 Ответ

0 голосов
/ 23 апреля 2020

Наконец-то все заработало. Microsoft действительно испортила этот урок. Не только путь был неправильным, но и другие ошибки во время выполнения с Iron Python. Это то, что я закончил. Спасибо тем, кто предложил помощь.

using Microsoft.Scripting.Hosting;
using IronPython.Hosting;
using System;
using System.Linq;
using System.IO;

namespace DynamicIronPythonSample
{
    class Program
    {
        static void Main()
        {
            // Set the current directory to the IronPython libraries.
            string path1 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
            string path2 = @"C:\Program Files\IronPython 2.7";
            string path = Path.Combine(path1, path2);
            System.IO.Directory.SetCurrentDirectory(path);

            // Create an instance of the random.py IronPython library.
            Console.WriteLine("Loading random.py");
            ScriptRuntime py = Python.CreateRuntime();
            dynamic random = py.UseFile("random.py");
            Console.WriteLine("random.py loaded.");

            //  Initialize an enumerable set of integers
            int[] items = Enumerable.Range(1, 7).ToArray();

            //  Randomly shuffle the array of integers by using IronPython.
            for (int i = 0; i < 5; i++)
            {
                //  Random doesn't work, used C# instead.
                //random.shuffle(items);
                int[] shuffled = items.OrderBy(n => Guid.NewGuid()).ToArray();


                foreach (int item in shuffled)
                {
                    Console.WriteLine(item);
                }
                Console.WriteLine("-------------------");
            }
        }
    }
}
...