VanDeath
VanDeath

Reputation: 529

Importing Python Librarys over C#?

So I'm new to Python and I work with IronPython in Visual Studio.

So in my C# Project I call the Python Script for executing some tasks. Now I work in a bigger company and so a lot of File Paths etc. are different. And I'm wondering if it's possible to pass the Python file a Path, and then import the Modules (for example urlib) from this path.

So in C# with IronPython it's possible to execute a Python script with

var ipy = Python.CreateRuntime();
dynamic PYthon_Script = ipy.UseFile("test.py");

And in my Python SCript I got the following Code:

import sys
path = "C:\Program Files (x86)\IronPython 2.7\Lib"
sys.path.append(path)
import urllib
import httplib
...

So I was wondering if it's possible to pass a parameter for the variable path, so sys.path.append(path) will change with the parameter given and the libraries are imported correctly.

Upvotes: 1

Views: 1901

Answers (2)

VanDeath
VanDeath

Reputation: 529

I found a way to do it in a [TestMethod] Project, without console I/O only with C# commands.

C# Code goes as follows:

IDictionary<string, object> options = new Dictionary<string, object>();
options["Arguments"] = new [] {"C:\Program Files (x86)\IronPython 2.7\Lib", "bar"};

var ipy = Python.CreateRuntime(options);
dynamic Python_File = ipy.UseFile("test.py");

Python_File.MethodCall("test");

So basically I submit the Dictionary with the Library path which I want to define in my python file.

So the PYthon Script looks as follows:

#!/usr/bin/python

import sys
path = sys.argv[0]  #1 argument given is a string for the path
sys.path.append(path)
import httplib
import urllib
import string

def MethodCall(OutputString):
    print Outputstring

So The method call is now much easier from C# And the argument passing stays the same. Also with this code you are able to get a custom library folder for the Python file which is very nice if you work in a network with a lot of different PC's

Upvotes: 0

abarnert
abarnert

Reputation: 365945

You just want to pass a parameter to the script? Sure, that's easy.

The main way to do that is by using sys.argv:

import sys
path = sys.argv[1]
sys.path.append(path)
import urllib
import httplib

Then instead of doing this:

py.exe myscript.py

You do this:

py.exe myscript.py "C:\Program Files (x86)\IronPython 2.7\Lib"

If you're running this directly from within a .NET launcher program, you can also just insert the variable dynamically:

PYthon_Script.SetVariable("path", "C:\Program Files (x86)\IronPython 2.7\Lib")

Then, from within the script, you can use that variable.

Or you can even modify sys.path itself from the launcher. See the Runtime docs for details.

If you want to add multiple paths, just change these two lines:

paths = sys.argv[1:]
sys.path.extend(paths)

If you want something that sticks around in your environment, so you don't have to pass it every time, that's what environment variables are for.

There's actually a standard environment variable named IRONPYTHONPATH that should work without you having to do anything. I've never used it myself, but if it works, you don't need to do anything explicit in your code at all. Just set it in your cmd.exe shell, in your Control Panel, in the C# program you're launching myscript.py from, whatever's appropriate. This answer has examples for the first two. (They're setting PYTHONPATH, which affects CPython, instead of IRONPYTHONPATH, which affects IronPython, but it should be obvious what to change.)

If that doesn't work, you can do the same thing manually:

import os
import sys
path = os.environ['MY_IRONPYTHON_EXTRA_PATH']
sys.path.append(path)
import urllib
import httplib

Now, you can set that MY_IRONPYTHON_EXTRA_PATH environment variable instead of IRONPYTHON_PATH.

Here, because you just have a string instead of a list, if you want to specify multiple paths, you need to add a separator. The standard path separator on Windows is a semicolon. So:

paths = os.environ['MY_IRONPYTHON_EXTRA_PATH'].split(';')
sys.path.extend(paths)

Upvotes: 1

Related Questions