2

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.

  • 1
    did you try to remove the `project` and just `from users import services`? – Brown Bear Feb 28 '20 at 22:41
  • @BearBrown So I removed every instance of "project" from all imports and it worked, but (you're a life saver). However, my IDE (pycharm) shows red underlines (unresolved reference). Any chance you know the solution to that? – Steven Tautonico Feb 28 '20 at 22:48
  • probably it help you, i use sublime and vim sorry https://stackoverflow.com/questions/38342618/pycharm-not-recognizing-django-project-imports-from-my-app-models-import-thing – Brown Bear Feb 28 '20 at 22:55
  • 1
    @BearBrown Yep, its fine, I got it. Just needed to reconfigure my sources. You're a god, thanks!!! – Steven Tautonico Feb 28 '20 at 23:01

2 Answers2

1

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)

0

Every django project comes with a settings.py file, and django will refuse to run, or load models, until that settings.py file is loaded.

settings.py file needs to be in a module and part of your current PYTHONPATH, and env. variable DJANGO_SETTINGS_MODULE should be set to it.

For example:

# assuming your file is under /myproject/mywebsite/settings.py
export PYTHONPATH=/myproject/
export DJANGO_SETTINGS_MODULE=mywebsite.settings

Edit:

If you are in a virtualenv you need to activate it before you can run ./manage.py 'command'

source path/to/your/virtualenv/bin/activate
AzyCrw4282
  • 7,222
  • 5
  • 19
  • 35