在脚本中更新django db,我得到ImportError取决于脚本的路径

时间:2013-10-03 21:48:02

标签: python django

我想要一个更新django数据库的python脚本。我有一个在django项目目录的顶层工作正常,但当它在子目录中时失败并带有ImportError。

虽然我的项目比这更复杂,但这是我的项目目录的简化示例:

    myproj/
    ├── settings.py
    ├── manage.py
    ├── bin/
    ├── myapp/
    │   ├── __init__.py
    │   ├── models.py
    │   ├── templates/
    │   ├── urls.py
    │   ├── views.py
    └── myproj.db

包myproj在python路径中,在settings.py中,INSTALLED_APPS元组列出'myapp'。

现在,如果我有一个像这样的简单python脚本

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproj.settings'

from django.contrib.auth.models import User

print User.objects.filter(username='foo').count()

我将此test.py命名为myproj /的顶部,然后从myproj / run内部     python test.py 一切正常(例如,如果我有一个名为'foo'的用户,则打印1)。

如果我只是将test.py移动到myproj / bin /并从myproj / bin中移动     python test.py 我得到了像这样的一连串错误

    Traceback (most recent call last):
      File "test.py", line 6, in <module>
        print User.objects.filter(username='foo').count() 
      File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 155, in filter
        return self.get_query_set().filter(*args, **kwargs)
      File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 669, in filter
        return self._filter_or_exclude(False, *args, **kwargs)
      File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 687, in _filter_or_exclude
        clone.query.add_q(Q(*args, **kwargs))
      File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1271, in add_q
        can_reuse=used_aliases, force_having=force_having)
      File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1139, in add_filter
        process_extras=process_extras)
      File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1325, in setup_joins
        field, model, direct, m2m = opts.get_field_by_name(name)
      File "/usr/local/lib/python2.7/dist-packages/django/db/models/options.py", line 351, in get_field_by_name
        cache = self.init_name_map()
      File "/usr/local/lib/python2.7/dist-packages/django/db/models/options.py", line 380, in init_name_map
        for f, model in self.get_all_related_m2m_objects_with_model():
      File "/usr/local/lib/python2.7/dist-packages/django/db/models/options.py", line 469, in get_all_related_m2m_objects_with_model
        cache = self._fill_related_many_to_many_cache()
      File "/usr/local/lib/python2.7/dist-packages/django/db/models/options.py", line 483, in _fill_related_many_to_many_cache
        for klass in get_models(only_installed=False):
      File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 197, in get_models
        self._populate()
      File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 72, in _populate
        self.load_app(app_name, True)
      File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 94, in load_app
        app_module = import_module(app_name)
      File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 35, in import_module
        __import__(name)
    ImportError: No module named myapp

我不确定如何解决这些错误。他们并没有真正说清楚出了什么问题。据推测,django正在使用自己的导入函数,可能它们受到python脚本和settings.py文件的相对位置的影响。

任何见解?

1 个答案:

答案 0 :(得分:0)

问题在于这一行:os.environ['DJANGO_SETTINGS_MODULE'] = 'myproj.settings'

  • 当您的脚本在myproj/之外时,您的PYTHON_PATH在myproj/之外开始,因此myproj是有效的包。

  • 当您的脚本位于myproj/内时,您的PYTHON_PATH将在myproj/内启动,因此myproj不再是有效的包。

修复方法是从脚本中明确设置PYTHON_PATH。

<强>无论其

你应该做的是将你的脚本变成management command,这样你就可以运行python manage.py myscript并确保Django已经完成了所有必要的初始化并在之前检查它运行你的代码。