How do I get to the IronPython Lib directory from the embedded engine?

I am implementing IronPython in a C # program. I understand that I need to call ScriptEngine.SetSearchPaths with a list of search paths so that my Python code can import modules. Specifically, I have to tell him where the standard library modules are, which in my case are here:

C:\Program Files (x86)\IronPython 2.0.2\Lib

      

My question is, how can this path be found programmatically?

Obviously this can be done because the IronPython Console knows about it:

>>> import sys
>>> sys.path
['C:\\Windows\\system32', 'C:\\Program Files (x86)\\IronPython 2.0.2\\Lib', 'C:\\Program Files (x86)\\IronPython 2.0.2', 'C:\\Program Files (x86)\\IronPython 2.0.2\\lib\\site-packages'] 

      

+2


source to share


4 answers


A possible solution would be to send a copy of the Lib folder with your application. I believe both the IronPython license ( Microsoft Public License (Ms-PL) ) and the Python license ( Python License ) will permit this.

Benefits for this plan:

  • Allows the app to find the Lib folder.

  • Installation will be easier as less than one installer will work with it.

  • The overall installer size will be smaller.

  • If the IronPython assemblies that the application uses are copied locally, then it makes sense to copy the Lib too, so they won't sync, as they might have done if the Lib from the program files were used.

Licenses do not appear to impose any onerous obligations. Ms-PL requires you to include a complete copy of Ms-PL with your distributions, but that doesn't really matter.

Edit: here's the code

The following is what I did.

the lib folder is copied to the AfterBuild target of my .csproj file. I couldn't figure out how to use the built-in MSBuild copy command because everything MSBuild-related is as painful as possible, so I used robocopy instead. (robocopy is a replacement for xcopy, you can use xcopy instead).

<Target Name="AfterBuild">
  <Exec Command="robocopy $(ProjectDir)..\3rdParty\IronPython\IronPython2.0.2\Lib $(OutputPath)IronPython\Lib /E /XD .svn" IgnoreExitCode="true" />
  <Exec Command="copy $(ProjectDir)..\3rdParty\IronPython\IronPython2.0.2\license.* $(OutputPath)IronPython\" />
</Target>

      



It copies license files (according to license requirements) and excludes subversion (.svn) folders.

The "3rdParty" folder is at the root level of my Visual Studio solution. This is where I keep third party libraries.

I am ignoring the exit code for robocopy because <Exec>

I cannot deal with the exit codes for robocopy. It's not perfect, but my guess is that a simple copy on the next line will cause any problems.

This is how I find the Lib path in the code:

var assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var libPath = Path.Combine(assemblyPath, @"IronPython\Lib");

      

To copy Lib to unit test do the following:

[TestMethod()]
[DeploymentItem(@"3rdParty\IronPython\IronPython2.0.2\Lib", @"IronPython\Lib")]
public void MyTest()
{
    // ...

      

+2


source


ipy.exe calculates this based on the EXE that started the process:

Assembly entryAssembly = Assembly.GetEntryAssembly();
// Can be null if called from unmanaged code (VS integration scenario)
if (entryAssembly != null) {
    string entry = Path.GetDirectoryName(entryAssembly.Location);
    string lib = Path.Combine(entry, "Lib");
    ...

      



Instead, you can look at the location of IronPython.dll:

Assembly entryAssembly = typeof(Hosting.Python).Assembly;

      

+2


source


Try to look:

HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Uninstall \ {80B8E2FB-A8AB-4CBF-836A-B43DE3B3C3F6} \ InstallLocation

The GUID should be the same (I think at least for the same IronPython version), so you should be fine. This will give you the installation directory, then you can simply add@"\Lib"

+1


source


Create an environment variable and access this environment variable from IronPython. This should be the easiest way to do it.

0


source







All Articles