了解python进口 [英] Understanding python imports

查看:130
本文介绍了了解python进口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



(示例注意:helloworld是我的项目的名称,它有一个名为app的应用程序。来自helloworld.views import的#

 #<<  - 这个工程
来自helloworld导入视图#<< - 这不工作
从helloworld.app导入视图#<< - 但这是有效的。为什么?

似乎行#2和#3几乎相同。为什么#2不工作?



编辑 - 添加了两个文件的来源。
您可能会识别此代码从Django Book项目( http://www.djangobook.com/en/2.0



helloworld / views.py



 从django.shortcuts导入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)
除了ValueError:
raise Http404()

next_time = datetime.datetime.now()+ datetime.timedelta(hours = offset)
返回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())



helloorld / app / views.py



  from django.shortcuts import render_to_response 

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

def search(request):
如果'q'在request.GET:
message ='你搜索对于:%r'%request.GET ['q']
else:
message ='你没有搜索任何东西'

return render_to_response('search_results.html'本地人())


解决方案

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

  import x 

导入名为 x的整个模块

  import xy 

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



创建时,您创建了此目录结构

  x 
__init__.py
y.py

添加到导入语句,您识别要从模块中拉出的特定对象,并移动到全局命名空间

  import x#整个模块
xa#必须从模块中挑选项目
xb
$ b从x导入a,b#将两个东西从模块中提取
a#items是全局
b

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

 从x import y#isn 't sensible 
import xy#import a整个模块。

有时候,你将有一个定义在 __ init __。py 文件。



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



使用导入模块导入整个模块。


In the process of learning Django and Python. I can't make sense of this.

(Example Notes:'helloworld' is the name of my project. It has 1 app called 'app'.)

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

It seems like line #2 and #3 are practically the same. Why does like #2 not work?

Edit -- Added the source of the two files. You might recognize this code from the Django Book project (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())

解决方案

Python imports can import two different kinds of things: modules and objects.

import x

Imports an entire module named x.

import x.y

Imports a module named y and it's container x. You refer to x.y.

When you created it, however, you created this directory structure

x
    __init__.py
    y.py

When you add to the import statement, you identify specific objects to pull from the module and move into the global namespace

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

If helloworld is a package (a directory, with an __init__.py file), it typically doesn't contain any objects.

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

Sometimes, you will have objects defined in the __init__.py file.

Generally, use "from module import x" to pick specific objects out of a module.

Use import module to import an entire module.

这篇关于了解python进口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆