Ubuntu – Python script that runs in Windows throws “” error in Ubuntu

18.04command linepythonscripts

I've written a Python script (work.py) and it's running well in Windows, but when I run it on Ubuntu, it shows me an error, if I enter the name as a string, but if I enter the name as an integer it's running fine.

NameError:

thanapong@thanapong-Lenovo-C360:~/Desktop$ python work.py 
Hours: 72 
Minutes: 0 
Seconds: 0 
Enter the name: CHATCHAI 
Traceback (most recent call last): 
  File "work.py", line 9, in <module>
    look for = str(input("Enter the name: "))
  File "<string>", line 1, in <module> 
NameError: name 'CHATCHAI' is not defined 
thanapong@thanapong-Lenovo-C360:~/Desktop$ python work.py 
Hours: 72 
Minutes: 0 
Seconds: 0 
Enter the name: BILLY 
Traceback (most recent call last): 
  File "work.py", line 9, in <module>
    look for = input("Enter the name: ") 
  File "<string>", line 1, in <module> 
NameError: name 'BILLY' is not defined 

With int it works fine:

thanapong@thanapong-Lenovo-C360:~/Desktop$ python work.py 
Hours: 72 
Minutes: 0 
Seconds: 0 
Enter the name: 2

3
0
Not found
1
2
3
4
0
Not found
1
2
3
4
0
Not found
1
2

This is my code.

import pandas as pd
import time

hourz=input('Hours: ')
minz=input('Minutes: ')
secz=input('Seconds: ')
lookfor = input("Enter the name: ")
name = str(lookfor)
x = 5
print(' ')
hour=int(hourz)
min=int(minz)
sec=int(secz)
day = int(((hour*60*60)+(min*60)+(sec))/86400)

print(day)
for b in range(day):
    for a in range(x):
        time.sleep(1)
        print(a)
        if a == 0:
            data = pd.read_csv("/media/thanapong/F91B-8B18/Customer_List.txt", header=0)
            query = data.loc[data['NAME']==name]
            if not query.empty:
                print(query)
                data.loc[data['NAME']==name, 'SCORE'] = (query.SCORE - 1)
                print()
                print(data.loc[data['NAME']==name])
                data.to_csv("/media/thanapong/F91B-8B18/Customer_List.txt", index=None)
            else:
                print('Not found')
print(' ')
print(' ')                
print("End")

This is example of Customer_List.txt (it has many lines).

ID,NAME,LASTNAME,SCORE,TEL,PASS
61070500216,CHATCHAI,KARUNA,22,123456789,12345
61070500217,BIGM,KORATBOY,15,123456789,123456789

Best Answer

  • So far, in Ubuntu, a python command provided by the system is always Python 2, never Python 3, and for Python 3 one must use python3.

    This is the case even on systems that don't have Python 2 installed but have Python 3 installed. Newer Ubuntu releases come that way, and there is a python3 command but no python command. You can install the python metapackage, which installs Python 2.

    Older Ubuntu releases ship with Python 2 installed and not Python 3. As Raffa says, you can install Python 3 on such a system through the python3 metapackage.

    Until recently, having python be Python 2 (and not Python 3) had been the official recommendation for Unix-like operating systems. The popular GNU/Linux systems adhered to this recommendation, with the notable exception of Arch Linux and its derivatives, on which python has long been Python 3. Debian and Ubuntu (which is a derivative of Debian), among other systems, continue to use the traditionally recommended naming scheme, so far.

    In my experience, users of some other OSes--especially Windows--often find this surprising. It can be especially confusing to novices who aren't well aware of the distinction between Python 2 and Python 3.

    My recommendation is:

    1. Always know (or find out) which of Python 2 or Python 3 one is writing.

      It is possible to write code deliberately that works as both, but people who do that know they're doing it, and this is not something most people will do when they start out.

    2. For Python source code files that you run (i.e., scripts/programs, rather than modules usable only as libraries), start the file with a shebang line that specifies which interpreter you want.

      For Python 3 you will usually wanted to use #!/usr/bin/env python3, as Ray Butterworth has commented. For Python 2, consider #!/usr/bin/env python2. Although you can use #!/usr/bin/env python in Ubuntu, using the python2 command is also supported, makes your meaning clear to humans, works on more operating systems, and might work longer into the future.

    The shebang line doesn't affect what interpreter is used if you run the script by passing its name as an argument to a command like python (or python2) or python3. Instead, mark the script executable so you can run it:

    chmod +x scriptname

    You only have to do that once. Then you can run the script like this:

    ./scriptname

    On Windows, you can use the py launcher command to run your script, which respects your shebang line unless you override it by specifying a version. That is, if your script starts with #!/usr/bin/env python2, running py scriptname in Windows has the same effect as py -2 scriptname, and if it starts with #!/usr/bin/env python3, running py scriptname in Windows has the same effect as py -3 scriptname.

    Note, however, that py treats python (with no version number) as specifying Python 3 rather than Python 2. This is another reason to consider writing python2 (rather than just python) at the end if your shebang lines even in Ubuntu; it makes your scripts more portable to Windows when the py launcher is used.


    As for why you got those errors when you inadvertently ran your Python 3 code in a Python 2 interpreter: the input function in Python 3 just reads and returns a line of input, which is what you want, but the input function in Python 2 reads a line of input, treats it as Python code, runs that code, and returns the resulting value. This has quite alarming security implications when used without care (even in Python 2 programs), and is also just not something people usually want to do. If you needed to write your script in Python 2, you would use the raw_input function instead as steeldriver says, but really the solution is to use python3 as described above, which in spite of its name is effectively the default Python interpreter in Ubuntu. Inputs like 123 are Python expressions, but inputs like BILLY aren't, unless some variable by that name has been defined; this is why numerical input happened to work.

  • Related Question