example_pypi_package
example_pypi_package copied to clipboard
ModuleNotFoundError: No module named 'examplepy'
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?
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.
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.pythe absolute import statementfrom example_pkg import file_bwould 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 thesrcfolder is no longer exists.
It turned out that there were two issues at play:
- PyCharm import hinting was incorrect and misleading me to think
from example_pkg import ...was not a valid import statement. - The
sys.pathis different when running tests as compared to manually running a file within theexample_pkgpackage.
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
- Mark the
srcdirectory 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). - 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.