Seite 1 von 1
Python in VB.NET Programm integrieren
Verfasst: Freitag 19. Februar 2010, 08:02
von IoI
Hallo,
ich habe in Python mit einem import und ein paar Zeilen Code eine Funktion erstellte, die in ein VB.NET Programm eingebettet werden müsste.
Warum ich die Funktion nicht direkt in VB.NET implementiere hat zwei Gründe:
- Die Implementierung würde anstatt 4 Stunden mehrere Wochen dauern, da ich nichts vergleichbares zu sympy gefunden habe
- Ich mag .NET nicht.
Irgendwelche Ideen, wie ich mein Python Programm in VB.NET einbinden kann (z.B. über Dlls)?
Re: Python in VB.NET Programm integrieren
Verfasst: Freitag 19. Februar 2010, 08:07
von /me
IoI hat geschrieben:Irgendwelche Ideen, wie ich mein Python Programm in VB.NET einbinden kann (z.B. über Dlls)?
Da bietet sich ein Produkt von Microsoft an:
IronPython.
Verfasst: Freitag 19. Februar 2010, 08:45
von IoI
Hatte ich mir auch schon kurz angeschaut, aber irgendwie noch nicht ganz verstanden.
Muss dann jeder Benutzer ironPython installiert haben oder wird mit Hilfe von ironPython ByteCode für CLR erstellt?
Verfasst: Freitag 19. Februar 2010, 08:52
von mkesper
Both the IronPython interpreter and runtime are made of Microsoft Intermediate Language (MSIL), meaning that it is bytecode like all .NET programs. Therefore it requires the IronPython binaries and either the .NET Framework 2.0 (Windows) or Mono (*nix).
Siehe
http://www.ironpython.info/index.php/Deployment
Verfasst: Freitag 19. Februar 2010, 09:35
von IoI
mkesper hat geschrieben:Both the IronPython interpreter and runtime are made of Microsoft Intermediate Language (MSIL), meaning that it is bytecode like all .NET programs. Therefore it requires the IronPython binaries and either the .NET Framework 2.0 (Windows) or Mono (*nix).
Siehe
http://www.ironpython.info/index.php/Deployment
Das wäre natürlich super, aber es fängt schon gut an
Code: Alles auswählen
IronPython 2.0 (2.0.0.0) on .NET 2.0.50727.1433
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append("C:\\Python25\\Lib\\site-packages")
>>> import sympy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python25\Lib\site-packages\sympy\__init__.py", line 22, in C:\Python2
5\Lib\site-packages\sympy\__init__.py
File "C:\Python25\Lib\site-packages\sympy\core\__init__.py", line 4, in C:\Pyt
hon25\Lib\site-packages\sympy\core\__init__.py
File "C:\Python25\Lib\site-packages\sympy\core\basic.py", line 2497, in C:\Pyt
hon25\Lib\site-packages\sympy\core\basic.py
File "C:\Python25\Lib\site-packages\sympy\core\symbol.py", line 293, in C:\Pyt
hon25\Lib\site-packages\sympy\core\symbol.py
File "C:\Python25\Lib\site-packages\sympy\core\sympify.py", line 191, in C:\Py
thon25\Lib\site-packages\sympy\core\sympify.py
File "C:\Python25\Lib\site-packages\sympy\core\numbers.py", line 1641, in C:\P
ython25\Lib\site-packages\sympy\core\numbers.py
File "C:\Python25\Lib\site-packages\sympy\core\function.py", line 474, in C:\P
ython25\Lib\site-packages\sympy\core\function.py
File "C:\Python25\Lib\site-packages\sympy\core\function.py", line 79, in __new
__
TypeError: WildFunction: can only extend one CLI or builtin type, not both IronP
ython.NewTypes.System.Object_3$23_4$24 (for IronPython.NewTypes.IronPython.Runti
me.Types.PythonType_2$14) and IronPython.NewTypes.System.Object_3$23_4$24_8$49 (
for IronPython.NewTypes.IronPython.Runtime.Types.PythonType_2$14)
>>>
genau wie andersherum
Code: Alles auswählen
S:\>cd sympy-0.6.6
S:\sympy-0.6.6>"C:\Programme\IronPython 2.0\ipy.exe" setup.py install
Traceback (most recent call last):
File "setup.py", line 33, in setup.py
File "S:\sympy-0.6.6\sympy\__init__.py", line 22, in S:\sympy-0.6.6\sympy\__in
it__.py
File "S:\sympy-0.6.6\sympy\core\__init__.py", line 4, in S:\sympy-0.6.6\sympy\
core\__init__.py
File "S:\sympy-0.6.6\sympy\core\basic.py", line 2497, in S:\sympy-0.6.6\sympy\
core\basic.py
File "S:\sympy-0.6.6\sympy\core\symbol.py", line 293, in S:\sympy-0.6.6\sympy\
core\symbol.py
File "S:\sympy-0.6.6\sympy\core\sympify.py", line 191, in S:\sympy-0.6.6\sympy
\core\sympify.py
File "S:\sympy-0.6.6\sympy\core\numbers.py", line 1641, in S:\sympy-0.6.6\symp
y\core\numbers.py
File "S:\sympy-0.6.6\sympy\core\function.py", line 474, in S:\sympy-0.6.6\symp
y\core\function.py
File "S:\sympy-0.6.6\sympy\core\function.py", line 79, in __new__
TypeError: WildFunction: can only extend one CLI or builtin type, not both IronP
ython.NewTypes.System.Object_3$44_4$45 (for IronPython.NewTypes.IronPython.Runti
me.Types.PythonType_2$15) and IronPython.NewTypes.System.Object_3$44_4$45_8$68 (
for IronPython.NewTypes.IronPython.Runtime.Types.PythonType_2$15)
S:\sympy-0.6.6>
Verfasst: Freitag 19. Februar 2010, 10:11
von mkesper
Verfasst: Freitag 19. Februar 2010, 10:42
von IoI
Ja, die Links hatte ich auch, scheint aber noch keine Lösung zu geben.
Wie auch immer, ich hab mir jetzt Python2.6 installiert und IronPython mit dem Parameter -X:FullFrames gestartet. Jetzt spukt er einen merkwürdigen Fehler aus.
Code: Alles auswählen
>>> import sympy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python26\lib\site-packages\sympy\__init__.py", line 23, in C:\Python26\lib\site-packages\sympy\__init__.py
File "C:\Python26\lib\site-packages\sympy\assumptions\__init__.py", line 2, in
C:\Python26\lib\site-packages\sympy\assumptions\__init__.py
File "C:\Python26\lib\site-packages\sympy\assumptions\ask.py", line 8, in C:\Python26\lib\site-packages\sympy\assumptions\ask.py
ImportError: No module named boolalg
die Zeile aus der ask.py
Code: Alles auswählen
from sympy.logic.boolalg import to_cnf, conjuncts, disjuncts, \
Equivalent, And, Or, Not
Das merkwürdige ist, dass das Modul unter C:\Python26\Lib\site-packages\sympy\logic\boolalg.py existiert.
Verfasst: Freitag 19. Februar 2010, 14:10
von IoI
Okay, bin ein paar Schritte weiter gekommen in dem ich in der sympy quellen ein paar imports vorgezogen und ergänzt habe.
Sympy kann ich um zwar laden, aber wenn ich sympify() ausführe importiert er noch parser, ein Modul das es für IronPython nicht gibt
http://www.codeplex.com/WorkItem/View.a ... temId=1347
und dem zufolge auch nicht geben wird. IronPython ist irgendwie nicht so das Wahre.
Verfasst: Freitag 19. Februar 2010, 14:56
von mkesper
Verfasst: Freitag 19. Februar 2010, 16:14
von Leonidas
Das geht in die andere Richtung: Ruft aus CPython .NET-Krams auf.
Ich würde jetzt einfach sagen: entweder du bettest CPython in dein .NET-Programm ein, oder du vergisst es. Andere Möglichkeiten gibt es da eher nicht.
Verfasst: Freitag 19. Februar 2010, 16:28
von IoI
Eigentlich will ich ja "nur" einen in Python erstellte Funktion (oder Klasse) unter VB.Net nutzten.
Quick 'n' Dirty würde mir reichen VB übergibt einen String und bekommt einen String zurück. Schöner wäre natürlich wenn ich in VB meinen Pythone Code als Klasse einbinden könnte so nach dem Motto "Imports Python.MyScript"
Verfasst: Freitag 19. Februar 2010, 16:31
von IoI
Leonidas hat geschrieben:
Das geht in die andere Richtung: Ruft aus CPython .NET-Krams auf.
Ich würde jetzt einfach sagen: entweder du bettest CPython in dein .NET-Programm ein, oder du vergisst es. Andere Möglichkeiten gibt es da eher nicht.
Deswegen dachte ich ja an eine DLL, aber weiß bis jetzt nur wie man eine Standalone EXE aus Pythoncode erstellt (mit pyinstaller oder py2exe)
Verfasst: Montag 22. Februar 2010, 09:39
von mkesper
Verfasst: Montag 22. Februar 2010, 13:21
von IoI
Ich bin gerade dabei es über Named Pipes zu probieren. Aber irgendwie klappt das noch nicht.
In Python habe ich dafür das
http://mail.python.org/pipermail/python ... 12152.html hier gefunden. Klappt Python <-> Python auch. Unter VB.NET versuche ich dann
Code: Alles auswählen
...
Dim pipeClient As New NamedPipeClientStream("localhost", _
"mynamedpipe", PipeDirection.In, PipeOptions.None)
pipeClient.Connect()
...
Wahrscheinlich ist der Pfad in Python \\.\pipe\mynamedpipe nicht identisch mit dem in VB.NET.
Verfasst: Montag 22. Februar 2010, 14:34
von IoI
Hihihi, jetzt mach ich es ganz stumpf.
Code: Alles auswählen
get = ""
while get != "exit":
get = raw_input()
doSomethingWithGet(get)
und in VB.NET öffne ich mein Python-Programm als Process und schreibe in STDOUT.
Nicht schön, aber läuft!
Verfasst: Montag 22. Februar 2010, 15:31
von BlackJack
@IoI: Zwei Zeilen weniger:
Code: Alles auswählen
for get in iter(raw_input, 'exit'):
do_something_with_get(get)
Verfasst: Montag 22. Februar 2010, 20:54
von IoI
BlackJack hat geschrieben:@IoI: Zwei Zeilen weniger:
Code: Alles auswählen
for get in iter(raw_input, 'exit'):
do_something_with_get(get)
Sehr geil, ich programmiere seit Jahren Python und hab das Gefühl, dass ich jeden Tag neue "Grund-"Funktionen lerne. Thx