在SQLAlchemy的创建引擎:不能主机名到地址转换:名称或服务不知道

问题描述 投票:3回答:2

我在执行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
python sqlalchemy psycopg2
2个回答
4
投票

只是看了看项目。在你dbconstants.py,设置

dbname='localhost:5432/mydb'

-2
投票

错误:psycopg2.OperationalError:无法解析主机名“localhost”的解决:名称或服务不已知

在我的情况:

我的LAN电缆与系统不正确连接。这就是为什么我收到此错误。因此,这可能是这个问题的方案的一个。

连接LAN电缆后再次运行run命令。它得到解决。

© www.soinside.com 2019 - 2024. All rights reserved.