2012-10-13 89 views
1

我想在運行Flask + sqlalchemy(mysql)的Linode實例上運行python setup.py一個小腳本。在Virtualenv中導入錯誤

#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 

有趣的是,當我登錄到服務器,激活虛擬ENV(名爲v1),並在IPython的一切運行init_db按預期工作。但是當我將它作爲腳本運行時,它給了我一個名爲MySQLdb的模塊的ImportError。 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 

來了,因爲MySQLdb的包沒有在VENV發現錯誤?那麼爲什麼不使用該軟件包的系統範圍安裝?

+0

如果人們認爲他們可以爲您提供更好的解決方案,他們仍然可以添加答案;兩天的延遲是讓他們有機會看看你的問題。 :-) –

回答

2

我用this博客文章在我的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") 
0

我只是碰到了這一點,對我來說是簡單的,骯髒的解決方法是cp -r /usr/lib/python2.7/dist-packages/*mysql* $VIRTUALENV/lib/python2.7/dist-packages/cp -r /usr/lib/python2.7/dist-packages/*MySQL* $VIRTUALENV/lib/python2.7/dist-packages/,其中拿起所有需要的零部件。

仔細研究它,結果發現pip install MySQL-python在virtualenv失敗,原因是libmysqlclient-dev沒有通過apt安裝。希望這可以幫助。

相關問題