Hello Everybody, I'm Harrys, currently trying to s...
# general
f
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
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
Thanks stuhood šŸ™‚ I've actually already had a look at that page, but I'll read it again more carefully.
w
ah, so you tried using the marker files strategy? what is different in your case?
f
Yes I did that. My case looks like this: https://github1s.com/zwarag/pantstest
w
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
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
no, your BUILD files actually look fine.
right: so there are no roots for your libs then
f
so libs should also have roots?
w
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
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
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
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
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
yeah maybe ^^
I'm not entirely sure either šŸ™‚
w
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
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
cool.. i think that you get the gist then šŸ˜ƒ
f
Yeah thanks a lot stu, that cleared some fog šŸ˜„
šŸ‘ 1
w
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
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
mm, yea: got it.
thanks! iā€™ll see if i can expand on IDE loading too.
f
you're welcome šŸ™‚
h
Hi @fancy-stone-87037, just wanted to check in on your progress? Happy to help if needed.