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>
    hello.py
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.

Recommended Answers

All 5 Replies

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.

See:
http://www.daniweb.com/forums/post104834-1.html

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")

main()

# 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
hello.main()

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

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

Member Avatar for leegeorg07

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\\'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.