DEV Community

abbazs
abbazs

Posted on • Edited on

3 2

How to add folders to python modules search path?

Consider you have development folder structure like this:

.
├── app
│   └── piccolo_conf.py
├── poetry.lock
├── pyproject.toml
├── README.md
└── work
Enter fullscreen mode Exit fullscreen mode

In this folder structure, app is the main application development folder. You want it to be clean. And you also want to write some experiment codes and test them to ensure that the code is meeting the requirement. While doing so, often it is required to refer to the code inside the app folder.

How to refer to the code inside the app folder while having the experiment code in the work folder?

Following code address that:

import sys
from pathlib import Path
# Path.cwd() - Get the current working directory
# Path(Path.cwd()).parent - Get the current working directory parent
# glob("app/**") - Get all the folders inside the app folder 
p = list(Path(Path.cwd()).parent.glob("app/**"))
sys.path.append(str(p[0].parent.absolute()))
for x in p:
    pth = str(x.absolute())
    # Exclude the folders that starts with __
    if not x.name.startswith("__"):
        sys.path.append(pth)
Enter fullscreen mode Exit fullscreen mode

Billboard image

Monitoring as code

With Checkly, you can use Playwright tests and Javascript to monitor end-to-end scenarios in your NextJS, Astro, Remix, or other application.

Get started now!

Top comments (0)

Imagine monitoring actually built for developers

Billboard image

Join Vercel, CrowdStrike, and thousands of other teams that trust Checkly to streamline monitor creation and configuration with Monitoring as Code.

Start Monitoring

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay