0
votes

I just installed Iron Python. When I open it up in terminal it shows installed. When I look for the .dll in Visual Studio I can find them an add them to the project. I was following along a tutorial on microsoft site. Everything was going fine until I got to the very end with...

Save the file and press CTRL+F5 to build and run the application.

Which is when I got the error.

Unhandled Exception: System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Program >Files (x86)\IronPython 2.7.9 for .NET 4.0\Lib'. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.Directory.SetCurrentDirectory(String path) at DynamicIronPythonSample.Program.Main() in >C:\Users\scott\source\repos\DynamicIronPythonSample\Program.cs:line 17

This is what I am referencing Walkthrough: Creating and Using Dynamic Objects (C# and Visual Basic)

How can I fix this issue? Line 17 is this line in Program.cs

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

Entire script

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("-------------------");
            }
        }
    }
}

IronPython 2.7.9 (2.7.9.0) on .NET 4.0.30319.42000 (64-bit) Type "help", "copyright", "credits" or "license" for more information.

.NET v4.0.30319

1
Don't use string concat for paths. Use Path.Combine. You can also use Directory.Exists to make sure your path is right. At a glance it looks like you're in the 32-bit program files and you have 64-bit installed. - Zer0
is your project folder "IronPython 2.7.9 for .NET 4.0"? - M A Salman
@Supercool DynamicIronPythonSample - Milliorn
Check if folder IronPython2. 7.9 for. Net 4.0 exists - M A Salman
@Zer0 string path1 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); string path2 = @"\IronPython 2.7.9 for .NET 4.0\Lib"; string path = Path.Combine(path1, path2); System.IO.Directory.SetCurrentDirectory(path); Like this? - Milliorn

1 Answers

0
votes

Finally got this to work. Microsoft really jacked up that tutorial. Not only was the path wrong, but other errors at runtime with IronPython. This is what I ended up with. Thank you to those that offered help.

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("-------------------");
            }
        }
    }
}