0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

psycopg3の「AttributeError: partially initialized module 'psycopg' has no attribute 'connect' (most likely due to a circular import)」でハマった

Posted at

発生事象

以下のスクリプトを実行しようとしたところエラー発生

select.py
import psycopg;

con = psycopg.connect(
    host = 'mydomain.jp',
    port = 5432,
    dbname='sample',
    user='admin',
    password='sample');
$ python3 select.py 
Traceback (most recent call last):
  File "select.py", line 1, in <module>
    import psycopg;
  File "/usr/local/lib/python3.8/site-packages/psycopg/__init__.py", line 9, in <module>
    from . import pq  # noqa: F401 import early to stabilize side effects
  File "/usr/local/lib/python3.8/site-packages/psycopg/pq/__init__.py", line 16, in <module>
    from . import abc
  File "/usr/local/lib/python3.8/site-packages/psycopg/pq/abc.py", line 11, in <module>
    from .._compat import Protocol, TypeAlias
  File "/usr/local/lib/python3.8/site-packages/psycopg/_compat.py", line 8, in <module>
    import asyncio
  File "/usr/local/lib/python3.8/asyncio/__init__.py", line 8, in <module>
    from .base_events import *
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 23, in <module>
    import socket
  File "/usr/local/lib/python3.8/socket.py", line 52, in <module>
    import os, sys, io, selectors
  File "/usr/local/lib/python3.8/selectors.py", line 12, in <module>
    import select
  File "/workspaces/scrapy_fd/select.py", line 5, in <module>
    con = psycopg.connect(
AttributeError: partially initialized module 'psycopg' has no attribute 'connect' (most likely due to a circular import)

原因

モジュール、必須ライブラリなどもっともらしいことを疑ったが、結果は「ファイル名」であった。

対処

コードの修正は不要で、ファイル名を変更するだけで正常に動作した。

select.py→sample.py

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?