2013-11-01 108 views
1

我是python新手,所以這可能是我剛纔錯過的東西... 我只是想運行一個文件,調用另一個文件。python typeerror'module'object is not callable

我有一個像myfile.py文件:

#!/usr/bin/python 

import another_file 

things = ... """ some code """ 

def mystuff(text, th=things): 
    return another_def(text, th) 

another_file」可以編譯/自身運行良好,並具有高清「another_def」和變量「th」(這些只是示例名稱。 ..)

所以我在命令行中運行python,然後嘗試:

>>> import myfile 
>>> t = myfile.mystuff('some text') 

,我得到的錯誤:

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "myfile.py", line 18, in mystuff 
    return another_def(text, th) 
TypeError: 'module' object is not callable 

我試圖import another_file即使是在myfile.py但似乎沒有任何區別。

如果這有什麼差別,我想:

print myfile 
<module 'myfile' from 'myfile.py'> 
print myfile.mystuff 
<function mystuff at 0x7fcf178d0320> 

,所以我認爲,如果能找到的文件和功能,問題是它試圖如何調用其他文件....也許。任何幫助感謝!

+0

你能發佈方法代碼'myfile.mystuff'這似乎是有問題? –

+2

如果您更改了名稱以保護無辜者,您已經很難回答這個問題。 「模塊不可調用」錯誤意味着您正在嘗試使用模塊,就像它是一個函數一樣。 – doctorlove

+0

@doctorlove我改變了名字,希望讓問題看起來更容易(沒有效果,但是哦) - 實際上你是對的,因爲在原始代碼中,'another_file'和'another_def'具有相同的名稱。感嘆。感謝 – dgBP

回答

3

我不太確定爲什麼你得到一個TypeError(有可能更給它比你顯示什麼),但如果你想從another_file訪問功能,那麼你應該做的:

return another_file.another_def(text, th) 
+0

似乎這樣做!我認爲通過導入文件,它會拿起函數名稱... * facepalm * – dgBP

+0

@dgBP很好聽! :) – TerryA

1

你可以做到這一點使用所謂的野生進口:

從other_file可以導入*

而且這樣你可以訪問該文件中的所有對象和功能。除非當然,你已經定義了

__all__ 

列表限制什麼可以導出。

例子:

#some_file.py 

a = 3; b = 4 
__all__ = ['a'] 
現在與野生進口

from some_file import * 

你只看到 'A'

相關問題