3

I have a file that I've made executable. It has a function in it that I would like to return its results to the command line but, I keep getting NameError messages. To break things down, I'm using LinuxMint Lisa and so far I have:

#! /usr/bin/env python
import mechanize
from BeautifulSoup import BeautifulSoup
import sys

def dictionary(word):
    br = mechanize.Browser()
    response = br.open('http://www.dictionary.reference.com')
    br.select_form(nr=0)
    br.form['q'] = word
    br.submit()
    definition = BeautifulSoup(br.response().read())
    trans = definition.findAll('td',{'class':'td3n2'})
    fin = [i.text for i in trans]
    query = {}
    for i in fin:
        query[fin.index(i)] = i
    return query

print dictionary(sys.argv)

Then I chmod from my terminal:

sudo chmod +x this_file.py

When I call this file from the command-line, I'll enter:

./this_file.py 'pass'(or any other string argument)

Which will return:

TypeError: Must assign a string

So I know I'm obviously not using sys.argv correctly but, I have a feeling like I'm mucking something else up when attempting to return this functions results to the command-line.

3
  • 2
    in print dictionary(agrv) argv is misspelled. Also, use sys.argv Commented Aug 12, 2012 at 20:57
  • 2
    It's better to provide the full trace-back to give the error message some context - esp information about line number where the error occurs. Commented Aug 12, 2012 at 21:26
  • Also, you might consider posting a different question instead of "evolving" this one. Just a thought. Commented Aug 12, 2012 at 21:29

5 Answers 5

3

Ok, I might as well post this as my answer instead of just a comment:

In

print dictionary(agrv)

argv is misspelled.

It should be

print dictionary(sys.argv)

Also, use sys.argv, argv by itself won't suffice

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

7 Comments

When I use sys.arvg I get a syntax error. So def dictionary(sys.argv): is incorrect?
@tijko sys.arv is incorrect, it's sys.argv (note the "g"). What you call your parameter your function header (the line with the def) is totally up to you though, but in the call you do need the right name.
Yeah, I need to be much more careful with spelling on here. What I had in the file was sys.argv.
@tijko I usually would copy-paste code from elsewhere to here, too much risk of typos otherwise :) Did this solve your problem?
I'm returning a TypeError now.
|
2

argv is an attribute of the sys module

Use either

sys.argv

or do

from sys import argv

2 Comments

I figured that but, for some reason I remember seeing that argv was used by itself?
if you write import sys you'd have to use sys.argv, else if wrote from sys import argv you'd have to use argv. And both would be correct.
1

Shouldn't it have been print dictionary(sys.argv[1]). I guess you want to search the commandline argument in the dictionary.com

Comments

1

The problem with the question as currently posted is that sys.argv is a list, not a string, so when you set the form entry 'q' you are setting it to a list of arguments to the program. You could change the program to pass in the first argument:

print dictionary(sys.argv[1])

Or call the dictionary functions multiple times:

for i in sys.argv[1:]:
    print dictionary(i)

Note that we don't want to include the program name itself so omit sys.argv[0].

1 Comment

Ah, that makes sense. I haven't used sys.argv much and it shows. I thought that **kwargs used lists,tuples, etc.
-1

You don't make it clear what you mean by 'returning' results to the command line. If you just want to print the results, you can do print.

But the error message you're getting has nothing to do with that. It's caused by two things: one, you've put agrv instead of argv. And second, you've imported sys, so you need to reference sys.argv not just argv.

1 Comment

Your dictionary function can call its argument whatever it likes. But you must call it (the last line in your code) with dictionary(sys.argv). This has nothing to do with argv specifically.

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.