Module discovery¶
Slotscheck needs to import your code to check it. If it can’t, it suggests which directory to add to Python’s import path:
ERROR: File '/home/user/project/src/foo/bar.py' is not in PYTHONPATH.
Try setting PYTHONPATH=src, or passing --pythonpath src
Either works. As a rule of thumb:
To check files in the current directory (or below it), run slotscheck as
python -m slotscheck.To check files anywhere else, pass
--pythonpath, or set the$PYTHONPATHenvironment variable.
That is all you need to know. If the suggestion didn’t work, see What about implicit namespace packages?. The rest of this page explains why slotscheck can’t find the directory on its own.
Why the import path matters¶
Python, not slotscheck, decides which files can be imported,
based on how you start slotscheck.
python -m slotscheck adds the current directory to sys.path,
so modules there can be imported. Bare slotscheck does not.
So if you run slotscheck foo.py, foo is not importable.
In fact, if foo is also the name of an installed module,
import foo imports that instead!
In that case slotscheck refuses to run rather than check the wrong files.
Take this file tree:
src/
foo/
__init__.py
bar.py
Each command below tries to check foo/bar.py.
Command |
Result |
|---|---|
|
❌ |
|
❌ bare |
|
✅ |
|
✅ |
|
✅ |
The results are the same with -m foo.bar in place of the file path.
What about implicit namespace packages?¶
To pick the directory to suggest, slotscheck walks up from your file until
it reaches a directory without an __init__.py.
An implicit namespace package (PEP 420) has no __init__.py,
so slotscheck can’t tell it apart from an ordinary directory.
The suggestion then stops one level too deep. Given this tree:
src/
mycompany/ <- a namespace package: no __init__.py
tools/
__init__.py
slotscheck suggests src/mycompany, and with that path your code imports
as tools rather than mycompany.tools.
Pass --pythonpath src instead, so that modules get their proper names.
Why doesn’t slotscheck just add the right paths
to sys.path for me?
sys.path is global: changing it affects every import in the process.
A wrong guess wouldn’t merely fail to help—it would change how
your own code imports its dependencies, as the namespace package case
above shows. So slotscheck doesn’t guess.
Since you’ll probably need only a single entry, pass --pythonpath
(or set $PYTHONPATH) yourself; slotscheck suggests the likely directory.