GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — pytest.ini pythonpath doesn't work with local early-loaded (-p) plugin · Issue #11118 · pytest-dev/pytest

pytest.ini pythonpath doesn't work with local early-loaded (-p) plugin #11118

New issue
Copy link
New issue
Copy link
Closed
#11128
Closed
pytest.ini pythonpath doesn't work with local early-loaded (-p) plugin#11118
#11128
Copy link
Labels
topic: configrelated to config handling, argument parsing and config filetype: docsdocumentation improvement, missing or needing clarificationtype: questiongeneral question, might be closed after 2 weeks of inactivity

Description

@a3f
a3f
opened on Jun 18, 2023
Issue body actions

I am using pytest 7.2.1 on NixOS 23.05 and want to early-load a plugin, so I can do some custom command line parsing using pytest_load_initial_conftests. Here's my setup:

 >conftest.py     echo 'pytest_plugins = [ "my_plugin" ]'
 >my_plugin.py    echo 'print("Plugin loaded")'
 >pytest.ini      echo '[pytest]' 
>>pytest.ini      echo 'pythonpath = .' 

Normal non-early loading of plugin works:

$ pytest -q
no tests ran in 0.00s
Plugin loaded

Early-loading plugin with manual PYTHONPATH override also works:

$ PYTHONPATH="$PYTHONPATH:." pytest -p my_plugin -q
Plugin loaded

no tests ran in 0.00s

But relying on pytest.ini pythonpath for early-loading fails:

$ pytest -p my_plugin
Traceback (most recent call last):
  File "/nix/store/apjpa7zk1p61ri44n1a4mykdyqpbdvw1-python3.10-pytest-7.2.1/lib/python3.10/site-packages/_pytest/config/__init__.py", line 774, in import_plugin
    __import__(importspec)
ModuleNotFoundError: No module named 'my_plugin'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/nix/store/apjpa7zk1p61ri44n1a4mykdyqpbdvw1-python3.10-pytest-7.2.1/bin/.pytest-wrapped", line 9, in <module>
    sys.exit(console_main())
  File "/nix/store/apjpa7zk1p61ri44n1a4mykdyqpbdvw1-python3.10-pytest-7.2.1/lib/python3.10/site-packages/_pytest/config/__init__.py", line 190, in console_main
    code = main()
  File "/nix/store/apjpa7zk1p61ri44n1a4mykdyqpbdvw1-python3.10-pytest-7.2.1/lib/python3.10/site-packages/_pytest/config/__init__.py", line 148, in main
    config = _prepareconfig(args, plugins)
  File "/nix/store/apjpa7zk1p61ri44n1a4mykdyqpbdvw1-python3.10-pytest-7.2.1/lib/python3.10/site-packages/_pytest/config/__init__.py", line 329, in _prepareconfig
    config = pluginmanager.hook.pytest_cmdline_parse(
  File "/nix/store/r5wq2w8xy457h1bwx3fyb8lvlaim3gkx-python3.10-pluggy-1.0.0/lib/python3.10/site-packages/pluggy/_hooks.py", line 265, in __call__
    return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
  File "/nix/store/r5wq2w8xy457h1bwx3fyb8lvlaim3gkx-python3.10-pluggy-1.0.0/lib/python3.10/site-packages/pluggy/_manager.py", line 80, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/nix/store/r5wq2w8xy457h1bwx3fyb8lvlaim3gkx-python3.10-pluggy-1.0.0/lib/python3.10/site-packages/pluggy/_callers.py", line 55, in _multicall
    gen.send(outcome)
  File "/nix/store/apjpa7zk1p61ri44n1a4mykdyqpbdvw1-python3.10-pytest-7.2.1/lib/python3.10/site-packages/_pytest/helpconfig.py", line 103, in pytest_cmdline_parse
    config: Config = outcome.get_result()
  File "/nix/store/r5wq2w8xy457h1bwx3fyb8lvlaim3gkx-python3.10-pluggy-1.0.0/lib/python3.10/site-packages/pluggy/_result.py", line 60, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/nix/store/r5wq2w8xy457h1bwx3fyb8lvlaim3gkx-python3.10-pluggy-1.0.0/lib/python3.10/site-packages/pluggy/_callers.py", line 39, in _multicall
    res = hook_impl.function(*args)
  File "/nix/store/apjpa7zk1p61ri44n1a4mykdyqpbdvw1-python3.10-pytest-7.2.1/lib/python3.10/site-packages/_pytest/config/__init__.py", line 1058, in pytest_cmdline_parse
    self.parse(args)
  File "/nix/store/apjpa7zk1p61ri44n1a4mykdyqpbdvw1-python3.10-pytest-7.2.1/lib/python3.10/site-packages/_pytest/config/__init__.py", line 1346, in parse
    self._preparse(args, addopts=addopts)
  File "/nix/store/apjpa7zk1p61ri44n1a4mykdyqpbdvw1-python3.10-pytest-7.2.1/lib/python3.10/site-packages/_pytest/config/__init__.py", line 1225, in _preparse
    self.pluginmanager.consider_preparse(args, exclude_only=False)
  File "/nix/store/apjpa7zk1p61ri44n1a4mykdyqpbdvw1-python3.10-pytest-7.2.1/lib/python3.10/site-packages/_pytest/config/__init__.py", line 702, in consider_preparse
    self.consider_pluginarg(parg)
  File "/nix/store/apjpa7zk1p61ri44n1a4mykdyqpbdvw1-python3.10-pytest-7.2.1/lib/python3.10/site-packages/_pytest/config/__init__.py", line 728, in consider_pluginarg
    self.import_plugin(arg, consider_entry_points=True)
  File "/nix/store/apjpa7zk1p61ri44n1a4mykdyqpbdvw1-python3.10-pytest-7.2.1/lib/python3.10/site-packages/_pytest/config/__init__.py", line 776, in import_plugin
    raise ImportError(
  File "/nix/store/apjpa7zk1p61ri44n1a4mykdyqpbdvw1-python3.10-pytest-7.2.1/lib/python3.10/site-packages/_pytest/config/__init__.py", line 774, in import_plugin
    __import__(importspec)
ImportError: Error importing plugin "my_plugin": No module named 'my_plugin'

I haven't seen anything in the docs to indicate that pythonpath is not applicable to -p. If that's intended behavior, it would be nice to amend the docs to reflect that. Also, I am curious if there's another way to get pytest -p my_plugin to just work? I'd like to add -p my_plugin to my pytest.ini eventually, so users just need to type in pytest and not have to worry about paths.

Activity

  1. a3f commented on Jun 18, 2023

    @a3f
    a3f
    on Jun 18, 2023
    Author
    More actions

    I haven't seen anything in the docs to indicate that pythonpath is not applicable to -p

    Rereading the docs, it says: Directories remain in path for the duration of the test session and the preparse precedes that. Still, I wonder if this should be made to work. After all I can use addopts to inject an option and it is surprising, at least to me, that pythonpath defined in the same pytest.ini doesn't have an effect.

  2. Zac-HD
    added
    type: questiongeneral question, might be closed after 2 weeks of inactivity
    type: docsdocumentation improvement, missing or needing clarification
    topic: configrelated to config handling, argument parsing and config file
    on Jun 18, 2023
  3. bluetech commented on Jun 20, 2023

    @bluetech
    bluetech
    on Jun 20, 2023
    Member
    More actions

    You are right @a3f, currently pythonpath is implemented as an internal pytest plugin, it tries to run as early as possible, however -p my_plugin loading happens before that, necessarily.

    I think that if pythonpath is to affect early plugin loading, it can't be implemented in a plugin but needs to be implemented in Config directly. As is, I think the complexity is not quite worth it.

    Agreed about mentioning this gotcha in the docs.

    I am using pytest 7.2.1 on NixOS 23.05 and want to early-load a plugin, so I can do some custom command line parsing using pytest_load_initial_conftests.

    Can you describe a bit more what the plugin does? Maybe we can suggest a different way to achieve it.

  4. a3f commented on Jun 20, 2023

    @a3f
    a3f
    on Jun 20, 2023
    Author
    More actions

    Thanks for the confirmation.

    I wanted to support following command line syntax:

    pytest [pytest-options] path/to/tests -- [extra-options and arguments]
    

    Or alternatively:

    pytest [pytest-options] path/to/tests --qemu [extra-options and arguments]
    

    These extra-options and arguments would be passed as-is to Qemu, which the pytest tests are running against. My idea was to have an early plugin hook command line parsing to facilitate this.

  5. bluetech commented on Jun 20, 2023

    @bluetech
    bluetech
    on Jun 20, 2023
    Member
    More actions

    The --qemu one looks like something that should be achievable just through pytest_addoption, at least if you're OK with requiring that it comes last. Something like this:

    import argparse
    
    import pytest
    
    
    def pytest_addoption(parser: pytest.Parser) -> None:
        parser.addoption("--qemu", nargs=argparse.REMAINDER)
    
    
    # Use the option
    def pytest_configure(config: pytest.Config) -> None:
        qemu_opts = config.getoption("qemu") or []
  6. a3f commented on Jun 20, 2023

    @a3f
    a3f
    on Jun 20, 2023
    Author
    More actions

    @bluetech Nice, Thanks a lot! This works fine for me. :)

  7. bluetech
    added a commit that references this issue on Jun 22, 2023

    reference: add note that `pythonpath` does not affect `-p`

    This issue will close once commit 1e8135d is merged into the 'main' branch.
    1e8135d
  8. bluetech
    closed this as completedin #11128on Jun 22, 2023
  9. nicoddemus
    added a commit that references this issue on Aug 6, 2024

    Apply pythonpath option as early as possible (#12536)

    This issue will close once commit 85760bf is merged into the 'main' branch.
    85760bf
  10. luketainton
    added a commit that references this issue on Jan 5, 2026

    chore(deps): update dependency pytest to v9 (#305)

    4b75154
  11. imgbot
    added 2 commits that reference this issue on Apr 18, 2026

    reference: add note that `pythonpath` does not affect `-p`

    This issue will close once commit eca19b9 is merged into the 'main' branch.
    eca19b9

    reference: add note that `pythonpath` does not affect `-p`

    This issue will close once commit 8701f7e is merged into the 'main' branch.
    8701f7e
  12. MHammett
    added a commit that references this issue on Sep 18, 2026

    test: extend the socket guard to collection

    73d61a3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: configrelated to config handling, argument parsing and config filetype: docsdocumentation improvement, missing or needing clarificationtype: questiongeneral question, might be closed after 2 weeks of inactivity

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.