【问题标题】:Understanding python imports了解python导入
【发布时间】:2009-05-23 01:04:11
【问题描述】:

在学习Django和Python的过程中。我无法理解这一点。

(示例说明:'helloworld' 是我的项目的名称。它有 1 个名为 'app' 的应用程序。)

from helloworld.views import *          # <<-- this works
from helloworld import views            # <<-- this doesn't work
from helloworld.app import views        # <<-- but this works.  why?

似乎第 2 行和第 3 行实际上是相同的。为什么like #2 不起作用?

编辑 -- 添加两个文件的来源。 您可能会从 Django Book 项目 (http://www.djangobook.com/en/2.0) 中认出这段代码

helloworld/views.py

​​>
from django.shortcuts import render_to_response
from django.http import HttpResponse, Http404
import datetime

def hello(request):
    return HttpResponse("Hello world")


def current_datetime(request):
    current_date = datetime.datetime.now()
    return render_to_response('current_datetime.html', locals())


def offset_datetime(request, offset):
    try:
        offset = int(offset)
    except ValueError:
        raise Http404()

    next_time = datetime.datetime.now() + datetime.timedelta(hours=offset)
    return render_to_response('offset_datetime.html', locals())

def display_meta(request):
    values = request.META.items()
    values.sort()
    path = request.path
    return render_to_response('metavalues.html', locals())

helloworld/app/views.py

​​>
from django.shortcuts import render_to_response

def search_form(request):
    return render_to_response('search_form.html')

def search(request):
    if 'q' in request.GET:
        message = 'You searched for: %r' % request.GET['q']
    else:
        message = 'You searched for nothing.'

    return render_to_response('search_results.html', locals())

【问题讨论】:

  • 发布helloworld的实际来源?
  • 导入 helloworld.views 作为视图是否有效?
  • 你有文件 helloworld/__init__.py 吗?
  • 是的,这里有 helloworld/__init__.py

标签: python django import


【解决方案1】:

Python 导入可以导入两种不同的东西:模块和对象。

import x

导入一个名为 x 的整个模块。

import x.y

导入一个名为 y 的模块,它是容器 x。你参考x.y。

然而,当你创建它时,你就创建了这个目录结构

x
    __init__.py
    y.py

当您添加到导入语句时,您确定要从模块中提取的特定对象并移动到全局命名空间中

import x # the module as a whole
x.a # Must pick items out of the module
x.b

from x import a, b # two things lifted out of the module
a # items are global
b

如果 helloworld 是一个包(一个目录,带有一个 __init__.py 文件),它通常不包含任何对象。

from x import y # isn't sensible
import x.y # importing a whole module.

有时,您将在__init__.py 文件中定义对象。

通常,使用“from module import x”从模块中挑选特定对象。

使用import module 导入整个模块。

【讨论】:

  • 所以“from helloworld import views”最好写成“import helloworld.views”?我明白了吗?
  • @T.斯通:正确。标准库示例为import os.path。
【解决方案2】:
from helloworld.views import *          # <<-- this works
from helloworld import views            # <<-- this doesn't work
from helloworld.app import views        # <<-- but this works.  why?

#2 和#3 不相同。

第二个从包helloworld 导入views。第三个从包helloworld.app中导入views,这是helloworld的子包。这意味着视图特定于您的 django 应用程序,而不是您的项目。如果您有单独的应用程序,您将如何从每个应用程序导入视图?您必须指定要从中导入的应用程序的名称,因此语法为 helloworld.app。

【讨论】:

    【解决方案3】:

    正如 sykora 所暗示的,helloworld 本身并不是一个包,所以#2 不起作用。您需要一个 helloworld.py,并进行适当设置。

    几天前我询问了有关导入的问题,这可能会有所帮助: Lay out import pathing in Python, straight and simple?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多