When running `run`, I get an import error: `Error ...
# general
s
When running
run
, I get an import error:
Error while finding module specification for 'package.package' (ModuleNotFoundError: No module named 'package')
When running
test
, the error is a simple
ModuleNotFoundError
. Has anyone ran into a similar issue?
w
hey @shy-island-79431!: this type of issue is usually due to missing source roots: https://www.pantsbuild.org/docs/initial-configuration#configuring-source-roots … do you have some configured for this layout?
s
this is what i have in my
pants.toml
:
Copy code
[source]
marker_filenames = ["setup.py"]
and when i run
./pants roots
, i get
Copy code
path/to/package/package
and
Copy code
path/to/package/package/src
w
@shy-island-79431: so, a source root is the portion of the directory structure that is “cut off”
and the remainder of the path to a file is what becomes its import path
so unless you have a directory structure
path/to/package/package/package/package
, a source root of
path/to/package/package
, will not let you import it as
package.package
if that makes sense
the source root is ~roughly “the directory that would go on the PYTHONPATH if you were running without pants”
s
however, i do have an import path
path/to/package/src/package/package.py
, so in that case, shouldn’t importing
package.package
work in this case?
since
path/to/package/src
is a known source root
w
if you have a file
path/to/package/src/package/package.py
, you’d need a source root
path/to/package/src
in order to import it as
package.package
, yea.
if that is all set up correctly, the next possibility is
__init__.py
files.
s
i currently have
__init__.py
files in
path/to/package/src/package
and
path/to/package/test
; however, they are empty in this case
also, i’m not sure if it’s relevant here, but previously we’ve been using
python setup.py develop
to run each of our libraries, this being the simplest of them
and that would run without issue. so i know the imports are correct at least in that context
w
which version is this? and if it’s 2.0.x, do you have dependency inference turned on (it’s on by default)
s
it’s v2.0.0, and it’s a fresh install, so it should be on
w
ok. does
./pants dependencies path/to/package/src/package/package.py
show what you would expect?
(or, actually: whichever target you were using
./pants run …
on)
s
it does:
path/to/package/src/package/package.py:../../package
and the same command for test shows a sane result as well
w
interesting. so, 1) source roots, 2) init.py files, 3) dependencies are probably ruled out? would be good to double check all of those, but that’s pretty mysterious.
if you have time to create a tiny reproduction repo or zipfile and file it as a pantsbuild ticket, it would help us to take a look, and be really appreciated
s
yeah, it’s pretty weird; i was going through the docs over and over to make sure everything was in line. i’m sure it’s small tiny detail somewhere that’s doing it. but yeah, i can do that. would that be through the github issues page?
also, thanks a lot for taking the time to help me out. i really appreciate it!
👍 1
h
That's what we're here for! 🙂
My best guess is indeed some source roots issue, but we'll get to the bottom of it either way.
s
I’m currently writing up a GitHub issue with all the info I’ve given here in a more concise and organized manner. Hopefully we can get figure it out.
❤️ 1
h
Thanks! Sorry for the trouble. It's either a bug or unclear documentation, presumably.
e
Wait, setup.py is being used to calculate the source root, right? That is at
path/to/package/setup.py
which says
path/to/package/src
is the 1st python package, aka
src
is the root package. That is definitely not what is intended.
w
@shy-island-79431 filed https://github.com/pantsbuild/pants/issues/11118 with a full sketch: thanks!
e
OK, I'll repeat this there. That is the 1st problem.
w
@shy-island-79431: i thought that you had mentioned that the output of
./pants roots
looked right, but yea: would be good to add that to the ticket as well
e
He mentioned the target specs looked right and those are in the ticket and they do not look right. I'll point that out too.
Aha - the "Using
setup.py
as a marker file" doc is the buggy thing here. That only works for projects with setup.py and a very specific structure.
h
Yes, that is right.
You can use
setup.py
as the marker file only if it happens to be exactly at the package root.
Thanks for the documentation edits, merged!
Will update them in the 2.1 docs as well