This problem has confused me for days.
I have two files, helpers.py
and launcher.py
.
In helpers.py
I have defined the function hello()
, which prints "hello".
I want to call hello()
in launcher.py.
This is what I wrote in launcher.py
:
from helpers import hello
....
helpers.hello()
But when I run it, I get this:
from helpers import hello
ImportError: No module named helpers
How do I fix this?
I tried the two ways:
from helpers import hello
hello()
and
import helpers
helpers.hello()
But still this bug:
import helpers
ImportError: No module named 'helpers'
I think there should be something wrong in the CLASSPATH of Terminal.
The problem highlighted in these answers was an issue, but in the end resetting the classpath resolved.
The problem is with this line:
helpers.hello()
Replace it with this:
hello()
Now it works because you've only imported the name hello
from the helpers
module. You haven't imported the name helpers
itself.
So you can have this:
from helpers import hello
hello()
Or you can have this:
import helpers
helpers.hello()
I reset the CLASSPATH and it works fine somehow. Weird problem. Thanks everyone!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With