Pass variable from VB.net to Python.

15388
12
Jump to solution
11-20-2014 01:13 PM
DaleShearer
Occasional Contributor


Thanks for looking at my question.  From searching on the internet either this cannot be done, or what code I find does not work.  It shows the VB.net side, but not what has to be done on the Python side.

This should be simple, I have a form in VB.net where the user selects an excel file to be converted to a dbf file then added to a geodatabase.  I can hard code the file and geodatabase name into my python script and it works fine.  I can run the script (with the hard coded names within) from my VB.net form, works fine.

What I want to do is when the user selects a file name from the listbox, that file name is then sent to the Python script as a variable when the script is called from VB.net.  Then within the Python script it picks up that variable.

Am I even close as to how I think this should work.  Dale,

0 Kudos
12 Replies
DaleShearer
Occasional Contributor

At least I am not too far off. I do have the arcpy.env.workspace in the Python Module.

Here is the more accurate code, I had left off the file extensions:

VB.net

Dim A1 As String = "ExcelFile.xlsx"

Dim A2 As String = "DbfFile.dbf"

Shell("C:\Python27\arcGIS10.2\python.exe ""C:\PythonScripts\ExceltoDBF.py" + " " + A1 + " " + A2)

Python Script

import arcpy

import sys

arcpy.env.workspace = "C:/PythonScripts"

arcpy.ExcelToTable_conversion(sys.argv[1],sys.argv[2])

The ExelFile.xlsx is in the C:/PythonScripts folder, when the python script runs it will create the DbfFile.dbf and place it in the same folder.

Once I get this to work the input and output files will be in and go to different places.

When it goes to run the Python script the lack window pops up and closes instantly.  When I run the script with the hard coded file names it stays open until the processing is done.

0 Kudos
PaulCrickard1
Occasional Contributor II

When you execute in VB it closes when done - which is usually what you want when calling from an external program.

the /K keeps it open

cmd /K C:\anaconda\python.exe C:\users\me\desktop\line.py arg1 arg2

0 Kudos
DaleShearer
Occasional Contributor

Thanks Paul for all the help, I also thank James, Xander and Joe.  This was a learning experience.  This issue was how the shell command was constructed

I had been trying the following without success:

Turned out I had double quotes between the exe and arguments, took those out and it works fine.  I took out the addition of the space after the file name just to clean it up a little.

VB.net

Dim A1 As String = "ExcelFile.xlsx"

Dim A2 As String = "DbfFile.dbf"

Shell("C:\Python27\arcGIS10.2\python.exe ""C:\PythonScripts\ExceltoDBF.py" + " " + A1 + " " + A2)

Turned out I had double quotes between the exe and arguments, took those out and it works fine.  I took out the addition of the space after the file name just to clean it up a little.

Shell("C:\Python27\arcGIS10.2\python.exe C:\PythonScripts\ExceltoDBF.py " + A1 + " " + A2)

I will be posting my next issue, I want to stop execution of the VB program while the Python script runs.

Of course it is an issue with the sctructure of the exe and the arguments to Python.

Dale,

0 Kudos