python2 python3 import,从python2到python3的导入处理/模块的更改?
我試圖遵循與SQLAlchemy相關的this previous question中顯示的設計模式,并打算在多個文件中共享一個公共的基本實例。代碼完全可以在python2和python3上運行。在
但是,當我移動文件a.py、b.py、c.py和基準.py在一個模塊(稱為model)中,添加必要的uinit_uy.py文件,它繼續在python2上工作,但隨后在python3上生成一個錯誤(詳細信息如下)。在
我有以下文件:
型號/基準.py在from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
型號/a.py
^{pr2}$
型號/b.pyfrom sqlalchemy import *
from base import Base
class B(Base):
__tablename__ = "B"
id = Column(Integer, primary_key=True)
A_id = Column(Integer, ForeignKey("A.id"))
型號/c.pyfrom sqlalchemy import *
from base import Base
class C(Base):
__tablename__ = "C"
id = Column(Integer, primary_key=True)
A_id = Column(Integer, ForeignKey("A.id"))
型號/初始型號
(空的)
在主.py在from sqlalchemy import create_engine
from sqlalchemy.orm import relationship, backref, sessionmaker
from model import base
from model import a
from model import b
from model import c
engine = create_engine("sqlite:///:memory:")
base.Base.metadata.create_all(engine, checkfirst=True)
Session = sessionmaker(bind=engine)
session = Session()
a1 = a.A()
b1 = b.B()
b2 = b.B()
c1 = c.C()
c2 = c.C()
a1.Bs.append(b1)
a1.Bs.append(b2)
a1.Cs.append(c1)
a1.Cs.append(c2)
session.add(a1)
session.commit()
Python2號作品:$ python main.py ; echo $?
0
python3出錯:$ python3 main.py ; echo $?
Traceback (most recent call last):
File "main.py", line 7, in
from model import a
File "/home/shale/code/py/try/model/a.py", line 2, in
from base import Base
ImportError: No module named base
1
我最終解決了這個問題基準.py但有人知道為什么這會在python3中產生錯誤,而在python2中卻不會產生錯誤?首先是什么變化造成的?在
總結
以上是生活随笔為你收集整理的python2 python3 import,从python2到python3的导入处理/模块的更改?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php如何进行错误处理,php如何自定义
- 下一篇: php rsa加密实例,关于PHP语言的