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

ModuleNotFoundError: No module named 'examplepy' #1

Open
erelsgl opened this issue Feb 3, 2022 · 2 comments
Open

ModuleNotFoundError: No module named 'examplepy' #1

erelsgl opened this issue Feb 3, 2022 · 2 comments

Comments

@erelsgl
Copy link

erelsgl commented Feb 3, 2022

I downloaded the example package and tried to run test_module1.py. I got an error: ModuleNotFoundError: No module named 'examplepy'. I use VSCode on Windows 10, and the contents of vscode.env are as in the repository:

PYTHONPATH=/;src/;${PYTHONPATH}

Is it possible that VSCode ignores this file?

@davidebbo
Copy link

Getting the same error, trying to run the tests with the unmodified repo. I'm new to packages, and was hoping to find a clear sample that I can run as is.

@Callum-FPA
Copy link

Callum-FPA commented Apr 12, 2023

Update @ 15 Jan 2024

We can avoid the raw PYTHONPATH manipulation by importing the module with an 'editable installation', which lets you use it like any other installed module (for the most part). This is supported by pip and most virtual environment tools.

Read more:
https://setuptools.pypa.io/en/latest/userguide/development_mode.html
https://docs.pipenv.org/en/latest/advanced.html#editable-dependencies-e-g-e

Original Post @ 12 Apr 2023

Thought I'd drop a comment since I've been bashing my brain out over this for the last day:

I've also run into the same issue, regarding being unable to directly import from the package name, and no tutorial or example project I've seen seems to have had it or address it.

Given the file structure:

example_pkg/
├─ src/
│  ├─ example_pkg/
│  │  ├─ __init__.py
│  │  ├─ file_a.py
│  │  ├─ file_b.py
├─ tests/
│  ├─ __init__.py
│  ├─ test_something.py
├─ pyproject.toml

When running pytest:

  • In src/example_pkg/file_a.py the absolute import statement from example_pkg import file_b would raise an "example_pkg" ModuleNotFoundError.
  • A relative import works (e.g. from .file_b import SomeClass), but this can get ugly when files/modules are nested in sub-dirs/packages.
  • Using from src.example_pkg ... was suggested by my IDE, PyCharm 2022.3.1, but this fails once packaged because the src folder is no longer exists.

It turned out that there were two issues at play:

  1. PyCharm import hinting was incorrect and misleading me to think from example_pkg import ... was not a valid import statement.
  2. The sys.path is different when running tests as compared to manually running a file within the example_pkg package.

Fixing Tests

For our tests, we need to add "src" to sys.path so that the module "example_pkg" becomes visible. If we inspect sys.path we can see that the tests run from the project root (i.e. "/.../example_pkg"), when we want to also search "/.../example_pkg/src"). The simplest way I've found to do this is to, in tests/__init__.py, add:

import sys
from pathlib import Path

sys.path.append(
    str(Path(__file__).parent.parent.joinpath("src"))
)

And now $ pytest will work with from example_pkg ... import statements!

Fixing PyCharm

  1. Mark the src directory as the 'Sources Root' (to do this, right click on the folder in the project explorer and navigate to the bottom of the context menu).
  2. Update the import index by making a small change (which can be immediately reverted) to PyCharm's recognised file types for python, with Settings > Editor > File Types > Python. See here for an example.

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

3 participants