在 SQLAlchemy 中创建引擎:无法将主机名转换为地址:名称或服务未知

Creating engine in SQLAlchemy: could not translate host name to address: Name or service not known

我正在 linux mint 17.2 中执行以下 project

我安装了必要的模块并创建了数据库服务器,并创建了一个名为 'dbconstants.py' 的附加文件,其中包含数据库详细信息。

username = 'postgres'
password = 'postgres'
dbname = 'mydb'

我正在尝试按如下方式部分执行项目:

python -m plagcomps.extrinsic.extrinsic_testing

但是,我收到以下错误

Traceback (most recent call last):
  File "/usr/lib/python2.7/runpy.py", line 151, in _run_module_as_main
    mod_name, loader, code, fname = _get_module_details(mod_name)
  File "/usr/lib/python2.7/runpy.py", line 101, in _get_module_details
    loader = get_loader(mod_name)
  File "/usr/lib/python2.7/pkgutil.py", line 464, in get_loader
    return find_loader(fullname)
  File "/usr/lib/python2.7/pkgutil.py", line 474, in find_loader
    for importer in iter_importers(fullname):
  File "/usr/lib/python2.7/pkgutil.py", line 430, in iter_importers
    __import__(pkg)
  File "plagcomps/__init__.py", line 8, in <module>
    import intrinsic
  File "plagcomps/intrinsic/__init__.py", line 2, in <module>
    from cluster import cluster
  File "plagcomps/intrinsic/cluster.py", line 4, in <module>
    import classify
  File "plagcomps/intrinsic/classify.py", line 173, in <module>
    Base.metadata.create_all(engine)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/schema.py", line 3687, in create_all
    tables=tables)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1855, in _run_visitor
    with self._optional_conn_ctx_manager(connection) as conn:
  File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1848, in _optional_conn_ctx_manager
    with self.contextual_connect() as conn:
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 2039, in contextual_connect
    self._wrap_pool_connect(self.pool.connect, None),
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 2078, in _wrap_pool_connect
    e, dialect, self)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1405, in _handle_dbapi_exception_noconnection
    exc_info
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/compat.py", line 199, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 2074, in _wrap_pool_connect
    return fn()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 376, in connect
    return _ConnectionFairy._checkout(self)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 713, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 480, in checkout
    rec = pool._do_get()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 1060, in _do_get
    self._dec_overflow()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/langhelpers.py", line 60, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 1057, in _do_get
    return self._create_connection()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 323, in _create_connection
    return _ConnectionRecord(self)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 449, in __init__
    self.connection = self.__connect()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 607, in __connect
    connection = self.__pool._invoke_creator(self)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/strategies.py", line 97, in connect
    return dialect.connect(*cargs, **cparams)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/default.py", line 385, in connect
    return self.dbapi.connect(*cargs, **cparams)
  File "/usr/lib/python2.7/dist-packages/psycopg2/__init__.py", line 179, in connect
    connection_factory=connection_factory, async=async)
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not translate host name "mydb" to address: Name or service not known

更新:我修改了 dbconstants.py 如下:

username = 'postgres'
password = 'postgres'
dbname = 'localhost'

然后我收到以下错误:

/usr/lib/pymodules/python2.7/matplotlib/__init__.py:1173: UserWarning:  This call to matplotlib.use() has no effect
because the backend has already been chosen;
matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

  warnings.warn(_use_error_msg)
Traceback (most recent call last):
  File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/home/eadaradhiraj/Downloads/plagcomps/extrinsic/extrinsic_testing.py", line 615, in <module>
    save_to_db=False, ignore_high_obfuscation=False, show_false_negpos_info=False, get_best_of=False)
  File "/home/eadaradhiraj/Downloads/plagcomps/extrinsic/extrinsic_testing.py", line 553, in test
    mid = fingerprintstorage.get_mid(method, n, k, atom_type, hash_size)
  File "plagcomps/extrinsic/fingerprintstorage.py", line 313, in get_mid
    with _get_connection(autocommit=True) as conn:
  File "plagcomps/extrinsic/fingerprintstorage.py", line 92, in _get_connection
    conn = psycopg2.connect(user = username, password = password, database = dbname.split("/")[1], host="localhost", port = 5432)
IndexError: list index out of range

刚看了一下项目。在你的 dbconstants.py 中,设置

dbname='localhost:5432/mydb'

Error: psycopg2.OperationalError: could not translate host name "localhost" to address: Name or service not known

我的情况

我的局域网电缆没有正确连接到系统。这就是我收到此错误的原因。所以这可能是这个问题的一种情况。

连接网线后,再次运行使用运行命令。解决了。​​