當我嘗試:
PROJECT_ROOT = os.path.dirname(__file__)
我得到的錯誤是這樣的:
Traceback (most recent call last):
PROJECT_ROOT = os.path.dirname(__ FILE__)錯誤
File "< stdin>", line 1, in <module>
NameError: name '__file__' is not defined
是否有人知道如何解決這個問題?
當我嘗試:
PROJECT_ROOT = os.path.dirname(__file__)
我得到的錯誤是這樣的:
Traceback (most recent call last):
PROJECT_ROOT = os.path.dirname(__ FILE__)錯誤
File "< stdin>", line 1, in <module>
NameError: name '__file__' is not defined
是否有人知道如何解決這個問題?
通過實際的模塊而不是Python的REPL來運行這段代碼。
如果您嘗試使用解釋器中的__file__
,則不會定義__file__
。這是預期的行爲。 __file__
是模塊的一個屬性。 Here
是關於這個問題的討論。
您可以通過執行此測試:
~$ echo "print __file__" > test.py
~$ python test.py
test.py
__file__
從模塊中工作的地方。
~$ python
Python 2.7.0+ (r27:82500, Sep 15 2010, 18:04:55)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print __file__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '__file__' is not defined
__file__
沒有定義
>>> import test
test.pyc
>>> print __file__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '__file__' is not defined
__file__
沒有定義
>>> print test.__file__
test.pyc
>>>
:
從解釋現在
__file__
是爲測試模塊
這解釋了我的一些事情,但我仍然不知道我的問題的解決方案是什麼。我不想閱讀python書,我只需要在一個文件夾中創建帶有兩個應用程序的小型django-powerade項目。它必須是兩個應用程序在一個文件夾中。但是,感謝一些方向。 – krzyhub 2011-04-05 19:37:29
定義請您settings.py試試這個:
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
我嘗試這顯然在bash: 'PROJECT_ROOT = os.path.dirname(__ FILE__) ' 現在我得到: '-bash:語法錯誤附近意想不到的標記'('' 我仍然不知道一些有關bash的重要事情,我想..是否有任何解決方案,您可以提供? – krzyhub 2011-04-05 17:18:26
你真的需要停止在bash提示符下編寫Python代碼。而當你在這裏,閱讀教程。 http://docs.python.org/tutorial/index.html – 2011-04-05 17:21:44
所以我有很多工作要做。感謝所有。 – krzyhub 2011-04-05 17:27:52