0

I have a segmenting.py module in a package called processing.

I am trying to call a function in the module in my main. It is extremely simple.

In main.py

from processing import segmenting

segmenting.test()

In segmenting.py

def test():
    print 'succeed'

However, I end up with errors as follows:

>>> from processing import segmenting
>>> 
>>> segmenting.test()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'test'
>>> 

Where went wrong?

7
  • 1
    Do you have a init.py in your package? Commented Sep 4, 2013 at 9:33
  • Note that stackoverflow cut the "_" before the init and after. There are 2 underscores before and after. Commented Sep 4, 2013 at 9:34
  • 2
    @BenMezger without __init__.py the import wouldn't work Commented Sep 4, 2013 at 9:38
  • your code works perfectly for me (obviously, adding __init__.py you do not mention to the package) Commented Sep 4, 2013 at 9:39
  • 1
    @BenMezger: use backticks: __init__.py. See daringfireball.net/projects/markdown/syntax#code Commented Sep 4, 2013 at 9:40

1 Answer 1

2

The most likely cause is that you didn't restart your interactive interpreter after editing (and saving!) segmenting.py. Modules are imported only once and cached. If you edit the source code and then run the import statement again, the module is simply retrieved from the cache and doesn't pick up your changes. See also the reload() built-in.

Sign up to request clarification or add additional context in comments.

3 Comments

But Python is also OOP like Java right? I don't have to "construct the object and then run its method?" Just directly call the functions in a .py file will do?
This is completely unrelated to OOP. No classes involved here, at least not in a way that would matter in the given context.
So under what circumstances do we construct an object out of a class in Python? It seems that I have been using python to write scripts

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.