1

In otsu.py I have:

 def Hello(n):
    print "Hello",n
 print "abc"
 exit()

In another.py

from otsu import Hello
Hello(5)

When I run python another.py, the output is abc, not Hello, 5.

What am I doing wrong?

5
  • 1
    I would expect to get both abc and Hello, 5. Are you sure you're getting the right import? Commented Jan 1, 2015 at 22:57
  • You may have an otsu.pyc or otsu.pyo file in the directory with old code in it Commented Jan 1, 2015 at 22:58
  • It's working absolutely fine for me. Try from otsu import * Commented Jan 1, 2015 at 23:02
  • 1
    @JonathanDavies wild card imports are generally not recommended, as they clutter the namespace and make the code much harder to follow (or even, in the worst cases, break the code by shadowing other names). Commented Jan 1, 2015 at 23:09
  • I added the exit() after, but it turns out that was causing the problem. I didn't know that it would exit from the main program as well Commented Jan 1, 2015 at 23:17

1 Answer 1

3

Firstly, make sure you don't have any stale .pyc or .pyo files in the directory. Or if you're using Python 3 then remove the __pycache__ directory just to be sure. This is likely the problem.

In another.py, running from otsu import Hello should print abc. Then running Hello(5) will produce Hello 5. So your output will look like:

abc
Hello 5

I just ran this to confirm and it worked as expected.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.