Can't import top level packages in django project

Steven Tautonico :

I'm working on a django project that requires me to import code from another package. For exmaple, in my "home" app, I'm import from the "users" app. In my index.py view in the home app, I'm using from project.users import services. Running this from a python shell works fine, however, if I try to run python3 manage.py runserver, I get the error ModuleNotFoundError: No module named 'project.users'. I tried adding my project directory and its parent to my sys.path, however, nothing changes. I also tried using a different machine and a venv with no success. How would I be able to fix this?

Edit: I also tried running from project.users import services from the django shell (python3 manage.py shell) and I get the same import error.

Steven Tautonico :

I solved the problem by changing my imports from from project.users import services to from users.import services.

Also, to fix the red underlines in pycharm, just mark your project root ("project" folder in my example) as sources root (Right click > Mark directory as.. > Sources root).

(@BearBrown's answer, credit to him)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=18511&siteId=1