如果两个模块相互导入会发生什么?
为了概括这个问题,Python中的循环导入怎么样?
我有两个python模块:
a.py
import b
def hello():
print "hello"
print "a.py"
print hello()
print b.hi()
Run Code Online (Sandbox Code Playgroud)
b.py
import a
def hi():
print "hi"
Run Code Online (Sandbox Code Playgroud)
当我跑步时a.py,我得到:
AttributeError: 'module' object has no attribute 'hi'
Run Code Online (Sandbox Code Playgroud)
错误是什么意思?我如何解决它?
安装mechanize之后,我似乎无法导入它.
我尝试从pip,easy_install和via python setup.py install这个repo安装:https://github.com/abielr/mechanize.所有这一切都无济于事,因为每次我输入我的Python互动时,我得到:
Python 2.7.3 (default, Aug 1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mechanize
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named mechanize
>>>
Run Code Online (Sandbox Code Playgroud)
我之前运行的安装报告说它们已经成功完成,所以我希望导入能够正常工作.可能导致此错误的原因是什么?
我知道python中循环导入的问题已经出现过很多次了,我已经阅读过这些讨论了.在这些讨论中反复提出的评论是,循环导入是设计错误的标志,应重新组织代码以避免循环导入.
有人可以告诉我如何在这种情况下避免循环导入吗?:我有两个类,我希望每个类都有一个构造函数(方法),它接受另一个类的实例并返回该类的实例.
更具体地说,一个类是可变的,一个是不可变的.散列,比较等需要不可变类.可变类也需要做事.这与sets和frozensets或者列表和元组类似.
我可以将两个类定义放在同一个模块中.还有其他建议吗?
玩具示例是类A,其具有属性是列表,而类B具有属性是元组.然后类A有一个方法,它接受类B的实例并返回类A的实例(通过将元组转换为列表),类似地,类B有一个方法,它接受类A的实例并返回类B的实例(通过将列表转换为元组).
所以我收到了这个错误
Traceback (most recent call last):
File "/Users/alex/dev/runswift/utils/sim2014/simulator.py", line 3, in <module>
from world import World
File "/Users/alex/dev/runswift/utils/sim2014/world.py", line 2, in <module>
from entities.field import Field
File "/Users/alex/dev/runswift/utils/sim2014/entities/field.py", line 2, in <module>
from entities.goal import Goal
File "/Users/alex/dev/runswift/utils/sim2014/entities/goal.py", line 2, in <module>
from entities.post import Post
File "/Users/alex/dev/runswift/utils/sim2014/entities/post.py", line 4, in <module>
from physics import PostBody
File "/Users/alex/dev/runswift/utils/sim2014/physics.py", line 21, in <module>
from entities.post import Post
ImportError: cannot import name Post
Run Code Online (Sandbox Code Playgroud)
你可以看到我进一步使用相同的import语句并且它有效吗?关于循环导入是否有一些不成文的规则?如何在调用堆栈中进一步使用相同的类?
假设我有以下目录结构:
a\
__init__.py
b\
__init__.py
c\
__init__.py
c_file.py
d\
__init__.py
d_file.py
Run Code Online (Sandbox Code Playgroud)
在a包中__init__.py,c导入包.但是c_file.py进口a.b.d.
程序失败,说尝试导入b时不存在.(它确实不存在,因为我们正在进口它.)c_file.pya.b.d
如何解决这个问题呢?
我收到一个我不明白的错误!
无法导入名称项
在我的模型中,我有物品.这些项目是行动所必需的.但其中一些项目对行动有影响:
项目
from django.db import models
from effects.models import Effect
class Type(models.Model):
name = models.CharField(max_length=200)
def __unicode__(self):
return self.name
class Item(models.Model):
name = models.CharField(max_length=200)
description = models.CharField(max_length=200)
type = models.ForeignKey(Type)
quality = models.IntegerField()
effects = models.ManyToManyField(Effect,through='ItemEffect',blank=True)
item_requirement = models.ManyToManyField('self',through='ItemCraft',symmetrical=False,blank=True)
points = models.IntegerField()
def __unicode__(self):
return self.name
class Food(Item):
ap = models.IntegerField()
class Tool(Item):
durability = models.IntegerField()
[....]
class ItemEffect(models.Model):
item = models.ForeignKey(Item)
effect = models.ForeignKey(Effect)
def __unicode__(self):
return self.item.name+':'+str.lower(self.effect.name)
class Meta:
verbose_name_plural = 'items effects'
class ItemCraft(models.Model):
item …Run Code Online (Sandbox Code Playgroud) 我有一个名为的脚本requests.py导入请求包.该脚本无法访问包中的属性,也无法导入它们.为什么这不起作用,我该如何解决?
以下代码提出了一个问题AttributeError.
import requests
res = requests.get('http://www.google.ca')
print(res)
Run Code Online (Sandbox Code Playgroud)
Traceback (most recent call last):
File "/Users/me/dev/rough/requests.py", line 1, in <module>
import requests
File "/Users/me/dev/rough/requests.py", line 3, in <module>
requests.get('http://www.google.ca')
AttributeError: module 'requests' has no attribute 'get'
Run Code Online (Sandbox Code Playgroud)
以下代码提出了一个问题ImportError.
from requests import get
res = get('http://www.google.ca')
print(res)
Run Code Online (Sandbox Code Playgroud)
Traceback (most recent call last):
File "requests.py", line 1, in <module>
from requests import get
File "/Users/me/dev/rough/requests.py", line 1, in <module>
from requests import get
ImportError: cannot import name 'get' …Run Code Online (Sandbox Code Playgroud) 我只是继承了一些让我感到不安的代码:有一个测试库,其中包含与我们网站上的网页相对应的类,每个网页类都有自动化该页面功能的方法.
有一些方法可以单击页面之间的链接,返回链接页面的类.这是一个简化的例子:
文件homePageLib.py:
class HomePage(object):
def clickCalendarLink(self):
# Click page2 link which navigates browswer to page2
print "Click Calendar link"
# Then returns the page2 object
from calendarLib import CalendarPage
return CalendarPage()
Run Code Online (Sandbox Code Playgroud)
文件calendarLib.py:
class CalendarPage(object):
def clickHomePageLink(self):
# Click page1 link which navigates browswer to page1
print "Click Home Page link"
# Then return the page2 object
from homePageLib import HomePage
return HomePage()
Run Code Online (Sandbox Code Playgroud)
然后,这允许脚本文件单击页面并将该对象作为该方法的返回值,这意味着脚本作者不必在浏览站点时保持实例化新页面.(我觉得这是一个奇怪的设计,但我不能完全理解为什么,除此之外,有一个名为'clickSomeLink'的方法并返回结果页面的对象似乎很奇怪.)
以下脚本说明了脚本如何在站点中导航:(我插入print page以显示页面对象如何更改)
脚本文件:
from homePageLib import HomePage
page = HomePage()
print page
page = …Run Code Online (Sandbox Code Playgroud) 我有两个文件app.py和mod_login.py
app.py
from flask import Flask
from mod_login import mod_login
app = Flask(__name__)
app.config.update(
USERNAME='admin',
PASSWORD='default'
)
Run Code Online (Sandbox Code Playgroud)
mod_login.py
# coding: utf8
from flask import Blueprint, render_template, redirect, session, url_for, request
from functools import wraps
from app import app
mod_login = Blueprint('mod_login', __name__, template_folder='templates')
Run Code Online (Sandbox Code Playgroud)
并且python返回此错误:
Traceback (most recent call last):
File "app.py", line 2, in <module>
from mod_login import mod_login
File "mod_login.py", line 5, in <module>
from app import app
File "app.py", line 2, in <module>
from mod_login …Run Code Online (Sandbox Code Playgroud) python ×10
import ×2
dependencies ×1
django ×1
exception ×1
flask ×1
mechanize ×1
pageobjects ×1
shadowing ×1