Tom Elliott
Tom Elliott

Reputation: 11

Module is imported but its functions can't be called

I have a module that I stored in the site-packages directory as suggested by several sources. I can import other packages installed via pip from that directory. Here is a minimal test case with Python 3.6.3.

THE MODULE MyStuff:

def Dummy()
    return(0)

TEST PROGRAM:

try:
   import MyStuff
except:
   print('Import Failed')    

print(MyStuff)
print(dir(MyStuff))

result = Dummy()
print(result)

I ran TEST PROGRAM with the Windows 10 command prompt as administrator with this result.(Blank lines added to improve readability.)

C:>python K:\PythonProjects\TestDummy.py

\Python\Python36-32\lib\site-packages\MyStuff.py'>

['Dummy', 'builtins', 'cached', 'doc', 'file', 'loader', >'name', 'package', 'spec']

Traceback (most recent call last): File "K:\PythonProjects\TestDummy.py", line 9, in module

result = Dummy()

NameError: name 'Dummy' is not defined

Upvotes: 0

Views: 29

Answers (2)

ForceBru
ForceBru

Reputation: 44838

Yes, Dummy is not defined. However, MyStuff.Dummy is.

This is what dir(MyStuff) tells you: the name Dummy is defined inside the MyStuff module. If you want this name to be visible in global scope, use from MyStuff import *.

Upvotes: 1

AstroMax
AstroMax

Reputation: 112

Shouldn't you be using Module.function() ?

Upvotes: 0

Related Questions