I have a simple Django project (namely the one from this tutorial).
The main part of the tree looks like this:
.├── env├── proj│ ├── proj│ │ ├── __init__.py│ │ ├── settings.py│ │ ├── urls.py│ │ └── wsgi.py│ ├── api│ │ ├── __init__.py│ │ ├── serializers.py│ │ ├── urls.py│ │ └── views.py│ ├── base│ │ ├── __init__.py│ │ ├── admin.py│ │ ├── apps.py│ │ ├── models.py│ │ ├── tests.py│ │ └── views.py| ├── __init__.py│ └── manage.py└── README.md
In the tutorial the outer proj
folder is the root.
In api/views.py is the following import: from base.models import Item
When I print sys.path
in settings.py, it contains /path/to/wrapper/proj
.
But I would like my root folder to be one level higher.
This is what I did:
- go to Settings> Project structure and change content root to
/path/to/wrapper
- append another
.parent
toBASE_DIR
in settings.py
(nowBASE_DIR = Path(__file__).resolve().parent.parent.parent
) - prepend
proj.
to imports
(nowfrom proj.base.models import Item
)
But that does not work: ModuleNotFoundError: No module named 'proj.base'
When I print sys.path
in settings.py, it still contains /path/to/wrapper/proj
.
Where does that actually come from? And how can I change it?
The only effect of the content root in the settings seems to be on syntax highlighting.
Since I changed it, PyCharm considers imports with prepended proj.
as correct.
I already use this structure in a different project (see here).
There it works. But I do not see the difference between the two projects.
EDIT: It was recommended in a comment to change the source root.
But that change had no effect (except that the root folder is now blue).
(Apparently the source root is implicitly the content root, unless specified otherwise.)