Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E _pytest.pathlib.ImportPathMismatchError #9567

Open
3 of 4 tasks
BinLiang2021 opened this issue Jan 27, 2022 · 9 comments
Open
3 of 4 tasks

E _pytest.pathlib.ImportPathMismatchError #9567

BinLiang2021 opened this issue Jan 27, 2022 · 9 comments
Labels
topic: collection related to the collection phase type: bug problem that needs to be addressed

Comments

@BinLiang2021
Copy link

  • When I test doctest in my own package, if I use pytest --doctest-modules, then it will raise this error. But If I do the doctest on each individual file, it will pass. I think the doctests is ok, But i can not avoid this error.
  • ---------------------- --------------------
    attrs 19.3.0
    Automat 0.8.0
    blinker 1.4
    certifi 2019.11.28
    chardet 3.0.4
    Click 7.0
    cloud-init 21.3
    colorama 0.4.3
    command-not-found 0.3
    configobj 5.0.6
    constantly 15.1.0
    cryptography 2.8
    cycler 0.11.0
    dbus-python 1.2.16
    distro 1.4.0
    distro-info 0.23ubuntu1
    entrypoints 0.3
    fonttools 4.29.0
    httplib2 0.14.0
    hyperlink 19.0.0
    idna 2.8
    importlib-metadata 1.5.0
    incremental 16.10.1
    iniconfig 1.1.1
    Jinja2 2.10.1
    jsonpatch 1.22
    jsonpointer 2.0
    jsonschema 3.2.0
    keyring 18.0.1
    kiwisolver 1.3.2
    language-selector 0.1
    launchpadlib 1.10.13
    lazr.restfulclient 0.14.2
    lazr.uri 1.0.3
    MarkupSafe 1.1.0
    matplotlib 3.5.1
    more-itertools 4.2.0
    netifaces 0.10.4
    numpy 1.22.1
    oauthlib 3.1.0
    packaging 21.3
    pexpect 4.6.0
    Pillow 9.0.0
    pip 20.0.2
    pluggy 1.0.0
    py 1.11.0
    pyasn1 0.4.2
    pyasn1-modules 0.2.1
    PyGObject 3.36.0
    PyHamcrest 1.9.0
    PyJWT 1.7.1
    pymacaroons 0.13.0
    PyNaCl 1.3.0
    pyOpenSSL 19.0.0
    pyparsing 3.0.7
    pyrsistent 0.15.5
    pyserial 3.4
    pytest 6.2.5
    python-apt 2.0.0+ubuntu0.20.4.6
    python-dateutil 2.8.2
    python-debian 0.1.36ubuntu1
    PyYAML 5.3.1
    requests 2.22.0
    requests-unixsocket 0.2.0
    SecretStorage 2.3.1
    service-identity 18.1.0
    setuptools 45.2.0
    simplejson 3.16.0
    six 1.14.0
    sos 4.1
    ssh-import-id 5.10
    systemd-python 234
    toml 0.10.2
    Tracknaliser 0.1.0
    Twisted 18.9.0
    ubuntu-advantage-tools 27.2
    ufw 0.36
    unattended-upgrades 0.1
    urllib3 1.25.8
    wadllib 1.3.3
    wheel 0.34.2
    zipp 1.0.0
    zope.interface 4.7.1
  • 6.2.5
  • minimal example if possible
@BinLiang2021
Copy link
Author

BTW, i use the WSL2(Ubuntu) to do this command

@The-Compiler
Copy link
Member

Please show the full output, and ideally provide an example (e.g. your project as a .zip or repository) which demonstrates the problem. Not much wen can do with so little information really.

@The-Compiler The-Compiler added status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity type: question general question, might be closed after 2 weeks of inactivity labels Jan 27, 2022
@BinLiang2021
Copy link
Author

Please show the full output, and ideally provide an example (e.g. your project as a .zip or repository) which demonstrates the problem. Not much wen can do with so little information really.

output:
7eb504fb6b2491d0ee8cfce67450c8f
461991b8ae854d721985225b0b3ad26

@BinLiang2021
Copy link
Author

1643295197(1)
And the individual doctest example

@and3rson
Copy link

and3rson commented May 4, 2022

I can reproduce this too whenever there are two modules with the same name in different packages. For example:

$ ls -R
./a:
foo.py

./b:
foo.py

$ pytest --doctest-modules .
================================================================ test session starts ================================================================
platform linux -- Python 3.10.4, pytest-7.1.2, pluggy-1.0.0
rootdir: /home/anderson/src/tp1
collected 0 items / 1 error

====================================================================== ERRORS =======================================================================
_____________________________________________________________ ERROR collecting b/foo.py _____________________________________________________________
.venv/lib/python3.10/site-packages/_pytest/runner.py:338: in from_call
    result: Optional[TResult] = func()
.venv/lib/python3.10/site-packages/_pytest/runner.py:369: in <lambda>
    call = CallInfo.from_call(lambda: list(collector.collect()), "collect")
.venv/lib/python3.10/site-packages/_pytest/doctest.py:545: in collect
    module = import_path(self.path, root=self.config.rootpath)
.venv/lib/python3.10/site-packages/_pytest/pathlib.py:556: in import_path
    raise ImportPathMismatchError(module_name, module_file, path)
E   _pytest.pathlib.ImportPathMismatchError: ('foo', '/home/anderson/src/tp1/a/foo.py', PosixPath('/home/anderson/src/tp1/b/foo.py'))
============================================================== short test summary info ==============================================================
ERROR b/foo.py - _pytest.pathlib.ImportPathMismatchError: ('foo', '/home/anderson/src/tp1/a/foo.py', PosixPath('/home/anderson/src/tp1/b/foo.py'))
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
================================================================= 1 error in 0.07s ==================================================================

Edit: I've found a temporary fix here #2042 (comment) (PY_IGNORE_IMPORTMISMATCH=1 env var.)

@Zac-HD Zac-HD added type: bug problem that needs to be addressed topic: collection related to the collection phase and removed type: question general question, might be closed after 2 weeks of inactivity status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity labels Jun 1, 2022
shenanigansd added a commit to darbiadev/darbiadev-shipping that referenced this issue Nov 12, 2023
shenanigansd added a commit to darbiadev/darbiadev-shipping that referenced this issue Nov 12, 2023
* Cleanup repo

Signed-off-by: GitHub <noreply@github.com>

* Bump CI versions

Signed-off-by: GitHub <noreply@github.com>

* Add pre-commit config

Signed-off-by: GitHub <noreply@github.com>

* oops

Signed-off-by: GitHub <noreply@github.com>

* go away

Signed-off-by: GitHub <noreply@github.com>

* Tell Ruff to set the line length to 120

Signed-off-by: GitHub <noreply@github.com>

* Setup Ruff config

Signed-off-by: GitHub <noreply@github.com>

* Limit support to Python 3.11 and 3.12

Signed-off-by: GitHub <noreply@github.com>

* Ruff removed the import from the root init py

Signed-off-by: GitHub <noreply@github.com>

* Add back other import

Signed-off-by: GitHub <noreply@github.com>

* Doctest thing is causing the failure for some reason

pytest-dev/pytest#9567
Signed-off-by: GitHub <noreply@github.com>

* Oh look I committed generated files

Signed-off-by: GitHub <noreply@github.com>

---------

Signed-off-by: GitHub <noreply@github.com>
@conradogarciaberrotaran

Issue still happening, any intention to fix?

@The-Compiler
Copy link
Member

@conradogarciaberrotaran That's the whole point of having an open issue in an issue tracker, yes.

@nicoddemus
Copy link
Member

nicoddemus commented Jan 23, 2024

@and3rson

I can reproduce this too whenever there are two modules with the same name in different packages.

Here what is happening is that I believe you do not have __init__.py files, which pytest needs in order to properly identify the containing directories as packages, so /home/anderson/src/tp1/a/foo.py and /home/anderson/src/tp1/b/foo.py end up both being imported as foo. PY_IGNORE_IMPORTMISMATCH=1 can be used to ignore the error, which is OK in some situations, also --importmode=importlib should work.

The proper fix in this case is to add __init__.py files to turn the directories into actual packages, so /home/anderson/src/tp1/a/foo.py will be imported as tp1.a.foo, and /home/anderson/src/tp1/b/foo.py will be imported as tp1.b.foo. See https://docs.pytest.org/en/stable/explanation/pythonpath.html#standalone-test-modules-conftest-py-files.

Not sure if this applies to the original's OP issue with doctest modules though.

@stefanv
Copy link

stefanv commented Feb 2, 2024

--import-mode=importlib (note typo above) helped me figure out the problem. In one of my tests, I was doing from util import x, where util.py was a file in the same directory. Seems legit, but didn't work any longer.

So, I had to change it to from .util import x and add an __init__.py to the same directory.

stefanv added a commit to scientific-python/yaml2ics that referenced this issue Feb 2, 2024
xref: pytest-dev/pytest#9567 (comment)

Also replaced `datetime.utcnow()` to avoid deprecation warning.
stefanv added a commit to scientific-python/yaml2ics that referenced this issue Feb 2, 2024
xref: pytest-dev/pytest#9567 (comment)

Also replaced `datetime.utcnow()` to avoid deprecation warning.
jarrodmillman added a commit to scientific-python/yaml2ics that referenced this issue Feb 2, 2024
* Work around pytest 8 failures

xref: pytest-dev/pytest#9567 (comment)

Also replaced `datetime.utcnow()` to avoid deprecation warning.

* Unpin pytest and pytest-cov

* Make utcnow compatible with Python <3.11

* Update yaml2ics.py

---------

Co-authored-by: Jarrod Millman <jarrod.millman@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: collection related to the collection phase type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

8 participants
@stefanv @and3rson @The-Compiler @nicoddemus @Zac-HD @conradogarciaberrotaran @BinLiang2021 and others