4

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

  1. I'm using OS X and Python 3.4
  2. The two files are in the same directory
  3. 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.

4
  • 1
    Could you please give information about your Operating system and file system structure in which you are working? Commented Aug 17, 2015 at 20:27
  • 1
    No module named helpers means it couldn't find the program. Use sys.path.append(dir_name) to add the directory to the ones that Python searches for programs from import statements. Commented Aug 17, 2015 at 20:37
  • 1
    Or you can also use the PYTHONPATH environment variable for that. Commented Aug 17, 2015 at 20:49
  • Changing the CLASSPATH (a Java environment variable) fixed a Python problem? That seems very strange. Are you sure you didn't change the PYTHONPATH (a Python environment variable) instead? Commented Aug 18, 2015 at 14:24

7 Answers 7

8

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

1 Comment

While it's true that the from helpers import hello/helpers.hello() combination won't work, that also won't lead to an ImportError.
3

I reset the CLASSPATH and it works fine somehow. Weird problem. Thanks everyone!

3 Comments

The best thing to do here (in my opinion) would be to move your accepted answer to this one and remove the edits to your question - at the moment it looks a bit confusing.
How did you reset the CLASSPATH?
Could you please expand on how you did this otherwise it is not really an answer.
1
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.

Namespace Document 1

Offical Python Namespace Document

Take a look at these links above.

3 Comments

That was already said, but does not help with the import error.
No one formally introduced the concept of namespaces...I thought provided some docs would help. When I first learned python, I came from a beginner Java background. So namespaces were kind of foreign to me but once I got to use them enough they became second nature.
Still it does not help.
1

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

9 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
@JRichardSnape: When you read my post completely, than you might have seen, that I gave other hints like the module search path that could help. I also left a comment at the post, that was unanswered. When you read the other answers here, than you might see, that many give less of an answer. Why don't you criticize those?
@JRichardSnape: I am so fed up with these people here and a review system, where you are continuously criticized, when you really try to help. I did also reviewing, but I did try to see the positives and not only the rule-book.
sorry you feel this way. As you obviously know - it just came to me in a string of reviews - sorry if you don't think I gave it enough consideration, I just saw that the error you noticed (whilst valid) doesn't affect the ImportError and that you'd got a question early in the answer that (in my opinion, YMMV) should be in a comment. I sometimes have the same frustration with reviews and flag decisions. In general, though, I reckon the pluses outweigh the minuses. I also try to see the positives, maybe I should have edited instead of reviewing the way I did.
@JRichardSnape: Thanks for the comment. I had some very frustrating moments yesterday with Stackoverflow. I feel more and more, that the review system of SO is bringing helping discussions down to a frustrating tour-de-force of whacking the rules on other people. Instead of letting helpful discussions flow, questions are stopped in midst of a discussion, because 5 people think, that there is some fault on it. Some times, only one decides, who votes first -- the other four just follow, because they do not take the time to read the full context. --
|
1

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

Comments

0

can't comment, but are the two files in the same folder? I would try:

from helpers.py import hello

1 Comment

Does not work. The system automatically adds that ".py".
0

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

1 Comment

I don't know, why all want to force a package here. Python allows to use standalone modules without package and all the other stuff. I don't see, how making things more complicated, will help the questioner. See here: docs.python.org/2/tutorial/modules.html#the-module-search-path

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.