-1

I'm an extremely new to coding and I'm writing simple short scripts to learn and practice. I'm following a textbook and I reached the chapter working with inputs. Even after following the code character for character, it wouldn't compile. Eventually, I figured out that I was using python3 grammar while the computer seemed to be expecting python2.

How can I get my computer to recognize the python3 grammar?

The code is written in atom so at first I fished around for a package that might somehow take care of it. Then a friend came over and we spent about two hours ramming our heads into it. Finally, he set up this thing called IDLE which produces a python3.6.7 shell which does run the code, so I know its possible. I've also looked at other forums and one solution that seemed to fit the bill was setting up an alias in bash. However, when I tried to access the bash file from my home directory, it denied me access. Also, I slightly scared of doing that because I saw a post from another novice saying he messed with his bash and now he cant log in to his device. In short, there's probably a simple solution to this but I'm too new to have heard of it.

Btw, I'm running Ubuntu 18.04 as my OS

Here's the code:

message = input('a')

print(message)

Thats the whole script. Two lines. It should simply print 'a' to the console, accept whatever I type, and then print that back out the console. In stead, it produces this error:

aWhatever
Traceback (most recent call last):
    File "/home/username/Desktop/Coding Projects/Python-Projects/Python-Projects/inputs_and_while_loops.py" line 1, in <module>
       message = input('a')
    File "<string>", line 1, in <module>
NameError: name 'Whatever' is not defined 

=================================================
(The program excited with code: 1)
Press any key to continue... 

Again, the code is written in python3 grammar while this seems to be expecting python2 grammar (which when I change it to, it works)

I know you experienced devs out there are probably laughing at how simple a problem this is, but please help a young fledgling coder if you've got the time. Also keep in mind that I know basically nothing about programming and working ubuntu, so if you've got the answer, please go into as much detail as possible and explain literally everything. Proper use of the word literally. I am doing this to learn after all.

Thanks for taking a moment to read this!

Edit: To be clear, I am writing this code in the Atom IDE. User paul41 provided a method that worked (thanks paul) by cd-ing into the directory the script was contained in and using the python3 command on the file. While this does get it to compile, its slightly unwieldy and I was looking for a way to get it to compile from Atom. My current package that I am using to run code is python-run-terminalnx. Heres the link to the page: https://atom.io/packages/python-run-terminalnx Since this package opens up a terminal window to run the code, I thought that there was some setting somewhere in my PATH (which I still don't quite understand how to use) that I could change with a command to get it to use python3 grammar instead of python2 grammar when compling python scripts as a default. Sorry, I should have been clearer

Chichri
  • 35
  • 5
  • 2
    Make the first line of your script like this ```#!/usr/bin/env python```. – accdias May 14 '19 at 02:42
  • For python3 you can use this one as well. `#!/usr/bin/env python3`. – bkyada May 14 '19 at 02:55
  • To accdias: It didn't do anything. The # comments the line out. After removing that, just gave me syntax error – Chichri May 14 '19 at 02:56
  • 2
    Possible duplicate of [Should I put #! (shebang) in Python scripts, and what form should it take?](https://stackoverflow.com/questions/6908143/should-i-put-shebang-in-python-scripts-and-what-form-should-it-take) – accdias May 14 '19 at 03:00

2 Answers2

0

You can do in these ways:

  1. If you know the path of python3, you can execute the following command. python3 <path to the scipt>. To check where python3 is installed use which python3.
  2. Or try using this way. Define a shebang which states the path of the biabry to be used to execute this script like this:
#!/usr/bin/env python3 . # in my case this was the location of python3
message = input('a')
print(message)

After this execute this command to make the script executable:

chmod +x <path to the scipt>

Finally, run this command:

go to the directory where the script is residing
./<path to the scipt>

Please refer to this link for more details of shebang: Should I put #! (shebang) in Python scripts, and what form should it take?.

ashish14
  • 650
  • 1
  • 8
  • 20
0

Ubuntu has both Python 2 and Python 3 installed. Running a program with python my_program.py uses Python 2. To use Python 3 run it with python3 my_program.py

paul41
  • 576
  • 7
  • 17
  • Yes these are how to run the program from the terminal. Open a terminal, cd to the directory that contains your python file and then run the command above changing my_program.py with the name of your program file. Atom my have a built in terminal, but I don’t use it so I’m not sure. Control + alt + t will open a terminal in Ubuntu. – paul41 May 14 '19 at 03:18