views:

61

answers:

2

Hi,

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 ?

+1  A: 

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()

gnibbler
I had a directory with the same name than the module which was conflicting. Many thanks for this debugging technique ;)
ereOn
+1  A: 

Beware path manipulation; trouble will find you.

Take a look at http://docs.python.org/tutorial/modules.html

I've set up what I think you are trying to do below. This should work.

File structure:

/SConstruct.py
/custom_dir/
/custom_dir/__init__.py
/custom_dir/mymodule.py

/custom_dir/_init_.py is blank

/custom_dir/mymodule.py:

def foo():
    print 'foo'

/SConstruct.py:

import custom_dir.mymodule as mymodule

mymodule.foo()
ChrisAdams
I get this: `AttributeError: 'module' object has no attribute 'foo': File "C:\workbench\scons_test\SConstruct", line 18: mymodule.foo()`
ereOn