How to set up an app folder outside a project folder?

Hi all,

I would like to set up a dev environment where I can keep an app folder outside the project folder. So, instead of having something like this:

.
├── config
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── db.sqlite3
├── manage.py
└── polls
    ├── __init__.py
    ├── admin.py
    ├── apps.py
    ├── migrations
    │   └── __init__.py
    ├── models.py
    ├── tests.py
    ├── urls.py
    └── views.py

I would have the polls app folder at the same level of the project folder, like this:

.
├── polls
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
└── sample-project
    ├── config
    │   ├── __init__.py
    │   ├── asgi.py
    │   ├── settings.py
    │   ├── urls.py
    │   └── wsgi.py
    ├── db.sqlite3
    └── manage.py

The reason behind this is: I want to develop polls as a reusable app without having to copy/paste code from polls every time I need to build a new version of it.

After reading the docs and having a look around the internet, I figured that the polls folder should be in the Python path. My problem is that even though I managed to put polls in the python path, I still get a ModuleNotFound error. It seems to me that I am missing some configuration and asking here about this is my last resort :sweat_smile:

Thats the code I added to to settings.py for inserting the polls dir on the path:

# Build paths inside the project like this: BASE_DIR / 'subdir'.

BASE_DIR = Path(__file__).resolve().parent.parent

POLLS_DIR = BASE_DIR.resolve().parent / 'polls'
if POLLS_DIR not in path:
    path.insert(0, POLLS_DIR)

Thanks in advance,
Adriano

If you really want to make it a reusable app, then you might want to look into making it an installable package. (See Packaging Python Projects — Python Packaging User Guide to get started.) You can ignore all the stuff about uploading to PyPI, and just install it into your virtual environment.

You can also look at other installable packages designed to be used in Django to see how they handle some things.

Once it’s installed in the virtual environment, then you don’t need to worry about messing with the path or worrying about which directory it resides in.

(I’ve done this twice so far, and it works really well for us.)

2 Likes

Hi Ken,
Thank you so much for the quick response, I really appreciate it :slight_smile:

Your suggestion worked like a charm!

For those interested, here is what I have done after reading the docs and following @KenWhitesell 's advice:

I created yet another folder to house both polls and the necessary configuration files, so the tree now looks like this:

.
├── django-polls
│   ├── LICENSE
│   ├── README.md
│   ├── polls
│   │   ├── __init__.py
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── migrations
│   │   │   └── __init__.py
│   │   ├── models.py
│   │   ├── tests.py
│   │   ├── urls.py
│   │   └── views.py
│   ├── pyproject.toml
│   └── setup.cfg
└── sample-project
    ├── config
    │   ├── __init__.py
    │   ├── asgi.py
    │   ├── settings.py
    │   ├── urls.py
    │   └── wsgi.py
    ├── db.sqlite3
    └── manage.py

Once I was happy with the configuration files necessary to package my polls app, I made sure my virtual environment was activated and just ran pip install -e <pathToWhereSetup.cfgLives> . I then ran the django server to make sure it was working :smiley:

All the changes I was doing in the polls code located at django-polls/polls are now being picked live by the server. I am very very happy!

Many thanks again, Ken!