1

我正在尝试python setup.py在运行 Flask + sqlalchemy (mysql) 的 Linode 实例上运行一个小脚本。

#filename - setup.py
from daaru import db

def init_db():
    db.drop_all()
    db.create_all()

init_db()

这是我得到的错误 -

Traceback (most recent call last):
  File "setup.py", line 21, in <module>
    init_db()
  File "setup.py", line 9, in init_db
    db.drop_all()
  File "/home/captain/public/daaru/v1/local/lib/python2.7/site-packages/flask_sqlalchemy.py", line 830, in drop_all
    self._execute_for_all_tables(app, bind, 'drop_all')
  File "/home/captain/public/daaru/v1/local/lib/python2.7/site-packages/flask_sqlalchemy.py", line 814, in _execute_for_all_tables
    op(bind=self.get_engine(app, bind), tables=tables)
  File "/home/captain/public/daaru/v1/local/lib/python2.7/site-packages/flask_sqlalchemy.py", line 763, in get_engine
    return connector.get_engine()
  File "/home/captain/public/daaru/v1/local/lib/python2.7/site-packages/flask_sqlalchemy.py", line 443, in get_engine
    self._engine = rv = sqlalchemy.create_engine(info, **options)
  File "/home/captain/public/daaru/v1/local/lib/python2.7/site-packages/sqlalchemy/engine/__init__.py", line 338, in create_engine
    return strategy.create(*args, **kwargs)
  File "/home/captain/public/daaru/v1/local/lib/python2.7/site-packages/sqlalchemy/engine/strategies.py", line 64, in create
    dbapi = dialect_cls.dbapi(**dbapi_args)
  File "/home/captain/public/daaru/v1/local/lib/python2.7/site-packages/sqlalchemy/connectors/mysqldb.py", line 52, in dbapi
    return __import__('MySQLdb')
ImportError: No module named MySQLdb

有趣的是,当我登录服务器时,激活虚拟环境(名为v1),然后init_db在 ipython 中运行,一切都按预期工作。但是当我将它作为脚本运行时,它给了我一个ImportError名为MySQLdb. MySQLdb 有一个系统范围的安装使用apt-get install python-mysqldb,因为这个包不能通过 pip 获得。

我在这里做错了什么?

pip freeze在服务器上运行向我展示了这一点:

Flask==0.9
Flask-SQLAlchemy==0.16
Jinja2==2.6
SQLAlchemy==0.7.9
Werkzeug==0.8.3
argparse==1.2.1
wsgiref==0.1.2

错误是因为在venv中找不到mysqldb包吗?那么为什么不使用该软件包的系统范围安装呢?

4

2 回答 2

2

我使用这篇博文在我的 venv 中安装了 mysql-python。现在一切正常。谢谢。我还添加了一个 fabfile 来自动部署 -

from fabric.api import *

env.user = "host"
env.hosts = ["hostname"]
env.directory = "/home/captain/public/daaru"
env.activate = "source /home/captain/public/daaru/v1/bin/activate"

def deploy():
    with cd(env.directory):
        run("git pull")
        run("sudo service apache2 reload")

def virtualenv(command):
    with cd(env.directory), prefix(env.activate):
        run(command)

def populate_db():
    virtualenv("python setup.py")

def freeze():
    """ command for testing virtualenv """
    virtualenv("pip freeze")
于 2012-10-13T06:46:17.007 回答
0

我刚刚遇到了这个问题,对我来说简单而肮脏的解决方法是 to cp -r /usr/lib/python2.7/dist-packages/*mysql* $VIRTUALENV/lib/python2.7/dist-packages/and cp -r /usr/lib/python2.7/dist-packages/*MySQL* $VIRTUALENV/lib/python2.7/dist-packages/,它获取了所有需要的部分。

pip install MySQL-python仔细研究后发现,由于libmysqlclient-dev没有通过 apt 安装,virtualenv中失败了。希望这可以帮助。

于 2014-09-05T17:39:18.253 回答