Sam Weisenthal
Sam Weisenthal

Reputation: 2951

Import function from other file in python

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?

Upvotes: 1

Views: 122

Answers (1)

s16h
s16h

Reputation: 4855

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.

Upvotes: 3

Related Questions