Python script that runs in Windows throws "<string>" error in Ubuntu

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.


I ran your code and it executed correctly. I could not create the same error as in your image. Your code accepted a string as an input for the name field. Python version 3.7 and operating system Ubuntu. Please see the output below:

Hours: 72
Minutes: 0
Seconds: 0
Enter the name: BILLY

3
0
   ID   NAME  LASTNAME  SCORE  ...  KORATBOY  15  123456789.1  123456789.2
0 NaN  BILLY       NaN    NaN  ...       NaN NaN          NaN          NaN

[1 rows x 16 columns]

   ID   NAME  LASTNAME  SCORE  ...  KORATBOY  15  123456789.1  123456789.2
0 NaN  BILLY       NaN    NaN  ...       NaN NaN          NaN          NaN

[1 rows x 16 columns]
1
2
3
4
0
   ID   NAME  LASTNAME  SCORE  ...  KORATBOY  15  123456789.1  123456789.2
0 NaN  BILLY       NaN    NaN  ...       NaN NaN          NaN          NaN

[1 rows x 16 columns]

   ID   NAME  LASTNAME  SCORE  ...  KORATBOY  15  123456789.1  123456789.2
0 NaN  BILLY       NaN    NaN  ...       NaN NaN          NaN          NaN

[1 rows x 16 columns]
1
2
3
4
0
   ID   NAME  LASTNAME  SCORE  ...  KORATBOY  15  123456789.1  123456789.2
0 NaN  BILLY       NaN    NaN  ...       NaN NaN          NaN          NaN

[1 rows x 16 columns]

   ID   NAME  LASTNAME  SCORE  ...  KORATBOY  15  123456789.1  123456789.2
0 NaN  BILLY       NaN    NaN  ...       NaN NaN          NaN          NaN

[1 rows x 16 columns]
1
2
3
4


End

Process finished with exit code 0

I think the solution is you need to use python3 at the command prompt and NOT python to run your file. Your code is clearly written for python3

If python3 is not installed on your system, you can install it by running the following command in the terminal:

sudo apt install python3