首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >pytest_postgresql示例引发导入错误心理

pytest_postgresql示例引发导入错误心理
EN

Stack Overflow用户
提问于 2022-03-01 05:59:23
回答 1查看 1.2K关注 0票数 1

我目前正在努力让pytest_postgresql在我的环境中运行。在我开始之前:我可以测试sql代码--它是self的,对吗?不只是你的数据库连接会像这样工作吗?

所以我接受了github自述的样本测试:

代码语言:javascript
运行
复制
def test_example_postgres(postgresql):
    """Check main postgresql fixture."""
    cur = postgresql.cursor()
    cur.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);")
    postgresql.commit()
    cur.close()

比我试一试:pytest

我得到了:

代码语言:javascript
运行
复制
================================================================================================================= ERRORS =================================================================================================================
________________________________________________________________________________________________ ERROR at setup of test_example_postgres _________________________________________________________________________________________________

request = <SubRequest 'postgresql' for <Function test_example_postgres>>

    @pytest.fixture
    def postgresql_factory(request: FixtureRequest) -> Iterator[connection]:
        """
        Fixture factory for PostgreSQL.

        :param request: fixture request object
        :returns: postgresql client
        """
>       check_for_psycopg()

venv\lib\site-packages\pytest_postgresql\factories\client.py:56:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    def check_for_psycopg() -> None:
        """
        Function checks whether psycopg was imported.

        Raises ImportError if not.
        """
        if not psycopg:
>           raise ImportError("No module named psycopg. Please install psycopg.")
E           ImportError: No module named psycopg. Please install psycopg.

venv\lib\site-packages\pytest_postgresql\compat.py:35: ImportError
======================================================================================================== short test summary info =========================================================================================================
ERROR test_postgresql.py::test_example_postgres - ImportError: No module named psycopg. Please install psycopg.
============================================================================================================ 1 error in 0.12s ============================================================================================================

所以我试了一下:pip install psycopg

我得到了:

代码语言:javascript
运行
复制
Requirement already satisfied: psycopg in c:\users\a05922\pycharmprojects\try_out_pytest_postgresql\venv\lib\site-packages (3.0.9)
Requirement already satisfied: tzdata in c:\users\a05922\pycharmprojects\try_out_pytest_postgresql\venv\lib\site-packages (from psycopg) (2021.5)

所以我想,如果我导入它,python可能会意识到这个包已经安装了:

代码语言:javascript
运行
复制
import psycopg


def test_example_postgres(postgresql):
    """Check main postgresql fixture."""
    cur = postgresql.cursor()
    cur.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);")
    postgresql.commit()
    cur.close()

我试过:pytest

我得到了:

代码语言:javascript
运行
复制
========================================================================================================== test session starts ===========================================================================================================
platform win32 -- Python 3.9.4, pytest-7.0.1, pluggy-1.0.0
rootdir: C:\Users\a05922\PycharmProjects\try_out_pytest_postgresql
plugins: postgresql-4.1.0
collected 0 items / 1 error                                                                                                                                                                                                               

================================================================================================================= ERRORS =================================================================================================================
__________________________________________________________________________________________________ ERROR collecting test_postgresql.py ___________________________________________________________________________________________________
ImportError while importing test module 'C:\Users\a05922\PycharmProjects\try_out_pytest_postgresql\test_postgresql.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
C:\Programme\Python\Python39\lib\importlib\__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
test_postgresql.py:1: in <module>
    import psycopg
venv\lib\site-packages\psycopg\__init__.py:9: in <module>
    from . import pq  # noqa: F401 import early to stabilize side effects
venv\lib\site-packages\psycopg\pq\__init__.py:114: in <module>
    import_from_libpq()
venv\lib\site-packages\psycopg\pq\__init__.py:106: in import_from_libpq
    raise ImportError(
E   ImportError: no pq wrapper available.
E   Attempts made:
E   - couldn't import psycopg 'c' implementation: No module named 'psycopg_c'
E   - couldn't import psycopg 'binary' implementation: No module named 'psycopg_binary'
E   - couldn't import psycopg 'python' implementation: libpq library not found
======================================================================================================== short test summary info =========================================================================================================
ERROR test_postgresql.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================================================================================ 1 error in 0.15s ============================================================================================================

这是我没有想法的地方,接下来该尝试什么。我试着搜索这个问题,但我空手而归。有人知道我怎么解决这个问题吗?

编辑:

pip uninstall psycopgpip install psycopg[binary]的建议

我得到了:

代码语言:javascript
运行
复制
Microsoft Windows [Version 10.0.18363.2094]
(c) 2019 Microsoft Corporation. Alle Rechte vorbehalten.

(venv) C:\Users\a05922\PycharmProjects\try_out_pytest_postgresql>pip install psycopg
Requirement already satisfied: psycopg in c:\users\a05922\pycharmprojects\try_out_pytest_postgresql\venv\lib\site-packages (3.0.9)
Requirement already satisfied: tzdata in c:\users\a05922\pycharmprojects\try_out_pytest_postgresql\venv\lib\site-packages (from psycopg) (2021.5)

(venv) C:\Users\a05922\PycharmProjects\try_out_pytest_postgresql>pip install psycopg[binary]
Requirement already satisfied: psycopg[binary] in c:\users\a05922\pycharmprojects\try_out_pytest_postgresql\venv\lib\site-packages (3.0.9)
Requirement already satisfied: tzdata in c:\users\a05922\pycharmprojects\try_out_pytest_postgresql\venv\lib\site-packages (from psycopg[binary]) (2021.5)
Collecting psycopg-binary==3.0.9
  Downloading psycopg_binary-3.0.9-cp39-cp39-win_amd64.whl (2.9 MB)
     ---------------------------------------- 2.9/2.9 MB 10.3 MB/s eta 0:00:00
Installing collected packages: psycopg-binary
Successfully installed psycopg-binary-3.0.9

pytest抓到我了

代码语言:javascript
运行
复制
========================================================================================================== test session starts ===========================================================================================================
platform win32 -- Python 3.9.4, pytest-7.0.1, pluggy-1.0.0
rootdir: C:\Users\a05922\PycharmProjects\try_out_pytest_postgresql
plugins: postgresql-4.1.0
collected 1 item                                                                                                                                                                                                                          

test_postgresql.py E                                                                                                                                                                                                                [100%]

================================================================================================================= ERRORS =================================================================================================================
________________________________________________________________________________________________ ERROR at setup of test_example_postgres _________________________________________________________________________________________________

request = <SubRequest 'postgresql' for <Function test_example_postgres>>

    @pytest.fixture
    def postgresql_factory(request: FixtureRequest) -> Iterator[connection]:
        """
        Fixture factory for PostgreSQL.

        :param request: fixture request object
        :returns: postgresql client
        """
        check_for_psycopg()
>       proc_fixture: Union[PostgreSQLExecutor, NoopExecutor] = request.getfixturevalue(
            process_fixture_name
        )

venv\lib\site-packages\pytest_postgresql\factories\client.py:57:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
venv\lib\site-packages\pytest_postgresql\factories\process.py:104: in postgresql_proc_fixture
    pg_bindir = subprocess.check_output(
C:\Programme\Python\Python39\lib\subprocess.py:424: in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
C:\Programme\Python\Python39\lib\subprocess.py:505: in run
    with Popen(*popenargs, **kwargs) as process:
C:\Programme\Python\Python39\lib\subprocess.py:951: in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <Popen: returncode: None args: ['pg_config', '--bindir']>, args = 'pg_config --bindir', executable = None, preexec_fn = None, close_fds = False, pass_fds = (), cwd = None, env = None
startupinfo = <subprocess.STARTUPINFO object at 0x000002A882A007C0>, creationflags = 0, shell = False, p2cread = Handle(880), p2cwrite = -1, c2pread = 14, c2pwrite = Handle(748), errread = -1, errwrite = Handle(632)
unused_restore_signals = True, unused_gid = None, unused_gids = None, unused_uid = None, unused_umask = -1, unused_start_new_session = False

    def _execute_child(self, args, executable, preexec_fn, close_fds,
                       pass_fds, cwd, env,
                       startupinfo, creationflags, shell,
                       p2cread, p2cwrite,
                       c2pread, c2pwrite,
                       errread, errwrite,
                       unused_restore_signals,
                       unused_gid, unused_gids, unused_uid,
                       unused_umask,
                       unused_start_new_session):
        """Execute program (MS Windows version)"""

        assert not pass_fds, "pass_fds not supported on Windows."

        if isinstance(args, str):
            pass
        elif isinstance(args, bytes):
            if shell:
                raise TypeError('bytes args is not allowed on Windows')
            args = list2cmdline([args])
        elif isinstance(args, os.PathLike):
            if shell:
                raise TypeError('path-like args is not allowed when '
                                'shell is true')
            args = list2cmdline([args])
        else:
            args = list2cmdline(args)

        if executable is not None:
            executable = os.fsdecode(executable)

        # Process startup details
        if startupinfo is None:
            startupinfo = STARTUPINFO()
        else:
            # bpo-34044: Copy STARTUPINFO since it is modified above,
            # so the caller can reuse it multiple times.
            startupinfo = startupinfo.copy()

        use_std_handles = -1 not in (p2cread, c2pwrite, errwrite)
        if use_std_handles:
            startupinfo.dwFlags |= _winapi.STARTF_USESTDHANDLES
            startupinfo.hStdInput = p2cread
            startupinfo.hStdOutput = c2pwrite
            startupinfo.hStdError = errwrite

        attribute_list = startupinfo.lpAttributeList
        have_handle_list = bool(attribute_list and
                                "handle_list" in attribute_list and
                                attribute_list["handle_list"])

        # If we were given an handle_list or need to create one
        if have_handle_list or (use_std_handles and close_fds):
            if attribute_list is None:
                attribute_list = startupinfo.lpAttributeList = {}
            handle_list = attribute_list["handle_list"] = \
                list(attribute_list.get("handle_list", []))

            if use_std_handles:
                handle_list += [int(p2cread), int(c2pwrite), int(errwrite)]

            handle_list[:] = self._filter_handle_list(handle_list)

            if handle_list:
                if not close_fds:
                    warnings.warn("startupinfo.lpAttributeList['handle_list'] "
                                  "overriding close_fds", RuntimeWarning)

                # When using the handle_list we always request to inherit
                # handles but the only handles that will be inherited are
                # the ones in the handle_list
                close_fds = False

        if shell:
            startupinfo.dwFlags |= _winapi.STARTF_USESHOWWINDOW
            startupinfo.wShowWindow = _winapi.SW_HIDE
            comspec = os.environ.get("COMSPEC", "cmd.exe")
            args = '{} /c "{}"'.format (comspec, args)

        if cwd is not None:
            cwd = os.fsdecode(cwd)

        sys.audit("subprocess.Popen", executable, args, cwd, env)

        # Start the process
        try:
>           hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
                                     # no special security
                                     None, None,
                                     int(not close_fds),
                                     creationflags,
                                     env,
                                     cwd,
                                     startupinfo)
E                                    FileNotFoundError: [WinError 2] Das System kann die angegebene Datei nicht finden

C:\Programme\Python\Python39\lib\subprocess.py:1420: FileNotFoundError
======================================================================================================== short test summary info =========================================================================================================
ERROR test_postgresql.py::test_example_postgres - FileNotFoundError: [WinError 2] Das System kann die angegebene Datei nicht finden
============================================================================================================ 1 error in 0.35s ============================================================================================================

最后的德文一行说:“系统找不到指定的文件”。

我很难弄清楚这个问题。在我看来,最像文件名的参数是cwd。只要cwd已经存在,这个参数就会在fsdecode上面创建几行。

这一行是:

代码语言:javascript
运行
复制
self = <Popen: returncode: None args: ['pg_config', '--bindir']>, args = 'pg_config --bindir', executable = None, preexec_fn = None, close_fds = False, pass_fds = (), cwd = None, env = None
startupinfo = <subprocess.STARTUPINFO object at 0x000002A882A007C0>, creationflags = 0, shell = False, p2cread = Handle(880), p2cwrite = -1, c2pread = 14, c2pwrite = Handle(748), errread = -1, errwrite = Handle(632)
unused_restore_signals = True, unused_gid = None, unused_gids = None, unused_uid = None, unused_umask = -1, unused_start_new_session = False

调用_execute_child的参数有哪些?如果是这样的话,cwd将是零,也不可能是问题所在。有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-01 16:20:50

如果您将最初的安装作为pip install psycopg进行,那么您就得到了纯Python版本。根据安装指令,意味着您还需要安装libpq库。不知道你怎么会在Windows上得到这样的结果。我想说的是,解决办法是:

pip uninstall psycopg

然后

pip install psycopg[binary]

这将使您能够自行安装(所有所需的库)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71304312

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档