Linux and Python...acting strangely

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

Linux and Python...acting strangely

Postby TheHegemon » Tue Feb 21, 2012 6:05 pm UTC

Okay, so maybe I'm just making a newbie error, but I'm working with python 3.2 and a linux box at the moment. When these programs are run via the terminal and ask for user input, they invariably throw a name error. If, for example, I input 'asdf' (without the quotes), the terminal tells me: NameError: name 'asdf' is not defined.

If I typed in 'asdf' with the quotes, though, it works fine. It also works the first way if I run it in IDLE. Is this something with the linux terminal? Am I missing something basic in Python? Thanks!
TheHegemon
 
Posts: 7
Joined: Sun Sep 25, 2011 2:51 am UTC

Re: Linux and Python...acting strangely

Postby Shivahn » Tue Feb 21, 2012 6:20 pm UTC

It works if you type in asdf without quotes in IDLE?

It shouldn't unless you've bound it first. What's happening when you type in asdf without quotes is you're asking the shell for the value that the variable asdf is bound to. It's looking for the binding and, unless you've previously typed in asdf=something, asdf is unbound, so it's throwing an error because you're asking for a binding for a variable that doesn't exist.

When you type in 'asdf' what you're asking is for the shell to tell you the value of the string 'asdf'. The value of a string is the string itself, so when asked what 'asdf' evaluates to, the shell responds 'asdf', as it's its own value. The string 'asdf' is a constant, whereas the variable asdf is a variable that needs to be bound to have meaning.
User avatar
Shivahn
 
Posts: 2144
Joined: Tue Jan 06, 2009 6:17 am UTC

Re: Linux and Python...acting strangely

Postby ahammel » Tue Feb 21, 2012 6:47 pm UTC

TheHegemon wrote:Okay, so maybe I'm just making a newbie error, but I'm working with python 3.2 and a linux box at the moment. When these programs are run via the terminal and ask for user input, they invariably throw a name error. If, for example, I input 'asdf' (without the quotes), the terminal tells me: NameError: name 'asdf' is not defined.
[...]


Back up a bit. Which programs? What do you mean "ask for user input"? Are you using the "input" function, or are you just typing things into the python shell?

As Shivahn pointed out, the NameError exception is the correct behaviour if you reference an unassigned variable name. Are you seeing something like this?

Code: Select all
Python 3.2.1 (default, Jul 11 2011, 18:55:33)
[GCC 4.6.1 20110627 (Red Hat 4.6.1-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> asdf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'asdf' is not defined
>>> 'asdf'
'asdf'
>>> asdf = 'some value'
>>> asdf
'some value'
I also answer to 'Alex'
User avatar
ahammel
 
Posts: 976
Joined: Mon Jan 30, 2012 12:46 am UTC
Location: Vancouver BC

Re: Linux and Python...acting strangely

Postby TheHegemon » Wed Feb 22, 2012 2:10 am UTC

@ahammel, even something like

Code: Select all
y = str(input('y? '))
print(y)


is throwing the error

"Traceback (most recent call last):
File "input_test.py", line 3, in <module>
y = str(input('y? '))
File "<string>", line 1, in <module>"

assuming I type 'asdf' at the prompt above.
TheHegemon
 
Posts: 7
Joined: Sun Sep 25, 2011 2:51 am UTC

Re: Linux and Python...acting strangely

Postby phlip » Wed Feb 22, 2012 2:15 am UTC

What version of Python are you using?

If you're using one of the Python 2 series, you should be using raw_input() instead of input(). You also don't need the str() call around it.
Code: Select all
y = raw_input("y? ")
print(y)


See, in the older versions of Python, the input() function asks the user to enter a Python expression - it then evaluates that expression and returns the result. So if you were to type in "1+3", for instance, it would set y to "4". Whereas raw_input returns exactly what is entered, as a string... so if you type "1+3" it'll set y to "1+3". There is rarely any good reason to use input(), and in Python 3 they changed it so that input() now does what raw_input() used to do, and there's now no builtin that asks for input from the user and then evaluates it.

So it sounds like you're using Python 2, but reading a tutorial intended for Python 3... because there were a lot of changes between the two versions, you should be using tutorials intended for the version you're using.
While no one overhear you quickly tell me not cow cow.
but how about watch phone?
User avatar
phlip
Restorer of Worlds
 
Posts: 6740
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia

Re: Linux and Python...acting strangely

Postby ahammel » Wed Feb 22, 2012 3:46 am UTC

I think philip's got it right. It sounds like you've got py3k installed (which would explain why it works through IDLE). If you've got both versions installed (and even if you only installed 3.2 explicitly, 2.7 is almost certainly on there somewhere) you have to use "python3" to call a shell from the command line. The "python" command gives you a py2k shell.

Oh, and in python 3:
Code: Select all
y = str(input("y? "))
gives you the same result as
Code: Select all
y = input("y? ")
I also answer to 'Alex'
User avatar
ahammel
 
Posts: 976
Joined: Mon Jan 30, 2012 12:46 am UTC
Location: Vancouver BC

Re: Linux and Python...acting strangely

Postby EvanED » Wed Feb 22, 2012 4:06 am UTC

It's definitely worth making sure it's what you think it should be. python --version will tell you what's actually being run, or you can look at the first line of output when you start up the REPL.
EvanED
 
Posts: 3767
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI

Re: Linux and Python...acting strangely

Postby TheHegemon » Wed Feb 22, 2012 6:01 am UTC

It was version incompatability. it works perfectly as long as i specify python3. Thanks, all!
TheHegemon
 
Posts: 7
Joined: Sun Sep 25, 2011 2:51 am UTC


Return to Coding

Who is online

Users browsing this forum: Farpappestals and 9 guests