https://pantsbuild.org/ logo
#general
Title
# general
s

shy-island-79431

11/09/2020, 10:47 PM
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

witty-crayon-22786

11/09/2020, 11:00 PM
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

shy-island-79431

11/09/2020, 11:09 PM
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

witty-crayon-22786

11/09/2020, 11:10 PM
@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

shy-island-79431

11/09/2020, 11:13 PM
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

witty-crayon-22786

11/09/2020, 11:14 PM
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

shy-island-79431

11/09/2020, 11:15 PM
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

witty-crayon-22786

11/09/2020, 11:22 PM
which version is this? and if it’s 2.0.x, do you have dependency inference turned on (it’s on by default)
s

shy-island-79431

11/09/2020, 11:23 PM
it’s v2.0.0, and it’s a fresh install, so it should be on
w

witty-crayon-22786

11/09/2020, 11:24 PM
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

shy-island-79431

11/09/2020, 11:26 PM
it does:
path/to/package/src/package/package.py:../../package
and the same command for test shows a sane result as well
w

witty-crayon-22786

11/09/2020, 11:28 PM
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

shy-island-79431

11/09/2020, 11:30 PM
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

happy-kitchen-89482

11/09/2020, 11:43 PM
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

shy-island-79431

11/09/2020, 11:50 PM
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

happy-kitchen-89482

11/10/2020, 12:35 AM
Thanks! Sorry for the trouble. It's either a bug or unclear documentation, presumably.
e

enough-analyst-54434

11/10/2020, 1:14 AM
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

witty-crayon-22786

11/10/2020, 1:15 AM
@shy-island-79431 filed https://github.com/pantsbuild/pants/issues/11118 with a full sketch: thanks!
e

enough-analyst-54434

11/10/2020, 1:15 AM
OK, I'll repeat this there. That is the 1st problem.
w

witty-crayon-22786

11/10/2020, 1:16 AM
@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

enough-analyst-54434

11/10/2020, 1:18 AM
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

happy-kitchen-89482

11/10/2020, 2:15 AM
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