When i try:
PROJECT_ROOT = os.path.dirname(__file__)
i get error like this:
Traceback (mos开发者_Go百科t recent call last):
File "< stdin>", line 1, in <module>
NameError: name '__file__' is not defined
Does someone know how to fix this?
Run that line of code via an actual module instead of in the Python REPL.
If you are trying to use __file__
from your interpreter, no __file__
is defined. This is the intended behavior. __file__
is an attribute of modules. Here
is a discussion about the subject.
You can test by doing this:
~$ echo "print __file__" > test.py
~$ python test.py
test.py
__file__
works in from within modules.
Now from the interpreter:
~$ 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__
is not defined
>>> import test
test.pyc
>>> print __file__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '__file__' is not defined
__file__
is not defined
>>> print test.__file__
test.pyc
>>>
__file__
is defined for the test module
Please try this in your settings.py :
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
精彩评论