Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

I can't run python scripts on my mac

I follow along in my own text editor and not through Workspace. I'm currently at the Python Collections > Shopping List Take Three.

I've installed Python 3.5.2, but when I run this .py script:

shopping_list = []

def show_help():
    print("\nSeperate each item with a comma.")
    print("Type DONE to quit, SHOW to see the current list and HELP to get this message.")

def show_list():
    count = 1
    for item in shopping_list:
        print("{}: {}".format(count, item))
        count += 1

print("Give me a list of things you want to shop for.")
show_help()

while True:
    new_stuff = input("> ")

    if new_stuff == "DONE":
        print("\n Here is your list:")
        show_list()
        break
    elif new_stuff == "HELP":
        show_help()
        continue
    elif new_stuff == "SHOW":
        show_list()
        continue
    else:
        new_list = new_stuff.split(",")
        index = input("Add this at a certain spot? Press enter for the end the list, or give me a number. Currently placeholder items in the list".format(len(shopping_list)))
        if index:
            spot = int(index) - 1
            for item in new_list:
                shopping_list.insert(spot, item.strip())
                spot += 1
        else:
            for item in new_list:
                shopping_list.append(item.strip())

I get this error.

Traceback (most recent call last):
  File "shipping_list3.py", line 17, in <module>
    new_stuff = input("> ")
  File "<string>", line 1, in <module>
NameError: name 'apple' is not defined

But if I open the file in IDLE and run it there, it works as in the video. Am I missing some libraries on my mac? Or what is the deal here?

5 Answers

Hello,

So this is one of those rabbit hole questions lol. To keep things simple, instead of using "input()" use raw_input().

 new_stuff = raw_input("> ")

AND 

index = raw_input("Add this at a certain spot? Press enter for the end the list, or give me a number. Currently placeholder items in the list".format(len(shopping_list)))

Py 2 vs 3 have changed the functionality around how input works. In this instance, if you run your code, you will notice that even if you put in DONE it fails BUT if you put in "DONE" (with the quotes) it will actually prompt you with the next input and it works. EXCEPT, enter is the next input if I want it added to my list and I can't put quotes around an enter command. Thus it will still fail with a nameError message. I want to really go down this rabbit hole right now, but you should use raw_input() and shouldn't really be in the habit of using input() unless there is a specific use case. You can always google it for more in depth-ness, but whenever excepting input from a user, raw_input works just fine.

Everything in your code correctly, my friend. I think the terminal 'python3' instead of 'python' you use.

$ python --version 

into your ~/.profile or ~/.bashrc or ~/.zshrc

Such as:

alias python=python3

Have fun.

Hello Christian,

Setting up Python for an apple device is covered by Treehouse see: https://teamtreehouse.com/library/setting-up-a-local-python-environment-mac. What text editor are you using specifically? It might not support the use of Python so be sure to use the IDLE, as mentioned or PyCharm see: https://www.jetbrains.com/pycharm/.

Hope this helps.

I mean I could totally be off here and if I am ok. But you realize he ran the program right? He actually posted an error from running the program. Which means he has python installed. Yes, there is a difference between python 2 and 3 on how it handles input() vs raw_input(). But the program is being ran with an error. Which means python is there and to fix said error is to use raw_python. I maybe it's off base and if I am that's fine. But more than one person has commented about setting up python. Which he has clearly done. I too have ran this in python 3 and raw_input is still the answer to the question. Which as I mentioned was a rabbit hole way outside the scope of this course.

Hello Ryan,

There is an error. However, I ran this code in the Python IDLE and there wasn't. So it must have been the text editor he was using, which he mentions in his question. I do not believe that a text editor can support or even run a file with the .py extension.

Hi all, thanks for all the feedback, I really appreciate it.

Just to clearly. I ran the script IDLE too and it worked. But my problem to begin with, wasn't because I ran the script from my editor. I ran the script from the Terminal and got the error. So I thought that perhaps I was missing some dependencies or libraries on my Mac to properly run scripts in Terminal. But I think that @Ryan Ruscett answer above regarding raw_input vs input might be right.