我很難設置一個簡單的測試。Python,導入模塊進行測試
我的項目結構如下:
project:
models:
__init__.py
user.py
constants.py
test:
test.py
我想測試user.py
PY運行test.py
。
user.py
from sqlalchemy import Column, Integer, String, Text
from sqlalchemy.orm import relationship
from .models.constants import *
from .models import Base
class User(Base):
__tablename__ = 'users'
uid = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String, nullable=False)
email = Column(String, nullable=False)
picPath = Column(String, unique=True)
description = Column(Text)
def __repr__(self):
return "<User(uid=%s, name=%s)>" %(self.uid, self.name)
test.py
from ..user import User, Group
def _TestUser():
TEST_DB_URI = "postgresql://project:[email protected]:5432/projectdbtest"
SessionMaker = sessionmaker()
engine = create_engine(TEST_DB_URI)
SessionMaker.configure(bind=engine)
session = SessionMaker()
user = User("test subject", "[email protected]", "~/testsubject.jpg", "I am a test subject")
session.add(user)
session.commit()
然而,當我運行python3 -m test.py
我收到以下錯誤:
關於SystemError:父模塊'不裝,無法執行相對進口
我想我可能有一個dd模塊包到python路徑?
看一看[這裏](http://stackoverflow.com/questions/16981921/relative-imports-in-python-3) –