相关疑难解决方法(0)

Python中的循环(或循环)导入

如果两个模块相互导入会发生什么?

为了概括这个问题,Python中的循环导入怎么样?

python circular-dependency cyclic-reference

318
推荐指数
8
解决办法
13万
查看次数

AttributeError:'module'对象没有属性

我有两个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)

错误是什么意思?我如何解决它?

python attributeerror

178
推荐指数
9
解决办法
55万
查看次数

无法导入明确安装的模块

安装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 python-module mechanize

120
推荐指数
10
解决办法
25万
查看次数

如何避免Python中的循环导入?

我知道python中循环导入的问题已经出现过很多次了,我已经阅读过这些讨论了.在这些讨论中反复提出的评论是,循环导入是设计错误的标志,应重新组织代码以避免循环导入.

有人可以告诉我如何在这种情况下避免循环导入吗?:我有两个类,我希望每个类都有一个构造函数(方法),它接受另一个类的实例并返回该类的实例.

更具体地说,一个类是可变的,一个是不可变的.散列,比较等需要不可变类.可变类也需要做事.这与sets和frozensets或者列表和元组类似.

我可以将两个类定义放在同一个模块中.还有其他建议吗?

玩具示例是类A,其具有属性是列表,而类B具有属性是元组.然后类A有一个方法,它接受类B的实例并返回类A的实例(通过将元组转换为列表),类似地,类B有一个方法,它接受类A的实例并返回类B的实例(通过将列表转换为元组).

python import circular-dependency

98
推荐指数
3
解决办法
5万
查看次数

Python循环导入?

所以我收到了这个错误

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语句并且它有效吗?关于循环导入是否有一些不成文的规则?如何在调用堆栈中进一步使用相同的类?

python import circular-dependency

89
推荐指数
6
解决办法
7万
查看次数

Python中的循环导入依赖项

假设我有以下目录结构:

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

如何解决这个问题呢?

python dependencies circular-dependency python-import

72
推荐指数
4
解决办法
6万
查看次数

Django无法导入名称x

我收到一个我不明白的错误!

无法导入名称项

在我的模型中,我有物品.这些项目是行动所必需的.但其中一些项目对行动有影响:

项目

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)

python django django-models

47
推荐指数
4
解决办法
4万
查看次数

从脚本导入已安装的包会引发"AttributeError:module has no attribute"或"ImportError:无法导入名称"

我有一个名为的脚本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)

python exception python-module shadowing

43
推荐指数
2
解决办法
1万
查看次数

Pythonic方法解决循环导入语句?

我只是继承了一些让我感到不安的代码:有一个测试库,其中包含与我们网站上的网页相对应的类,每个网页类都有自动化该页面功能的方法.

有一些方法可以单击页面之间的链接,返回链接页面的类.这是一个简化的例子:

文件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)

python pageobjects

33
推荐指数
2
解决办法
2万
查看次数

ImportError:无法导入名称

我有两个文件app.pymod_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 flask

27
推荐指数
3
解决办法
15万
查看次数