我的django应用程序之外有一个文件夹,我想用它从django中提取一些数据,以便在一些外部脚本中使用。
然而,当我运行安装程序时,我得到以下错误:
window.termList = {};
// Then, in a loop or whatever
window.termList[_idContainer] = new Terminal({cursorBlink: true});
如何在django文件夹之外引用设置模块?
由于
修改 我有多个外部脚本使用来自django DB的信息和我在django中已有的函数,我定期用cron运行这些脚本。
我想将这些脚本从我的django应用程序的根目录移到子目录中并从那里运行它们,但现在我无法导入设置。
旧结构:
[root@localhost cronjobs]# python
Python 2.7.5 (default, Nov 6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ.setdefault("DJANGO_SETTINGS_MODULE", "infternal.settings")
'infternal.settings'
>>> from django.conf import settings
>>> import django
>>> django.setup()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.7/site-packages/django/__init__.py", line 22, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/usr/lib64/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
self._setup(name)
File "/usr/lib64/python2.7/site-packages/django/conf/__init__.py", line 41, in _setup
self._wrapped = Settings(settings_module)
File "/usr/lib64/python2.7/site-packages/django/conf/__init__.py", line 97, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportE
脚本在这里运行正常
新结构:
APP
-Site
-Site
-APP
--settings.py
manage.py
script.py
脚本无法再找到infternal.settings
答案 0 :(得分:2)
你可以使用这个我常用来从django外部运行脚本的函数。
def __setup_django(root_path, settings):
import os
import django
os.chdir(root_path)
# Django settings
sys.path.append(root_path)
os.environ['DJANGO_SETTINGS_MODULE'] = settings
django.setup()
PROJECT_PATH = "/home/a/b/c/someproject"
PROJECT_SETTING = "someproject.settings.local"
__setup_django(PROJECT_PATH, PROJECT_SETTING)
答案 1 :(得分:0)
运行脚本时只需设置PYTHONPATH
。
PYTHONPATH=/app DJANGO_SETTINGS_MODULE=app.settings /script.py
PYTHONPATH
与PATH
类似,这是python用于导入模块的目录的冒号分隔列表。