Import error in django project

I am using django framework, Here i have created the first_app application in django, i have created a simple view which will return hello world http responce,
I am trying to map this url in the urls.py, but it throws import_error by saying that " attempted relative import beyond top-level package", i have attached the project folder structure below for reference,

error image

In the future, please try to show complete error messages, and show them as text rather than a screenshot - it’s easier to read and work with that way, since we can copy and paste out of it for the sake of discussion. That also helps make sure we see the whole thing. To get a complete error message, scroll up to where it says Traceback (most recent call last):, and copy and paste the entire thing, and format it like a code block (Python’s stack traces are designed to be displayed in the terminal, so using code formatting makes sure things line up properly).

In order to support relative imports like this, the containing folder (the outer first_project) needs to be the top-level package. For example, assuming you start the program from the main.py that is outside of those folders, and it does an absolute import of something from that package, the rest should work with relative imports. You will want to make sure that the package is installed in that venv (the pycharm_venv), perhaps in “editable mode”. (I do not know how this works in Pycharm, I only know the command-line tools.)

You cannot directly treat a module inside your package as the driver script; that takes some setup work. Here is one of many possible references on Stack Overflow (but right now, the reference questions about import are a total mess and a lot of them have bad or outdated advice - trying to clean this up is one of my current projects):

The key point is that the .s that you use in an import statement (“up” the path for relative imports, as well as “down” the path to get sub-packages etc.) reflect a package hierarchy, not the actual directory hierarchy on your HD/SSD. It is possible to set up packages so that parts of them come from completely different locations (or even from other sources besides .py files). Therefore, code like from ..first_app import views will not naively start from the folder where that source file is located and look into containing folders to find the “parent”. It needs for the package to have been loaded first, and then it will look into locations that the package has determined.


Note: if you intended for the first_app and first_project to be separate packages that are not sub-packages of some common “parent” package (this is a legitimate choice), then you should use absolute import, not relative import in order to import from one to the other. The idea is that, if the packages are “separate” then there is no reason to assume that they will be put anywhere related to each other when they are installed. Using absolute import means that the usual mechanisms are used to figure out where one package is from the other - by name, and by searching the sys.path for the “usual suspects” of package locations.