I wrote a simple .py program for

def main():
               print ("Hello World")

That's it. I saved it as hello.py
But when i try to run it
I get this error:

>>> hello.py
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
NameError: name 'hello' is not defined

Please Help me. I need the power of python. This happens with every program. I can define functions in the command-line and run them fine, but I can't do advanced stuff.
Any help would be appreciated.

P.S: Even if the code is error free does not work.

7 Years
Discussion Span
Last Post by leegeorg07

You are trying to run your Python program file from the Python shell. That does not work!

Use one of the many good Python IDEs like PyScripter or IDLE, write your program in the associated editor and run it from there.


If you use Python30, adjust your print to print() accordingly.

BTW, do us all a favor and use the customary 4 space indentation, makes your code much more readable,

This code should work with Python30 ...

# modified for Python30

def main():
    print("Hello World")


# make console window wait
input("Press Enter to go on ... ")

Save your code as hello.py. If you use the Windows OS you can also doubleclick on your filename hello.py to make it run.


To gain access to hello.py in another module you could always use an import statement. In order to do this you will have to make sure that the module that you have saved is on the python path.

The python path can be viewed by importing the sys module and then typing sys.path. If your module is not in one of the folders listed you can either move the module to one of the folders that is already on the path or append a new folder to the python path. After you have accomplished this you can simply use an import statement to gain access to the main() function in hello.py. In general import will give you access to all functions defined in the module that is imported.


I forgot that I had to set the path. I did set it to 'D:\Program Files\Python30', my python folder but that did not work.

I'm actually new to python. Very new. I am trying to learn a new and powerful programming/scripting language but I can't seem to run any programs. So Any help is appreciated.

import hello

Why did this work? I mean, is this like C++ only you have to tell it to execute the main function?


It worked. I didn't execute just hello.py
I executed hello.main, the function inside.
Ok, Thank you very much.


you said befor that you set the path to

D:\Program Files\Python30

this wont work, what you actually need to set it as is 'D:\Program Files\Python30\\'

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.