我有四个不同的文件:main,vector,entity和physics.我不会发布所有代码,只发布导入,因为我认为这就是错误所在.(如果你愿意,我可以发布更多)
主要:
import time
from entity import Ent
from vector import Vect
#the rest just creates an entity and prints the result of movement
Run Code Online (Sandbox Code Playgroud)
实体:
from vector import Vect
from physics import Physics
class Ent:
#holds vector information and id
def tick(self, dt):
#this is where physics changes the velocity and position vectors
Run Code Online (Sandbox Code Playgroud)
向量:
from math import *
class Vect:
#holds i, j, k, and does vector math
Run Code Online (Sandbox Code Playgroud)
物理:
from entity import Ent
class Physics:
#physics class gets an entity …Run Code Online (Sandbox Code Playgroud) 所以我收到了这个错误
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
如何解决这个问题呢?
我有一个Bottle webserver模块,其中包含以下行:
from foobar.formtools import auto_process_form_insert
Run Code Online (Sandbox Code Playgroud)
该foobar.formtools模块包含以下行:
from foobar.webserver import redirect, redirect_back
Run Code Online (Sandbox Code Playgroud)
当然,两者都会导致以下错误:
ImportError:无法导入名称auto_process_form_insert
ImportError:无法导入名称重定向
事实上,在Python中,两个模块不能互相导入,并且所有模块导入必须是分层的,或者我做错了什么?或者,是否有一种解决方法是将所有这些不错的功能放在新模块中?
我有一个名为的脚本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) 假设我们有两个具有循环依赖关系的模块:
# a.py
import b
def f(): return b.y
x = 42
Run Code Online (Sandbox Code Playgroud)
# b.py
import a
def g(): return a.x
y = 43
Run Code Online (Sandbox Code Playgroud)
这两个模块在目录pkg中为空__init__.py.导入pkg.a或pkg.b正常工作,如本答案中所述.如果我将导入更改为相对导入
from . import b
Run Code Online (Sandbox Code Playgroud)
我ImportError试图导入其中一个模块时得到一个:
>>> import pkg.a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pkg/a.py", line 1, in <module>
from . import b
File "pkg/b.py", line 1, in <module>
from . import a
ImportError: cannot import name a …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) 我可以在我的代码中使用openpyxl作为导入.但是,当我尝试执行以下操作时:
from openpyxl.cell import get_column_letter
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
ImportError: cannot import name get_column_letter
Run Code Online (Sandbox Code Playgroud)
我正在使用python 2.7.我用它安装了它easy_install.尝试搜索此问题但找不到与之相关的任何内容.
好的我有两个模块,每个模块都包含一个类,问题是它们的类互相引用.
让我们说例如我有一个房间模块和一个包含CRoom和CPerson的人员模块.
CRoom类包含有关房间的信息,以及房间中每个人的CPerson列表.
然而,CPerson类有时需要将CRoom类用于它的房间,例如找到门,或者看看房间里还有谁.
问题是两个模块互相导入我只是得到一个导入错误,其中第二次导入:(
在c ++中,我可以通过仅包含头来解决这个问题,因为在这两种情况下类都只有指向另一个类的指针,前向声明就足以满足标题,例如:
class CPerson;//forward declare
class CRoom
{
std::set<CPerson*> People;
...
Run Code Online (Sandbox Code Playgroud)
反正有没有在python中执行此操作,除了将两个类放在同一个模块或类似的东西?
编辑:添加了使用上面的类显示问题的python示例
错误:
回溯(最近一次调用最后一次):
文件"C:\ Projects\python\test\main.py",第1行,
从房间导入CRoom
文件"C:\ Projects\python\test\room.py",第1行,
来自人员导入CPerson
文件"C:\ Projects\python\test\person.py",第1行,
从房间导入 CRoom
ImportError:无法导入名称
CRoom room.py
from person import CPerson
class CRoom:
def __init__(Self):
Self.People = {}
Self.NextId = 0
def AddPerson(Self, FirstName, SecondName, Gender):
Id = Self.NextId
Self.NextId += 1#
Person = CPerson(FirstName,SecondName,Gender,Id)
Self.People[Id] = Person
return Person
def FindDoorAndLeave(Self, PersonId):
del Self.People[PeopleId]
Run Code Online (Sandbox Code Playgroud)
person.py
from room import CRoom
class CPerson:
def __init__(Self, …Run Code Online (Sandbox Code Playgroud) 所以我在同一个项目中运行了2个应用程序.
我的文件结构如下:
/project_codebase
/project
__init.py
settings.py
urls.py
wsgi.py
...
/app1
...
/app2
...
manage.py
Run Code Online (Sandbox Code Playgroud)
所以,我出于某些奇怪的原因为我的基目录设置了一个不同的名称(也就是说,它以codebase结尾).希望这不是问题.
在我的settings.py中,我有这个:
INSTALLED_APPS = [
...
'app1',
'app2',
]
Run Code Online (Sandbox Code Playgroud)
好的,所以在我的models.py(来自app2)中,我可以轻松地从app1导入模型from app1.models import *,但是,当我from app2.models import *在models.py(来自app1)中使用时,我得到一个ImportError.
对此有何解决方案?
python ×10
importerror ×2
module ×2
cyclic ×1
dependencies ×1
django ×1
django-apps ×1
exception ×1
import ×1
openpyxl ×1
pageobjects ×1
python-2.7 ×1
shadowing ×1