I am following the instructions on Heroku for using Memcahier with Python.
When trying to use the 'mc' variable, which is set in settings.py, in another file I get the following error:
Exception Value: name 'mc' is not defined
I have tried importing settings.py into the file I wish to use the 'mc' variable but I get another error:
'Settings' object has no attribute 'mc'
How can I access this mc variable outside of the settings file?
答案 0 :(得分:1)
This is probably an importing issue.
You need to access mc
via settings.mc
, because, provided you imported it using import settings
at the beginning of the file, it is not included in your current namespace, but in a seperate one called "settings".
If you whish to import it directly into your current namespace, use
from settings import *
instead.
This only works, when your own file is in the same directory as settings.py, or if settings.py is in a directory known to Python. (See PYTHONPATH)
If settings.py is in another Directory, you could for example Import it using the whole path
围绕它时,为什么我的图像会消失顺便说一句,浏览Python文档永远不会伤害:see this
此外,请确保为您的设置模块使用正确的大小写。如果将设置文件导入为"设置"使用小写字母,然后你必须像所有地方一样访问它,因为 Python区分大小写