example_pypi_package icon indicating copy to clipboard operation
example_pypi_package copied to clipboard

ModuleNotFoundError: No module named 'examplepy'

Open erelsgl opened this issue 3 years ago • 2 comments
trafficstars

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?

erelsgl avatar Feb 03 '22 02:02 erelsgl

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.

davidebbo avatar Jan 28 '23 17:01 davidebbo

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.

Callum-FPA avatar Apr 12 '23 00:04 Callum-FPA