Jack Zhao
Jack Zhao

Reputation: 135

Calling a Python function from another file

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.

Upvotes: 4

Views: 41287

Answers (7)

Bilbo Swaggins
Bilbo Swaggins

Reputation: 93

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

Upvotes: 1

Jack Zhao
Jack Zhao

Reputation: 135

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

Upvotes: 3

Juergen
Juergen

Reputation: 12728

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

Upvotes: 1

corn3lius
corn3lius

Reputation: 4985

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

Upvotes: 0

bud
bud

Reputation: 485

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.

Upvotes: 1

Michael Laszlo
Michael Laszlo

Reputation: 12239

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

Upvotes: 8

Ben Ference
Ben Ference

Reputation: 45

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

from helpers.py import hello

Upvotes: 0

Related Questions