[Django] Guide package path problem

Supplement to record a question about the path of the package guide. A BASE_DIR path is provided in the Django configuration file for file location, and we print it out
settings.py

import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

print(BASE_DIR)

Found that it is an inner directory (the main application in the project)

It obtains the absolute path of the configuration file according to os.path.abspath( file ), and then takes the upper-level directory twice through os.path.dirname() to locate the inner directory.

If you want to add another guide package path at the same inner level, you can use sys.path.insert() to insert

import os
import sys

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(
    0,
    os.path.join(
        os.path.dirname(BASE_DIR), 'apps'
    )
)

If you have inserted the guide package path in the configuration file, but pycharm does not automatically prompt, you can try to right-click the directory name of the guide package and mark it as Sources Root

Guess you like

Origin blog.csdn.net/qq_39147299/article/details/108537735
Recommended