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 Python Basics (2015) Python for Beginners Running Python Scripts

anhedonicblonde
anhedonicblonde
3,133 Points

"helloworld" is not defined?

when I tried to do the initial script run of helloworld.py, I got a traceback error that said "helloworld is not defined". I tried it on my system with Python's shell and the same thing happened. I'm baffled. Any ideas? I've run scripts before with no problem. Thanks in advance.

print("Hello world")

this is then saved as "helloworld.py" (no quotes of course) and I try to call it in either Python's shell or Powershell (or in the Playground) and every time I get "helloworld is not defined" as if it were a variable I was trying to call within the script (when actually it's taking the script name and dropping the .py extension). I even tried running scripts I wrote before that I knew worked, and they did the same thing (only with their own file names with the .py dropped).

Thanks!

[MOD: fixed backticks in markdown -cf]

anhedonicblonde
anhedonicblonde
3,133 Points

there are no ticks around the print function, i was trying to do the markdown thing and i thought it would pre-format the code to look like code (like when you pre-format text in html) but instead it looks like i put ticks in my code. sorry about this. i'm new here.

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

With file helloworld.py:

helloworld.py
print("hello world")

If you are at a command or bash shell prompt:

$ python helloworld.py
hello world
$

If you are in Python REPL, import the file which will interpret all of the commands:

$ python
Python 3.4.0 (default, Jun 19 2015, 14:20:21) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import helloworld
hello world
anhedonicblonde
anhedonicblonde
3,133 Points

thanks! i needed to exit out of the python shell and back into the command line, now all is good :)

Edit

sorry i didnt read the first paragraph of your post very clearly

when you run helloworld.py are you in your terminal/powershell/python IDE shell and using

python helloworld.py

? for python IDLE if you have a separate file open from the shell you want to hit F5 to run the script

anhedonicblonde
anhedonicblonde
3,133 Points

hi Darrian that's what i have in my file - but when i try to run it in the shell, i get the bizarro message. it's taking the filename helloworld.py, dropping the .py extension, and telling me the error based on that - because my code looks exactly like what you have there. i am mystified. i have run scripts before and not had this problem. it totally acts like i am asking for an undefined variable but i'm not. my mind is boggled. thanks for responding! annie

anhedonicblonde
anhedonicblonde
3,133 Points

yes that is what i'm using to run it. i went into that because i first encountered the error using the treehouse interface to try to run the helloworld.py code in the python basics lesson, strangely enough. i didn't do F5 because i was trying to duplicate what we were doing in the lesson.

anhedonicblonde
anhedonicblonde
3,133 Points

Just to update in case anyone is curious: I can run the script open as a module in the python shell where it prints the output and THEN gives a NameError like helloworld is a variable (it's not, it's the scriptname with the .py extension dropped, as 'helloworld' does not exist anywhere in the script as such). I am going to uninstall Python and then re-install and see if this fixes the problem. Bizarre. This never happened before. I promise I have run scripts and they ran just fine. Don't know what has caused this illogical error to occur. Thanks for the replies :)