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

fancy-stone-87037

03/24/2021, 8:07 PM
Hello Everybody, I'm Harrys, currently trying to set up my first bigger Python project and I think pants is a really good idea. Currently, trying out to understand how to set up 2 applications with two libraries. If anybody could help me understand how I can import the lib into the app, I'd be really happy. I've read the example with the "utils" folder, but my setup is a little different. I think I have to set the PYTHONPATH (which I tried) but it's not quite working yet.
👋 1
w

witty-crayon-22786

03/24/2021, 8:11 PM
hey Harrys! welcome.
the relevant keyword is probably “source root”: source roots are how Pants decides how to build your PYTHONPATH: see https://www.pantsbuild.org/docs/source-roots
f

fancy-stone-87037

03/24/2021, 8:16 PM
Thanks stuhood 🙂 I've actually already had a look at that page, but I'll read it again more carefully.
w

witty-crayon-22786

03/24/2021, 8:20 PM
ah, so you tried using the marker files strategy? what is different in your case?
f

fancy-stone-87037

03/24/2021, 8:28 PM
Yes I did that. My case looks like this: https://github1s.com/zwarag/pantstest
w

witty-crayon-22786

03/24/2021, 8:28 PM
for the record: you do not need to set the PYTHONPATH explicitly: pants will compute it
@fancy-stone-87037: the first thing i see is that you don’t have a source root for
lib
… what does running
./pants roots
report?
f

fancy-stone-87037

03/24/2021, 8:30 PM
I think what I'm missing is in my BUILD files, I thought I need to have anything other than
python_library
. I wrongly assumed that applications should be
python
without the
_library
image.png
w

witty-crayon-22786

03/24/2021, 8:30 PM
no, your BUILD files actually look fine.
right: so there are no roots for your libs then
f

fancy-stone-87037

03/24/2021, 8:31 PM
so libs should also have roots?
w

witty-crayon-22786

03/24/2021, 8:31 PM
any code that should be imported, yea. the source root is how pants chooses the pythonpath
so i think that setting:
Copy code
[source]
root_patterns = [
  '/app/*',
  '/lib/',
]
might do it…?
then re-run
./pants roots
f

fancy-stone-87037

03/24/2021, 8:32 PM
I thought roots are only for the applications as I would never "release" the libs in a standalone matter like pypi, I'd like to have them "bundled" into the app1 or app2 when I want to release app1 or two
w

witty-crayon-22786

03/24/2021, 8:33 PM
nope… you need them for all importable code.
maybe the description at the top of https://www.pantsbuild.org/docs/source-roots#what-are-source-roots is unclear in that regard…?
f

fancy-stone-87037

03/24/2021, 8:34 PM
mhmm, I see, when I add the libs I get all of it.
Copy code
zwarag@DESKTOP-0J42R57:~/repos/ptest$ ./pants roots
21:33:04.97 [INFO] initializing pantsd...
21:33:05.13 [INFO] pantsd initialized.
app/app1
app/app2
lib/adder
lib/subtractor
w

witty-crayon-22786

03/24/2021, 8:36 PM
so, that’s not quite right.
a root
lib/adder
is effectively a PYTHONPATH entry
lib/adder
…oh. actually, yea… maybe that is what you want?
f

fancy-stone-87037

03/24/2021, 8:37 PM
yeah maybe ^^
I'm not entirely sure either 🙂
w

witty-crayon-22786

03/24/2021, 8:37 PM
a root
lib/adder
for a file
lib/adder/adder.py
would result in the file being importable as
from adder import add
it doesn’t get you much namespacing, but it’s valid.
if you made
lib
a source root, then you would import
lib/adder/adder.py
as
from adder.adder import add
so a question of how much namespacing you want.
f

fancy-stone-87037

03/24/2021, 8:38 PM
yeah so in my "real" project I would have two libts that already have different namespaces and are unrelated, so I think I actually do want it that way
w

witty-crayon-22786

03/24/2021, 8:38 PM
cool.. i think that you get the gist then 😃
f

fancy-stone-87037

03/24/2021, 8:39 PM
Yeah thanks a lot stu, that cleared some fog 😄
👍 1
w

witty-crayon-22786

03/24/2021, 8:48 PM
if i may: where do you think you got the impression that you only need source roots for apps? somewhere on https://www.pantsbuild.org/docs/source-roots ? i’d like to make some edits to the docs.
f

fancy-stone-87037

03/24/2021, 8:49 PM
Well I come from the javascript world and had an idea how things could work. I dont think I really got it from the documentation.
What would help tho is to have example of commands what one would normally use. The documentation is very well orgranized in topics but I think a little "cross concerning" would help.
Some questions that went through my head that might help: Alright I've set up build files. They are empty. Did it even work? Am I now able to run one of the apps? My IDE (VSCode) is not able to show me that the lib is imported in the application. Mhmm probably something wrong with the PYTHONPATH that has been mentioned.
w

witty-crayon-22786

03/24/2021, 8:53 PM
mm, yea: got it.
thanks! i’ll see if i can expand on IDE loading too.
f

fancy-stone-87037

03/24/2021, 8:55 PM
you're welcome 🙂
h

happy-kitchen-89482

03/27/2021, 9:37 PM
Hi @fancy-stone-87037, just wanted to check in on your progress? Happy to help if needed.
5 Views