1

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

1

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 *
Sign up to request clarification or add additional context in comments.

2 Comments

lol, I think I answered his question in like 2 seconds! It's nuts how fast people can answer on this site.
thanks. and how can i import all the methods of the hello.py (assuming it has more than one method)
0

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.

3 Comments

thanks. and how can i import all the methods of the hello.py (assuming it has more than one method)
@user3526694 That would be just from hello import *
@user3526694 Also if I have resolved your issue, please accept my answer. In about 4 minutes time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.