ModuleNotFoundError for model class in django - django

I ran into the following ModuleNotFoundError error.
File "C:\django-project\CodingWithMitchBlog\demoproject\Blog\api\urls.py", line 3, in <module>
from demoproject.Blog.api.views import api_detail_BlogPost_view
ModuleNotFoundError: No module named 'demoproject.Blog'
'Blog' is my appname.
This is my project structure.

This might be due to incorrect import statements used in the files. If the import statements has the projectname prefixed try removing it and run again.
For example change:
from demoproject.Blog.models import ModelClass
to
from Blog.models import ModelClass
in the urls.py file

Related

ModuleNotFoundError: No module named 'core'

Django Version: 3.1.5
folder structure
So, I'm studying Django. When I try to generate random data for my project I get this error:
Traceback (most recent call last):
File "C:\PythonProjects\DJANGO\myblogsite\blog\management\commands\create_data.py", line 2, in <module>
from core.models import Category, Post, Comment
ModuleNotFoundError: No module named 'core'
Process finished with exit code 1
create_data.py
from django.core.management.base import BaseCommand
from core.models import Category, Post, Comment
from random import randint
import datetime
Has anybody a clue how to deal with this problem?
from django.core.management.base import BaseCommand
from blog.models import Category, Post, Comment
from random import randint
import datetime

Django+Cython import cython module in django app views [duplicate]

This question already has an answer here:
Python Package "No module named..."
(1 answer)
Closed 2 years ago.
a newbie to django and Cython. I am creating an app in django and need to import function in views.py from cythonized module. following is views.py inside my app.
from django.shortcuts import render
import sys
import numpy as np
import random
import math
from cython_node_val import node_val
def home(request):
return render(request,'Home.html',{"name":"user"})
def shortest_path1(request):
K=int(request.POST['number of layers'])
if ((K%2!=0) or (K < 0)):
return render(request,"shortest_path1.html",{'shortest_path1':"K must be an even integer"})
else:
......
Node_val=node_val(Hash,C,K) #node_val is from cython_node_val which is a .pyx file, Hash C and K
are defined in body after else statement.
sPath=np.zeros((K,3))
sPath[K-1,:]=Node_val[n-1,:]
for m in range(K-2,-1,-1):
sPath[m,:]=Node_val[int(sPath[m+1,1])]
return render(request,"shortest_path1.html",{'shortest_path1':sPath[:,3]})'''
the directory of my project is like following:
my app directory looks like this
cython_node_val.pyx works fine when importing into a normal .py file, but when doing the same inside views.py in my app it throws me following error
File "C:\Users\amit\projects\application_shortest_path\shortest_path\DS2P\urls.py", line 9, in <module>
from . import views
File "C:\Users\amit\projects\application_shortest_path\shortest_path\DS2P\views.py", line 6, in <module>
from cython_node_val import node_val
ModuleNotFoundError: No module named 'cython_node_val'
I believe if views.py is a python file and we can do operations, it should pull cython_node_val and associated functions. Where am i wrong?
Thanks for your time.
Use os.getcwd() to debug where you are running from your script views.py:
import os
print(os.getcwd())
ModuleNotFoundError: No module named 'cimport'
Then adjust the path to your needs
Is usually an error when you are trying to reference something that isn't in the python running path script + the path that you are giving inside your script.

ModuleNotFoundError: No module named 'app.myapp' In Django

File "C:\Users\user\app-rest\app\app\urls.py", line 4, in <module>
from app.myapp.views import UserViewSet
ModuleNotFoundError: No module named 'app.myapp'
Not sure what the issue is but when I comment out the import and the related views on my URL page, it works. Could it be from the folder being app\app\url being the same name twice? if it is how can I change that without screwing up the rest of my code?
settings are set up right because it usually works.
Thank everyone in advance.
This looks like a good use case for a relative import, if you are accessing the view from within the same django app, then replace the import statement with
from .views import UserViewSet
Reference

Cron job for Django File

I want to setup a Cron job to run a django file.
What I want to know is django-cron a good option for this? i.e. or are there any other apps that can be used?
If no, how to run a django file through command line ?
admin#ADMIN-PC ~/workspace/bolt (master)
$ python ./log/cron.py
Traceback (most recent call last):
File "./log/cron.py", line 3, in <module>
from account.models import UserProfile
ImportError: No module named account.models
I have set the following variable
admin#ADMIN-PC ~/workspace/bolt (master)
$ export DJANGO_SETTINGS_MODULE=settings
I keep getting these errors as the files that are being referenced have direct imports
from foo.models import *
Any help will be highly appreciated.
Custom Command
from django.core.management.base import BaseCommand, CommandError
import pdb
import datetime
from too.models import UserProfile
from foo.api import end_all_foo_conversations
class Command(BaseCommand):
# in minutes
def handle(self,*args,**options):
print datetime
The error I am getting while trying to run the command is as follows:-
'queryset': self.rel.to._default_manager.using(db).complex_fi
imit_choices_to),
AttributeError: 'str' object has no attribute '_default_manager'
I think you should write a custom management command and run it through manage.py.

ImportError: No module named models

I am going a tad crazy here. I keep getting this error: ImportError: No module named models and I am not sure why. Here is what I have found so far...
>>> from django.shortcuts import get_object_or_404, redirect
>>> from mystore.cart import cart
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/Jeff/django/mystore/cart/cart.py", line 3, in <module>
from mystore.cart.models import CartItem
ImportError: No module named models
>>>
I am not sure what's going on with this... line 3 in cart.py is this:
from mystore.cart.models import CartItem
If I try to do: from mystore.cart.models import CartItem it works fine...
Any suggestions?
Almost certainly, you have a circular dependency: mystore.cart.cart is importing mystore.cart.models, which in turn is trying to import mystore.cart.cart.
You should determine if both of those imports are necessary, and if either of them could be moved out of the global scope into a function or method.
Why are you doing from mystore.cart import cart? That should be just from mystore import cart.
Very early in the mystore.cart.models an error is occurring that's why nothing in models.py can be imported. The error can be a circular import, a conditional statement that's triggered during runtime but not at the command prompt or is happening inside something else your are importing at the beginning of models.py
You have to put a point before.
bad
from models import *
good
from .models import *
that means that is at the same level.