Pass variable from VB.net to Python.

15383
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
1 Solution

Accepted Solutions
PaulCrickard1
Occasional Contributor II

Looks good. On the python side, import sys. Then you will have an array named sys.argv. You don't have to do anything to add data to it, it is already there.

sys.argv[0] will be the file name (ExceltoDBF.py)

[1] through will be the command line arguments. So think of the args as being indexed at 1.

View solution in original post

0 Kudos
12 Replies
JoeBorgione
MVP Emeritus

I'm neither an expert in vb.net or python, but back in the olden days, when I had more hair and none of it was gray, the operating system of choice was unix. We would write a variable name to a text file, and then open that file to read the contents.  We moved variables back and forth from AML to C or Bourne shell scripts that way.

That should just about do it....
0 Kudos
XanderBakker
Esri Esteemed Contributor

Maybe this site can help you:

28.1. sys — System-specific parameters and functions — Python 3.3.6 documentation

Although this is 3.3 documentation, it will also work for python version 2.x

or this site:

10.6. Handling command-line arguments

Kind regards, Xander

0 Kudos
JamesCrandall
MVP Frequent Contributor

This Python - Command Line Arguments  was a good tutorial on processing incoming arguments in a python script.

This .net - execute python script from vb.net - Stack Overflow has a good example of executing the python script from .NET application including the arguments.

PaulCrickard1
Occasional Contributor II

James is right. The answer is to pass args to the python script from the command line. The only other option is using Iron Python, but that is overkill. Just accept args and then use the process execute python.exy myprogram.py param1 param 2....

0 Kudos
DaleShearer
Occasional Contributor

I want to thank you all for your help, I have learned more in looking at the links you gave me.  But I am still stumped, It must be from years in VBA to VB.net land, then figuring out Python as I go.

I see (I think) how it works, but I am stuck on this:

I have a shell command to call my Python script:

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

This works fine as I have the Excel file name and dbf file name hardcoded.  Now the way I see it I can pass the two file names via the arg1 and arg2 variables.

Shell("C:\Python27\arcGIS10.2\python.exe ""C:\PythonScripts\ExceltoDBF.py arg1 arg2""")

How do I set up the arg1 and arg2 variables so that they are recognized by the Shell command?

I may not be asking the right thing the right way, I am just trying to figure out how to set the varaibles to the file names so that they can be passed to the script.

I am not wanting anyone to do this for me, I have not grasped how to do this yet, just point me the right direction.

Thanks, Dale

0 Kudos
DaleShearer
Occasional Contributor

Okay, I think I made some progress, here is my current shell command structure, it does not die in VB.Net and it calls the script.  No, I do not have the script side done, hopefully this is the correct structure.

Dim A1 As String = "ExcelFile"

Dim A2 As String = "DbfFile"

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

Dale,

0 Kudos
PaulCrickard1
Occasional Contributor II

Looks good. On the python side, import sys. Then you will have an array named sys.argv. You don't have to do anything to add data to it, it is already there.

sys.argv[0] will be the file name (ExceltoDBF.py)

[1] through will be the command line arguments. So think of the args as being indexed at 1.

0 Kudos
DaleShearer
Occasional Contributor

Thanks Paul, I am getting there.  Your explanantion of the sys.argv really helps, I might of blown over it, or read it and did not understand at the time.

From within python I ran a simple routine of  print 'Argument List:', str(sys.argv) and I got the file name just like you said.

Now I am working on the two variables I am passing in, so I am thinking it should work this way:  (which is not working)

VB.net

Dim A1 As String = "ExcelFile"

Dim A2 As String = "DbfFile"

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

Python

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

I know it all right here in front of me, I just have to see how it works.

Dale,

0 Kudos
PaulCrickard1
Occasional Contributor II

That looks like it should work.

If it doesn't you may need to add

arcpy.env.workspace = "c:/data"  #wherever the files are or use the path as a parameter.

0 Kudos