我有一个python脚本可以在我运行它的所有其他远程服务器上运行但由于某些原因在我安装了Anaconda的特定远程服务器上我得到了一个针对SQLAlchemy的ImportError。我正在连接MySQL数据库。
#!/usr/bin/python
from sqlalchemy import *
from sqlalchemy.orm import sessionmaker, mapper
import sys
dev_engine = create_engine('mysql://...')
prod_engine = create_engine('mysql://...')
def transactions(exchange, filepath):
dev_connect = dev_engine.connect()
prod_connect = prod_engine.connect()
get_dev_instrument = "select * from " + exchange + "_instrument;"
instruments = dev_engine.execute(get_dev_instrument)
instruments_list = [r for r in instruments]
get_prod_instrument = "select * from " + exchange + "_instrument;"
instruments_after = prod_engine.execute(get_prod_instrument)
instruments_after_list = [r2 for r2 in instruments_after]
...
尝试运行时出现以下错误。
File "/home/local/filepath/general.py", line 2, in <module>
from sqlalchemy import *
ImportError: No module named sqlalchemy
但是如果我运行python shell,我可以导入sqlalchemy
my.username@remote-server:~$ python
Python 2.7.11 |Anaconda 2.4.1 (64-bit)| (default, Dec 6 2015, 18:08:32)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> from sqlalchemy import *
>>>
有什么问题?
编辑:如果我从其目录中调用我的脚本,它的工作原理非常好。我正在使用完整的文件路径调用脚本,因为我试图从Crontab运行它。
my.username@remote-server:~$ python general.py arg1 arg2
formed maps
my.username@remote-server:~$
my.username@remote-server:~$: /usr/bin/python /home/local/TMG/filepath/general.py arg1 arg2
Traceback (most recent call last):
File "/home/local/TMG/filepath/general.py", line 2, in <module>
from sqlalchemy import *
ImportError: No module named sqlalchemy
答案 0 :(得分:0)
尝试添加:
import sys
sys.path.append("/home/local/TMG/filepath")
在你的剧本的最开始。