将SQLAlchemy导入我的项目

时间:2014-07-18 22:16:08

标签: python python-2.7 sqlite sqlalchemy

环境:

  • Python 2.7.5
  • SQLAlchemy 0.9.6

我做了什么:

我想出于不同的原因将SQLAlchemy源包含到我的项目中。主要原因是以后为我的用户避免任何虚拟环境。

好的,我的结构如下:

$ tree . -L 2
myapp
├── libs
│   ├── ...
│   └── sqlalchemy
├── myapp
│   └── ...
└── MyApp.py

好的,我将libs路径添加到系统路径。

### MyApp.py
# ...
BASE_DIR = os.path.dirname(__file__)
LIBRARY_DIR = os.path.join(BASE_DIR, 'libs')
sys.path.append(LIBRARY_DIR)
# ...

我现在尝试从libs.sqlalchemy.orm

导入
from libs.sqlalchemy.orm import relationship

我收到错误

Traceback (most recent call last):
  File "/.../myapp/MyApp.py", line 13, in <module>
    from libs.sqlalchemy.orm import relationship
  File "/.../myapp/libs/sqlalchemy/orm/__init__.py", line 69, in <module>
    from . import strategies as _strategies
  File "/.../myapp/libs/sqlalchemy/orm/strategies.py", line 301, in <module>
    @properties.RelationshipProperty.strategy_for(lazy="noload")
AttributeError: 'module' object has no attribute 'RelationshipProperty'

这是追溯指向

的地方
### ./libs/sqlalchemy/orm/strategies.py
@log.class_logger
@properties.RelationshipProperty.strategy_for(lazy="noload") # <- here
@properties.RelationshipProperty.strategy_for(lazy=None)
class NoLoader(AbstractRelationshipLoader):
    # ...

实际问题:

我错过了什么/做错了什么? 我从here下载了tar.gz并将lib / sqlalchemy文件夹复制到我的lib路径

1 个答案:

答案 0 :(得分:2)

只是unpackig tar文件不能用于炼金术,因为它有本地库需要编译,所以你可以

sudo pip install alchemy or
untar your tar.gz and run "python setup.py install"

这就是“pip install”正在做的事情:

Installing collected packages: sqlalchemy
  Running setup.py install for sqlalchemy
    building 'sqlalchemy.cprocessors' extension
    /usr/bin/clang -fno-strict-aliasing -fno-common -dynamic -arch i386 -g -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c lib/sqlalchemy/cextension/processors.c -o build/temp.macosx-10.3-fat-2.7/lib/sqlalchemy/cextension/processors.o
    /usr/bin/clang -bundle -undefined dynamic_lookup -arch i386 -g build/temp.macosx-10.3-fat-2.7/lib/sqlalchemy/cextension/processors.o -o build/lib.macosx-10.3-fat-2.7/sqlalchemy/cprocessors.so
    building 'sqlalchemy.cresultproxy' extension
    /usr/bin/clang -fno-strict-aliasing -fno-common -dynamic -arch i386 -g -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c lib/sqlalchemy/cextension/resultproxy.c -o build/temp.macosx-10.3-fat-2.7/lib/sqlalchemy/cextension/resultproxy.o
    /usr/bin/clang -bundle -undefined dynamic_lookup -arch i386 -g build/temp.macosx-10.3-fat-2.7/lib/sqlalchemy/cextension/resultproxy.o -o build/lib.macosx-10.3-fat-2.7/sqlalchemy/cresultproxy.so
    building 'sqlalchemy.cutils' extension
    /usr/bin/clang -fno-strict-aliasing -fno-common -dynamic -arch i386 -g -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c lib/sqlalchemy/cextension/utils.c -o build/temp.macosx-10.3-fat-2.7/lib/sqlalchemy/cextension/utils.o
    /usr/bin/clang -bundle -undefined dynamic_lookup -arch i386 -g build/temp.macosx-10.3-fat-2.7/lib/sqlalchemy/cextension/utils.o -o build/lib.macosx-10.3-fat-2.7/sqlalchemy/cutils.so