我正在尝试建立并连接到本地数据库。我正在将Anaconda版本1.6.14与python 3.6.5一起使用-我的路径中已经安装了5.7.23,以及mysqlclient == 1.3.13。
import os
cwd = os.getcwd()
config_path = cwd + '/config.ini_sample'
print(config_path)
import configparser
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
config_fields = ['host', 'port', 'user', 'passwd', 'database', 'sql_type',
'charset']
def read_config():
config = configparser.ConfigParser()
with open(str(config_path)) as fp:
config.read_file(fp)
actual_config_fields = config['Global'].keys()
missing = set(config_fields) - actual_config_fields
if missing:
print('The configuration file (%s) ' % str(config_path) +
'is invalid. ' +
'Missing fields %s' % (', '.join(map(repr, missing))))
raise Exception
return config
def setup_database_connection():
global _engine, _session
config = read_config()
connection_url = ("{sql_type}://{user}:{passwd}@{host}:{port}/"
"{database}?charset={charset}")
connection_url = connection_url.format(**config['Global'])
_engine = create_engine(connection_url, encoding='utf-8')
_session = sessionmaker(bind=engine)
setup_database_connection()
config.ini.sample是
[Global]
host = localhost
user = your_user
port = 3306
passwd = your_pass
charset = utf8
database = erhvervsdata
sql_type = mysql
和错误
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-4-6dff59dfc481> in <module>
35 _session = sessionmaker(bind=engine)
36
---> 37 setup_database_connection()
38
<ipython-input-4-6dff59dfc481> in setup_database_connection()
32 "{database}?charset={charset}")
33 connection_url = connection_url.format(**config['Global'])
---> 34 _engine = create_engine(connection_url, encoding='utf-8')
35 _session = sessionmaker(bind=engine)
36
/anaconda3/envs/Virk/lib/python3.6/site-packages/sqlalchemy/engine/__init__.py in create_engine(*args, **kwargs)
423 strategy = kwargs.pop('strategy', default_strategy)
424 strategy = strategies.strategies[strategy]
--> 425 return strategy.create(*args, **kwargs)
426
427
/anaconda3/envs/Virk/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py in create(self, name_or_url, **kwargs)
79 if k in kwargs:
80 dbapi_args[k] = pop_kwarg(k)
---> 81 dbapi = dialect_cls.dbapi(**dbapi_args)
82
83 dialect_args['dbapi'] = dbapi
/anaconda3/envs/Virk/lib/python3.6/site-packages/sqlalchemy/dialects/mysql/mysqldb.py in dbapi(cls)
100 @classmethod
101 def dbapi(cls):
--> 102 return __import__('MySQLdb')
103
104 def do_executemany(self, cursor, statement, parameters, context=None):
/anaconda3/envs/Virk/lib/python3.6/site-packages/MySQLdb/__init__.py in <module>
16 from MySQLdb.release import __version__, version_info, __author__
17
---> 18 import _mysql
19
20 if version_info != _mysql.version_info:
ImportError: dlopen(/anaconda3/envs/Virk/lib/python3.6/site-packages/_mysql.cpython-36m-darwin.so, 2): Library not loaded: @rpath/libssl.1.0.0.dylib
Referenced from: /anaconda3/envs/Virk/lib/python3.6/site-packages/_mysql.cpython-36m-darwin.so
Reason: image not found
答案 0 :(得分:1)
尝试将所有anaconda库包含在其中
export DYLD_FALLBACK_LIBRARY_PATH=$HOME/anaconda/lib/:$DYLD_FALLBACK_LIBRARY_PATH