0

The version of Python I'm using is 2.7.

I have this function in a file called RunAPIGeocoder.py

APIGeocoder.geocode(excel_path=r'{}'.format(input_path))

I need to call this process in the command line and I want to simply pass through an excel file

if I change the function to

APIGeocoder.geocode(excel_path)

and run from CMD

python RunAPIGeocoder.py C:path\Book12019_07_29_16_03_12.875947.xlsx

it will give me this error

NameError: name 'excel_path' is not defined

excel_path is a parameter in the geocode function

I should note I am not trying to use raw_input from the user because this process will be piped into another process and I do not want user input

part of the geocode function in APIGeocoder.py

def geocode(excel_path):
    try:
        print 'Geocoding in Process...Start Time: {}'.format(time.strftime('%c'))

        # Read in data
        df_input = pd.read_excel(excel_path, converters={'NodeId':str})
6
  • Is the library the one described at geocoder.readthedocs.io? Because I can't find APIGeocoder. Commented Jul 30, 2019 at 14:48
  • no this is a custom script, nothing to do with that geocoder Commented Jul 30, 2019 at 14:49
  • 2
    Parameter != argument: APIGeocoder.geocode(excel_path=input_path). The call to format doesn't really accomplish anything. excel_path is the name of the parameter; input_path is the name of the argument assigned to the parameter for that function call. Commented Jul 30, 2019 at 14:54
  • @chepner the script works with APIGeocoder.geocode(excel_path=r'{}'.format(input_path)) Commented Jul 30, 2019 at 15:00
  • 1
    @ziggy Of course it does; because that call to format returns input_path unchanged. The problem with switching from geocode(excel_path=...) to geocode(excel_path in that you are switching from passing input_path via a keyword argument to passing the (nonexistent) local variable excel_path as a positional argument. Commented Jul 30, 2019 at 15:02

2 Answers 2

1

TL;DR:

Try changing APIGeocoder.geocode(excel_path) to APIGeocoder.geocode(excel_path=input_path).

The first version you provided works because the variable that contains what you are passing from the console is input_path.

excel_path is the parameter of the geocode method. On the other hand, input_path is the argument you are passing to the method. In your first example, you were using input_path as an argument, but preprocessing it first:

APIGeocoder.geocode(excel_path=r'{}'.format(input_path))

In other words, you are telling the geocode method that its parameter excel_path shall contain the value r'{}'.format(input_path), which is some processing that you do to the variable input_path. Read about the difference between parameters and arguments!

Sign up to request clarification or add additional context in comments.

5 Comments

says excel_path is not defined
can you provide me with a minimum not working example here? colab.research.google.com/drive/…
I fully understand the difference between a parameter and an argument
The answer you provided is equivalent to mine, right? Where I use input_path as a variable, you are using f.
yeah but *sys.argv[1:] was what I was missing and did not know how to use it
1

figured it out

def run_geocoder(f):
    APIGeocoder.geocode(excel_path=r'{}'.format(f))

run_geocoder(*sys.argv[1:])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.