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

average-father-89924

03/28/2023, 12:30 PM
Hello, I want to work with jupyter notebooks for some data science stuff in pants. I followed this work-around and it almost solves everything. However, how do I access local code that is shared? My folder structure looks like the following structure:
Copy code
src/
├─ notebooks/
├─ libraries/
   ├─ library1/
I would like to access
library1
in my notebook. I tried to set source roots but that did not work out for me. Do you have any hints?
e

enough-analyst-54434

03/28/2023, 12:37 PM
Can you please provide the output of
pants roots
?
a

average-father-89924

03/28/2023, 12:37 PM
> pants roots
.
src
e

enough-analyst-54434

03/28/2023, 12:37 PM
Should be:
Copy code
src
src/libraries
You want to
import library1
- correct?
a

average-father-89924

03/28/2023, 12:38 PM
doing
import libraries.library1...
is also fine
e

enough-analyst-54434

03/28/2023, 12:38 PM
No, it shouldn't be. You should 1st know what your package structure should be / is intended to be. Once you have that settled, you just need to tell Pants the source roots.
Does that make sense?
a

average-father-89924

03/28/2023, 12:39 PM
I actually would prefer doing
import libraries.library1
since that mimicks how it works elsewhere in the repo.
e

enough-analyst-54434

03/28/2023, 12:40 PM
Ok, once you know what you intend the imports to be, the source roots should cut there. If that's what you have already, the issue is elsewhere.
👍 1
So, you're saying with roots of
.
and
src
you cannot
import libraries.library1
?
a

average-father-89924

03/28/2023, 12:41 PM
Correct
Copy code
ModuleNotFoundError: No module named 'libraries'
Is the error I am getting
e

enough-analyst-54434

03/28/2023, 12:41 PM
And from where? Within src/notebooks? Is your repo `pants tailor`'d?
👀 1
a

average-father-89924

03/28/2023, 12:42 PM
you mean executing
pants tailor ::
right?
e

enough-analyst-54434

03/28/2023, 12:42 PM
Correct - after source roots are correct.
a

average-father-89924

03/28/2023, 12:45 PM
should
pants tailor ::
create any output at all? it does not for me. I might have wrecked it 😮‍💨
e

enough-analyst-54434

03/28/2023, 12:46 PM
I suggest not being so coy for the remote support. Full output of commands (the command you ran plus all its output), is very very helpful always.
👍 1
a

average-father-89924

03/28/2023, 12:46 PM
> pants roots
.
src
> pants tailor ::
>
e

enough-analyst-54434

03/28/2023, 12:47 PM
Thanks. But,
git status
should show some BUILD files?
The
tailor
goal creates
BUILD
files in various directories - Pants needs these to function.
a

average-father-89924

03/28/2023, 12:48 PM
No, no BUILD files created but I already have BUILD files in the directories where I need them.
e

enough-analyst-54434

03/28/2023, 12:48 PM
Ok - great.
Back to
ModuleNotFoundError: No module named 'libraries'
- can you provide command line + full output for that?
a

average-father-89924

03/28/2023, 12:49 PM
Copy code
> import libraries
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[3], line 1
----> 1 import libraries

ModuleNotFoundError: No module named 'libraries'
e

enough-analyst-54434

03/28/2023, 12:50 PM
Ah, right - but ... how did you get there?
pants run
a notebook or
pants package
a PEX that you run or ... ?
a

average-father-89924

03/28/2023, 12:51 PM
pants run //src/notebooks/lab.py:python-default_main -- --notebook-dir src/notebooks
I am following how it is done in this repo.
e

enough-analyst-54434

03/28/2023, 12:51 PM
What does
pants filedeps --transitive src/notebooks/lab.py
say?
a

average-father-89924

03/28/2023, 12:52 PM
> pants filedeps --transitive src/notebooks/lab.py
src/notebooks/BUILD
src/notebooks/lab.py
e

enough-analyst-54434

03/28/2023, 12:52 PM
Ok - getting somewhere. That implies lab.py has 0 transitive dependencies (via
import
statements) on libraries - is that correct?
a

average-father-89924

03/28/2023, 12:53 PM
yes, I think that is correct because lab.py is only the entry point. the dependencies are defined in the BUILD
e

enough-analyst-54434

03/28/2023, 12:54 PM
Well, not correctly probably due to the evidence of the
filedeps
output above. Can you share the BUILD?
a

average-father-89924

03/28/2023, 12:54 PM
jupyter_lab(name="python-default", requirements=[
"numpy==1.24.2",
"pandas==1.5.3",
"structlog==22.3.0",
"python-dateutil==2.8.2",
"jupyterlab==3.4.8"
])
e

enough-analyst-54434

03/28/2023, 12:55 PM
Where does
jupyter_lab
come from?
That is not a Pants thing IIRC.
a

average-father-89924

03/28/2023, 12:55 PM
It is this file
e

enough-analyst-54434

03/28/2023, 12:55 PM
Aha - Ok, so you want support on a non-Pants thing.
You made that semi-clear I guess. I did not page that in.
I'll let others chime in then. I don't have enough time this am before I leave here to go read that repo's code.
a

average-father-89924

03/28/2023, 12:56 PM
I don't exactly know where pants starts and it ends. Sorry. This repo has been shared by "officials(?)" here on slack so I thought it was deemed as THE jupyter solution
Thanks John I appreciate your help!
e

enough-analyst-54434

03/28/2023, 12:57 PM
Its a third-party provided plugin. So some Pants Slack folks have developed it, but not the core Pants maintainers. I am of the latter group and ignorant of this stuff I'm afraid.
👍 1
I'm pretty sure da-tubi is @bored-energy-25252.
r

refined-addition-53644

03/28/2023, 1:06 PM
I feel like this is more python/jupyter issue than pants. Pants is just providing a virtual env for jupyter to be run inside it. Are you able to import those local modules outside of pants by starting a jupyter server?
a

average-father-89924

03/28/2023, 1:17 PM
If I install in the same root folder jupyter via pip and then start it I can import the
src
folder via
import src
.
I also tried to use python_sources on the
libraries
folder but I since this is outside the folder the BUILD file resides in, it tells me I am not allowed to include it.
(I am pretty sure that I missed to put a basic configuration somewhere. So it's probably neither a pants, nor python/jupyter issue but I am the issue.)
r

refined-addition-53644

03/28/2023, 1:26 PM
You don't need to use pants targets and hence BUILD files for the code you are writing if I understand correctly the purpose of this plugin. The plugin gives you just a virtual env generated from pants provided lockfile. Bit hackish way is to append
src
to your sys.path at the top just before you import these libraries. I have had issues with importing such local libraries .
b

bored-energy-25252

03/29/2023, 3:59 PM
If you want to work with shared code, here is a sample repo: https://github.com/da-tubi/pants-mininal-lab
2 Views