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?
Edit in response to answers / comments
- I'm using OS X and Python 3.4
- The two files are in the same directory
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.
Second edit
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!
from helpers import hello
....
helpers.hello() ## You didn't import the helpers namespace.
Your problem is a matter of understanding namespaces. You didn't import the helpers namespace...which is why the interpreter doesn't recognize the helpers. I would strongly recommend you read up on namespaces, as they are very useful in python.
Offical Python Namespace Document
Take a look at these links above.
The python interpreter does not find your module "helpers".
With what operating system do you work?
When you are under Unix/Linux or similar, and your files are in the same directory, it should work. But I heard, that there are troubles working for example on Windows. Maybe, there must be a search path set.
See here: https://docs.python.org/2/tutorial/modules.html#the-module-search-path
Edit: Michael is right, when you do "from helpers import ..." than not the module is importet as such, but only hello is known to the system!
Just do
from helpers import hello
hello()
Or:
import helpers
helpers.hello()
Still the import error must be solved. For that, it would be useful to know your system and directory structure! On a system like Windows, it might be necessary, to set PYTHONPATH accordingly (see link above).
can't comment, but are the two files in the same folder? I would try:
from helpers.py import hello
File system :
__init__.py
helpers.py <- 'hello' function
utils
__init__.py <- functions/classes
barfoo.py
main.py
in main...
from helpers import hello
hello()
import utils # which ever functions/classes defined in the __init__.py file.
from utils import * # adds barfoo the namespace or you could/should name directly.
follow the importing modules in the docs
I had the same problem: ModuleNotFoundError: No module named "Module_Name". In my case, both the module and the script I was calling it to were in the same directory, however, my work directory was not correct. After I changed my working directory using the following, my import worked:
import os
os.chdir("C:\\Path\to\desired\directory")
来源:https://stackoverflow.com/questions/32058827/calling-a-python-function-from-another-file