导入模块==从模块导入*? [英] import module == from module import *?

查看:104
本文介绍了导入模块==从模块导入*?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Django教程中遇到问题,所以我在这里提出了一个问题。 a>没有人知道答案,但我最终在 Robert 的帮助下找到了答案。 Python似乎正在处理 import datetime 与datetime import * 中的相同。



工作代码:

  import datetime 
from django.db import models

class Poll(models.Model):
question = models.CharField(max_length = 200)
pub_date = models.DateTimeField('date published')
def __unicode __(self)
return self.question

def was_published_today(self):
return self.pub_date.date()== date.today()

不工作代码:(唯一的区别是import语句和最后一行。)



来自django.db导入模型的$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $($(((((((((((((( max_length = 200)
pub_date = models.DateTimeField('date published')
def __unicode __(self):
return self.question

def was_published_today(self) :
return(self.pub_date()== datetime.date.today())

<强>编辑:我想我还不够清楚。该代码产生完全相同的追溯,最后一行为 return(self.pub_date.date()== datetime.date.today())我原来忘记添加 .date()我所问的错误。



由不正常的代码产生的追溯:

 追溯(最近的最后一次呼叫):
文件< console>,第1行,< module>
文件/myDir/mySite/polls/models.py,第11行,was_published_today
return(self.pub_date()== datetime.date.today())
TypeError: 'datetime.datetime'对象不可调用

为什么这样做?



我的问题是关于忘记 .date()。我的问题是:为什么我的命名空间中的datetime没有我使用datetime import * 中的



注意第一个问题它正在做什么。这个问题询问为什么。



更新:突然间起作用。使用 datetime.date.today() AND date.today()。我的问题依然存在,为什么 date.today()工作?似乎datetime是在我的本地命名空间没有我放在那里。为什么?

解决方案

正如我们在评论中讨论的那样,问题不在于代码,而是更新源的方式。 python在 sys.modules 中缓存模块。您可以使用 reload 函数重新加载单个模块,但是对于许多更改,最好重新加载整个shell。在许多情况下,它似乎已经传播了这些更改,因为错误消息似乎已经改变,这是因为python不缓存文件的源代码,所以当它引用代码时,它会显示最新的版本。希望现在,您可以更好地应用其他答案。


I had a problem with the Django tutorial so I asked a question here. No-one knew the answer, but I eventually figured it out with help from Robert. Python seems to be treating import datetime the same as from datetime import *.

Working code:

import datetime
from django.db import models

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    def __unicode__(self):
        return self.question

    def was_published_today(self):
        return self.pub_date.date() == date.today()

Not working code: (The only differences are the import statements and the last line.)

from django.db import models
import datetime

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    def __unicode__(self):
        return self.question

    def was_published_today(self):
        return (self.pub_date() == datetime.date.today())

EDIT: I guess I wasn't clear enough. The code produces the exact same traceback with the last line being return (self.pub_date.date() == datetime.date.today()) Me originally forgetting to add .date() is NOT the error I'm asking about.

The traceback produced by the not working code:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/myDir/mySite/polls/models.py", line 11, in was_published_today
    return (self.pub_date() == datetime.date.today())
TypeError: 'datetime.datetime' object is not callable

Why on earth is it doing this?

My question is NOT about forgetting .date(). My question is: Why is datetime in my namespace without me using from datetime import *.

Note: The first question asked what it was doing. This question asks why.

UPDATE: Suddenly it works. With datetime.date.today() AND date.today(). My question remains though, why does date.today() work? It seems datetime is in my local namespace without me putting there. Why?

解决方案

As we discussed in the comments, the problem is not with the code, but the way you are updating the source. python caches modules in sys.modules. You can reload individual modules using the reload function, but for many changes it's best to reload the entire shell. In many cases it looked as though the changes had propagated because the error messages seemed to have changed, this is because python doesn't cache the source code of the file, so when it references code, it shows you the newest version. Hopefully now, you can apply the other answers with more success.

这篇关于导入模块==从模块导入*?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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