【问题标题】:Got "ImportError: No module named xxx" when trying to use ORM outside of Django尝试在 Django 之外使用 ORM 时出现“ImportError:没有名为 xxx 的模块”
【发布时间】:2013-01-23 06:32:52
【问题描述】:

亲爱的兄弟们, 我非常喜欢 Django ORM,所以我试图在 Django Project 本身之外使用它。但是当我尝试使用它时遇到了问题。 这是我在 Mac 上的文件结构:

testORM  
   |-- orm
        |-- __init__.py
        |-- models.py  
   |-- manage.py  
   |-- settings.py  
   |-- test.py

这是我非常简单的代码:

models.py:

from django.db import models

class test(models.Model):
    name = models.CharField(max_length=50)
    class Meta:
        db_table='test'
        app_label='orm'

manage.py:

import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'Portal',                      # Or path to database file if using sqlite3.
        'USER': 'postgres',                      # Not used with sqlite3.
        'PASSWORD': 'Cloud',                  # Not used with sqlite3.
        'HOST': 'localhost',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '5432',                      # Set to empty string for default. Not used with sqlite3.
    }
}

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# In a Windows environment this must be set to your system time zone.
TIME_ZONE = 'America/Chicago'

INSTALLED_APPS = (
    'orm',
)

test.py:

import sys
sys.path.append('/Users/wally/Documents/workspace/testORM')
sys.path.append('/Users/wally/Documents/workspace/testORM/orm')
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

from django.core.management import setup_environ
import settings
setup_environ(settings)

这是我尝试从 eclipse 执行 test.py 时得到的结果:

Traceback (most recent call last):
  File "/Users/wally/Documents/workspace/testORM/test.py", line 14, in <module>
    setup_environ(settings)
  File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 433, in setup_environ
    import_module(project_name)
  File "/Library/Python/2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
ImportError: No module named testORM

但是当我尝试执行时

python manage.py shell 
from django.db import connection
cursor = connection.cursor()

他们都可以从命令行正常工作!!!即使我创建模型的对象并保存数据也可以正常工作,通过 shell 一切正常。

  • 但我无法弄清楚为什么当我尝试执行时它不起作用 它们来自 test.py。我只是没有开始在 test.py 中做任何事情, 只需导入设置。
  • 我在网上搜索,有人通过将项目路径添加到“sys.path”来解决问题, 如您所见,我做到了,添加了硬编码的完整路径。但还是不行...
  • 有人遇到过这种情况吗?我真的很感激如果有人 有帮助。提前致谢。
  • 注意:我在我的 Mac(v10.8) 上使用 python2.7 和 Django1.4

【问题讨论】:

  • 尝试添加 sys.path.append('/Users/wally/Documents/workspace')?
  • 我刚刚试了,还是不行:(
  • 当你在命令行输入'python'时,脚本运行的python是否和你一样?
  • 是的,应该是同一个python,我查了一下,都是python 2.7。实际上,如果我通过命令行执行它:“python test.py”,它仍然无法正常工作,同样的错误:(无论如何,感谢您的帮助。
  • 你不应该使用setup_environ,因为它已经被弃用了一段时间。另请参阅 both good answers to this question 了解如何执行此操作。

标签: django orm


【解决方案1】:

我创建了所有这些文件来复制您的环境并且能够产生此错误。

当我在testORM 文件夹中创建一个空的__init__.py 文件时,python test.py 开始工作。

【讨论】:

  • 哦,是的,当我添加 __init__.py unser "testORM" 文件夹时,一切终于奏效了。非常感谢psjinx。错过那个文件我是多么愚蠢......
  • 问题是您不应该使用setup_environ,因为它会尝试导入您的项目路径,因此您会收到错误消息。我建议不要将您的项目文件夹设置为 python 模块,因为一旦您的项目文件夹名称更改,您将遇到导入问题。
  • +1 表示project folder should not be a python module。这不是一个好的做法。
猜你喜欢
  • 1970-01-01
  • 2014-12-09
  • 1970-01-01
  • 2012-12-09
  • 2018-03-04
  • 2013-01-13
  • 1970-01-01
  • 2012-10-21
相关资源
最近更新 更多