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
Related
I´m trying to make a cron inside my django app with django-crontab==0.7.1,.
Within my project settings installed apps, crontab app first, and then my app, both registered.
In my cron.py:
from .models import Url
urls=Url.objects.values_list('url')
def foo(url):
if url not in urls:
link= Url(
url=url,
)
link.save()
foo('https://example.org')
Also tried to put inside cron.py, but it doesn´t make sense since both are inside same app
from foo.models import Url
Also init.py inside app
My models.py inside foo app, after migrations ran
from django.db import models
class Url(models.Model):
url=models.URLField('url',blank=False,null=False)
but still getting ModuleNotFoundError: No module named 'foo'
Sorry if this has been asked before, but nothing watched yet gives me the answer
Thank you
I am working on a Django project. I am working on the register page. When I try to import my register/views.py to my mysite/urls.py file I get an error message. ModuleNotFoundError: No Module named 'register'. Both files are are in the same directory.
from django.contrib import admin
from django.urls import path, include
from register import views as v
Adding full exception message
Try the following:-
from . import views
Please add a blank __init__.py file in the register folder.
Only then python will understand that register is an importable package
Edit:
After seeing the exception, it looks like a working directory issue in pycharms. Please try the fix mentioned in this link
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
I have a Django model with some static methods. I'd like to call the methods from outside the application (cronjob).
The model I have:
class Job(models.Job):
#Irrelevant information
#staticmethod
def methodIwantToCall():
#statements
I have the following python file that I'm using for the cron job:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from myapp.models import Job
Job.methodIwantToCall()
At first, I was having an error about DJANGO_SETTINGS_MODULE not being set and I fixed that, however, now I have the following error: No module named myapp.utils
I feel like I'm doing something that I'm not supposed to do. So how do I call that static method the way I want it to be called?
EDIT: It looks like the paths are getting messed up when I'm importing from outside Django. For example, I have an import in my models file, when I call the cron file it fails importing with the message ImportError: No module named myapp.utils even though it's working.
The proper solution is to create custom manage.py command.
Assuming your cron job code resides in the same directory as your settings file, use the following setup code at the beginning:
from django.core.management import setup_environ
import settings
setup_environ(settings)
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.