הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Sunday, June 29, 2014 12:13 PM
My goal is to call a python script which takes a string as a parameter and returns a list.
Right now, I'm stuck at calling the script itself! I have the following code:
Dim st_path As String = "C:\Python33\new.py"
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim retVal As String
retval = Shell(st_path)
End Sub
I get an error saying that the file was not found when I click on Button1, even though the file is present!
PS: I'm using VS2010.
PPS: The following also gives the same error:
Dim st_path As String = "C:\\Python33\\new.py"
All replies (11)
Sunday, June 29, 2014 6:33 PM ✅Answered
This did the job:
p.StartInfo.FileName = "C:\Python33\python.exe"
p.StartInfo.Arguments = "new.py hello"
to an extent.
Thank you!
I'm giving up on getting return values! I'll just write into a file from py and read it from VB!
By the way, I did Google, a lot of what I found was for VB6, including the code which I've posted right on top! So I got confused, getting some sanity back now!
Sunday, June 29, 2014 2:03 PM | 1 vote
File not found could mean any file involved with the process.
Doesn't shell merely try to launch the executable that is responsible for files with a .PY extension?
Regardless you should be able to use Process.Start("C:\Python33\new.py") to launch whatever app is the default app for files ending in .py extension.
For example the default app on my system for my user to open .txt files is Notepad. This launches notepad which opens the .Txt file listed.
Process.Start("C:\Users\John\Desktop\Samsung disallow calls.Txt")
La vida loca
Sunday, June 29, 2014 2:08 PM
Error: Value of type 'System.Diagnostics.Process' cannot be converted to 'String'.
Sunday, June 29, 2014 2:30 PM
This change solved the problem.
Dim retVal As ProcessretVal = Process.Start("C:\Python33\new.py")
Could you please tell me how a list can be accepted as a return value?
Sunday, June 29, 2014 2:47 PM | 1 vote
Hi,
I have not done this with a Script but, i have done this with batch files and other odd-n-end exe`s that use the CMD prompt to display there output. I used the Process Class and re-directed the standard output so i could receive the text being sent to the CMD prompt in my VB application. Here is a simple example i had for doing this with a batch file that may help you understand how to set it up. You will still want to read the link above to understand some of the other Properties of the Process class that you might want to change.
Public Class Form1
Private WithEvents proc As New Process
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
proc.StartInfo.FileName = "C:\TestFolder\MyBatchFile.bat"
proc.StartInfo.CreateNoWindow = True
proc.StartInfo.UseShellExecute = False
'proc.EnableRaisingEvents = True 'Use this if you want to receive the ProcessExited event
proc.StartInfo.RedirectStandardOutput = True
proc.Start()
proc.BeginOutputReadLine()
End Sub
Private Sub proc_OutputDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles proc.OutputDataReceived
If e.Data <> Nothing Then
Invoke(New OutputRecievedDel(AddressOf OutputRecieved), e.Data)
End If
End Sub
Private Delegate Sub OutputRecievedDel(ByVal out As String)
Private Sub OutputRecieved(ByVal out As String)
ListBox1.Items.Add(out)
End Sub
End Class
If you say it can`t be done then i`ll try it
Sunday, June 29, 2014 4:18 PM
I wrote the first function that you've given.
Private WithEvents p As New Process
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
p.StartInfo.FileName = "C:\Python33\new.py"
p.StartInfo.Arguments = "hello"
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.UseShellExecute = False
p.StartInfo.CreateNoWindow = True
p.Start()
p.BeginOutputReadLine()
End Sub
Error: The specified executable is not a valid application for this OS platform.
Sunday, June 29, 2014 4:42 PM
Hi,
If you open the CMD prompt and type your file name and the argument "C:\Python33\new.py Hello" or "C:\Python33\new.py -Hello"does it run your script and show the return string in the CMD window ?
If you say it can`t be done then i`ll try it
Sunday, June 29, 2014 5:39 PM
Code in py:
print(sys.argv[1]) --> This prints the argument that has been passed from the cmd prompt
return "Done" --> The return string is not printed!
Sunday, June 29, 2014 6:03 PM | 1 vote
Well, after doing a few quick google searches it appears you need to do something like this. I don`t know if the Hello argument will work or not though. I don`t have Python or know much about it so i am not sure how to use it. You will need to do some searching on running a py file in vb.net with arguments or a few other words to see what you can come up with. I also did see something about some special tools for VS for using Python so you may want to look into them also to see what they are for.
proc.StartInfo.FileName = "C:\Python33\Python.exe"
proc.StartInfo.Arguments = "new.py Hello"
Take a look at these links if that does not work for you
Python - How do you run a .py file?
execute python script from vb.net
There is an endless amount of pages that pop up when i search "Run a py file in vb.net"
If you say it can`t be done then i`ll try it
Sunday, June 29, 2014 6:04 PM
Google search for "Run python script from vb.net"
La vida loca
Sunday, June 29, 2014 6:44 PM
Well, your welcome for the partial fix. I am sure it can be done if you have all the code i posted and your script will send the return string to the CMD prompt. You may have to tinker with the Process settings though. :)
If you say it can`t be done then i`ll try it