0

This is my project structure:

.
├── connectapp
│   ├── __init__.py
│   ├── __pycache__
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── djangoproject
│   ├── __init__.py
│   ├── __pycache__
│   ├── asgi.py
│   ├── djangoproject.sqlite3
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── djangoproject.sqlite3
├── hackerapp
│   ├── Controllers
│   ├── Models
│   ├── Serializers
│   ├── __init__.py
│   ├── __pycache__
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   └── tests.py
├── manage.py
└── requirments.txt

When I do below being in top level folder:

python3 manage.py migrate hackerapp

I am getting this:

djangoproject/djangoproject/urls.py", line 19, in <module>
    from ..hackerapp.Controllers import PersonViewSet, DepartmentViewSet
ImportError: attempted relative import beyond top-level package

To me looks import should work but it's not, can someone tell me why?

mzjn
  • 48,958
  • 13
  • 128
  • 248
Arie
  • 3,041
  • 7
  • 32
  • 63
  • Does this answer your question? [Import a module from a relative path](https://stackoverflow.com/questions/279237/import-a-module-from-a-relative-path) – Josh Friedlander Jun 12 '22 at 06:58
  • For the relative imports to work correctly, in this case, you may need to run the script with the [`-m` flag](https://docs.python.org/3/using/cmdline.html#cmdoption-m). Otherwise your import can be done as `from hackerapp.Controllers import PersonViewSet, DepartmentViewSet` – Billy Jun 12 '22 at 07:01
  • @Billy if I do your form import I am getting: `File "/djangoproject/djangoproject/urls.py", line 19, in from hackerapp.Controllers import PersonViewSet, DepartmentViewSet ImportError: cannot import name 'PersonViewSet' from 'hackerapp.Controllers' (unknown location) ` – Arie Jun 12 '22 at 07:11
  • @Josh Friedlander not exactly I think from this link is what Billy said above, look at my comment, I am till getting error – Arie Jun 12 '22 at 07:12
  • I think you should start by looking at similar questions: https://stackoverflow.com/search?tab=newest&q=%22attempted%20relative%20import%20beyond%20top-level%20package%22 – mzjn Jun 12 '22 at 07:18
  • @mzjn ive looked and seems like Billy's solution from above should should but it's not. Where's the issue ? – Arie Jun 12 '22 at 07:32

1 Answers1

0

I guess (?) migrate hackerapp are cli params to the manage.py script. The error means that the top level folder is not a package. To make it into a package add an (empty) __init__.py file then from that folder run:

python -m manage migrate hackerapp # note no .py
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361