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

Import is resolving the wrong module #937

Open
fraenkel opened this issue May 2, 2023 · 2 comments
Open

Import is resolving the wrong module #937

fraenkel opened this issue May 2, 2023 · 2 comments

Comments

@fraenkel
Copy link

fraenkel commented May 2, 2023

If you have a submodule under tasks that has the same name as a top-level module, the import will fail.

For example, I have a module named common which is located in my site-packages.
My tasks directory looks like:

/project
  /tasks
    __init__.py
    common.py
     

The issue is pretty simple,
https://github.com/pyinvoke/invoke/blob/main/invoke/loader.py#L77

import common now comes from /project/tasks instead of my site-packages.

@fraenkel
Copy link
Author

fraenkel commented May 2, 2023

If you have a module in the /tasks directory with the same name as a package in site-packages, they will resolve to the same name because the /tasks directory was added to sys.path.

Here is an example of the failure:

diff --git a/integration/_support/package/tasks/module.py b/integration/_support/package/tasks/module.py
index 05f37ee5..5bca5e80 100644
--- a/integration/_support/package/tasks/module.py
+++ b/integration/_support/package/tasks/module.py
@@ -1,4 +1,6 @@
 from invoke import task
+from . import pytest as pt
+from pytest import Testdir


 @task
diff --git a/integration/_support/package/tasks/pytest.py b/integration/_support/package/tasks/pytest.py

When you run inv -l

✗ inv -l
Traceback (most recent call last):
....
ImportError: cannot import name 'Testdir' from 'pytest' 

If we look at sys.modules, we see:

'tasks.pytest': <module 'tasks.pytest' from '/invoke/integration/_support/package/tasks/pytest.py'>, 
'pytest': <module 'pytest' from '/invoke/integration/_support/package/tasks/pytest.py'>}

which is wrong.

@fraenkel
Copy link
Author

fraenkel commented May 3, 2023

Comparing 2.0 with 2.1.1, the main difference is sys.path.
2.0: /invoke/integration/_support/package is added
2.1.1: /invoke/integration/_support/package/tasks is added

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant