Story time?
One late evening in 2021, an intern called me. He said the coffee on his desk had gone cold. And his terminal was cluttered with error messages, and his sanity was hanging by a thread.
This wasn’t the ChatGPT/AI Era! Everyone ran to Stack Overflow and Reddit Forums.
I was like — Woah, woah, woah! Please explain to me what’s going on, and buddy was so tensed, he almost cried.
Fast-forward to today, I met him and this was the first memory we discussed. He laughed about it.
He works at Google and is seen as one of the best engineers there! (He says)
Anyway, Without further delay, let’s get into the article.
So, we did a screen share over Zoom and he showed me
“ModuleNotFoundError: No module named ‘libs_stu’”
And as soon as I saw this, I went like — Ah, I have seen it.
He exclaimed, I knew there was something wrong with this repository.
He had been wrestling with import issues all evening, trying to get Python to recognize his project’s modules. You know the drill:
- Running the script → Error.
- Tweaking
PYTHONPATH→ Still broken. - Adding
sys.path.append(...)everywhere → Feels dirty.
Until he realized
he needed a better way.
Something clean, reusable, and automatic.
And then, after way too much Googling and debugging, he still couldn’t figure out and therefore blamed the mono repo xD
I gave him a single command that changed everything:
export PYTHONPATH=$PYTHONPATH:$(find $(pwd)/src/py/**/BUILD -type f | sed 's/\/BUILD//' | tr '\n' ':' | awk '{print substr($0, 1, length($0)-1)}')So, what was it? Let’s break it down.
But, first, let’s understand the problem with PYTHONPATH
If you’ve ever worked on a monorepo, or a complex Python project with multiple submodules, you’ve probably encountered this:
Your code is spread across multiple directories, but Python doesn’t know where to find everything.
Sure, you can manually set PYTHONPATH:
export PYTHONPATH=/path/to/module1:/path/to/module2But,
1) What if you have dozens of modules?
2) What if their locations change?
You’ll either end up maintaining a messy, hardcoded path, or writing hacky scripts to fix it.
That’s where this magic one-liner comes in.
Let’s break this line by line, shall we?
Find all directories with a BUILD file
find $(pwd)/src/py/**/BUILD -type fStarts searching from the current directory ($(pwd)) inside src/py/
Looks for files named BUILD
-type f ensures we only get files, not directories
You can also do:
find /Users/username/projectmono/src/py/ -name "BUILD"Extract just the directory names
sed 's/\/BUILD//'- Removes
/BUILDfrom each result, so we’re left with just the module paths
Format the list into a colon-separated string
tr '\n' ':'Converts the list from newlines to colons (:), which is what PYTHONPATH expects
Remove the trailing colon (to prevent issues)
awk '{print substr($0, 1, length($0)-1)}'Append the generated paths to PYTHONPATH
export PYTHONPATH=$PYTHONPATH:$(...)Now, no matter how many modules my project has, this command automatically finds and adds them to PYTHONPATH.
⚠️ Potential Issues
- If you don’t have
BUILDfiles, this won’t work. You might need to modify it to find Python files instead (find -name "*.py").
In case we are meeting for the first time, come over here, it’ll be worth the roller coaster of articles that are gonna come up in the next few weeks.
Use this to get to know more about me — https://linktr.ee/shashwat_writes