Search code examples
pythonwindowsimportmodulescons

What are the mandatory steps to import a module?


I'm new to python and I face an issue:

I try to extend my SConstruct file and to import a module located in a sub-directory of my project.

Here is my SConstruct file:

import os, sys
sys.path.append(os.path.abspath(os.path.join('.', 'custom_dir')))
import mymodule

mymodule.foo()

Here is the mymodule.py file, located into a subdirectory named custom_dir:

def foo():
  print 'foo'

I also have a __init__.py file in my custom_dir directory.

When I execute scons:

  File ".\SConstruct", line 22, in <module>
    mymodule.foo()
AttributeError: 'module' object has no attribute 'foo'

If I do python.exe SConstruct I got the same result.

What am I doing wrong here ?


Solution

  • You should make sure that you are importing the correct module and not a different one with the same name somewhere else in your path

    try running your program with python.exe -v SConstruct

    or

    print mymodule.__file__ right before print mymodule.foo()