我是新來的蟒蛇,我面臨一個問題:什麼是導入模塊的強制步驟?
我試圖擴展我的SConstruct
文件,並導入位於我的項目的子目錄中的模塊。
這裏是我的SConstruct
文件:
import os, sys
sys.path.append(os.path.abspath(os.path.join('.', 'custom_dir')))
import mymodule
mymodule.foo()
這裏是mymodule.py
文件,定位到一個名爲custom_dir
子目錄:
def foo():
print 'foo'
我也有一個__init__.py
文件在我custom_dir
目錄。
當我執行scons
:
File ".\SConstruct", line 22, in <module>
mymodule.foo()
AttributeError: 'module' object has no attribute 'foo'
如果我做python.exe SConstruct
我得到了相同的結果。
我在這裏做錯了什麼?
'def foo()'後面需要冒號。如果你從Python的命令行輸入mymodule,然後輸入mymodule .__ dict__,會發生什麼? – 2010-09-09 10:33:14
@Mike:如果我在與我的模塊相同的目錄中啓動命令行工具,我可以從命令行成功加載和使用模塊。 – ereOn 2010-09-13 08:33:37
實際上,我在想'mymodule .__ dict__'的輸出是什麼,但現在已經沒有意義了。 – 2010-09-13 14:38:58