I have one program on the /Desktop called hello.py
def pri():
print "hello"
Then I have another program on the /Desktop called run.py
from hello.py import pri
pri()
It gives me error that no module exist.
How can I successfully import methods from other python programs in the same directory.
2 Answers 2
You simply call it hello not hello.py:
from hello import pri
pri()
If you have a file called some_name.py, the module name is only some_name and not some_name.py.
To import all methods, do it as:
from hello import *
anon582847382
20.5k5 gold badges58 silver badges60 bronze badges
answered Apr 12, 2014 at 12:12
sshashank124
32.3k10 gold badges73 silver badges77 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
anon582847382
lol, I think I answered his question in like 2 seconds! It's nuts how fast people can answer on this site.
user3526694
thanks. and how can i import all the methods of the hello.py (assuming it has more than one method)
Remove the .py:
from hello import pri
^
You don't need the file extension when importing modules, so when you try to include it it will throw an error.
answered Apr 12, 2014 at 12:12
anon582847382
20.5k5 gold badges58 silver badges60 bronze badges
3 Comments
user3526694
thanks. and how can i import all the methods of the hello.py (assuming it has more than one method)
anon582847382
@user3526694 That would be just
from hello import *anon582847382
@user3526694 Also if I have resolved your issue, please accept my answer. In about 4 minutes time.
lang-py