Problem with Django: No module named 'myapp' - python

Good afternoon,
I'm new to Django and I'm trying to follow a tutorial. However, when running the server, the following error pops up:
File "C:\Users\Jorge\PycharmProjects\web\mysite\mysite\urls.py", line 18, in <module>
from myapp.views import *
ModuleNotFoundError: No module named 'myapp'
This is a screenshot about how my files are right now:

Your myapp folder should be inside in your mysite folder

Do the following:
Move myapp to C:\Users\Jorge\PycharmProjects\web\mysite\
In mysite\settings.py, add an entry as myapp.apps.MyappConfig in the INSTALLED_APPS list.
Now do python manage.py runserver from C:\Users\Jorge\PycharmProjects\web\mysite\.

Go to 'mysite' --> 'settings.py'
write as it is, Under
INSTALLED_APPS = [
'myapp',

Related

ModuleNotFoundError : no module found named <module_name>django

I am trying to make a web application using django. so in the website folder, I made another app named U0.
I made sure to add it in the settings section of the website. but when I try to migrate the manage.py file it throws an error saying, No Module found named U0django
In order to create a new app in Django, you have to use the Django builtin command to create the correct structure of an app for you like this:
django-admin startapp u0
btw I believe if you want to create it on your own you have to create a file named apps.py in your app folder and add these lines to that to help Django identify it as an app :
from django.apps import AppConfig
class U0(AppConfig):
name = 'u0'

No module named myapp

I have this project directory structure:
.
.pylintrc
|--myproj .
|--myapp
|--myproj (settings.py is here)
__init__.py
manage.py
.
In settings.py, INSTALLED_APPS I have the first entry 'myapp'.
From the root folder (containing .pylintrc), I call
$ DJANGO_SETTINGS_MODULE=myproj.myproj.settings pylint myproj --load-plugins pylint_django
However, I get error no module named 'myapp'. If I change the INSTALLED_APPS entry to 'myproj.myapp', then it is able to continue, but now I'm unable to start the project normally with manage.py runserver.
pastebin myproj.settings
What am I doing wrong, and what can I do to proceed?
Likely your settings.py is improperly configured, you need to do something like this:
settings.py
INSTALLED_APPS = [
...
'myapp.apps.MyappConfig',
...
]
If you open the apps.py file in your myapp directory you'll see the config class that should be included in INSTALLED_APPS
The same problem happened to me and I find my solution.
In My Case, I missed my urls.py file.
re_path(r"^chaining/", include("smart_selects.urls")),
to
re_path(r"^chaining/", include("packages.smart_selects.urls")),

Django - app in subfolder

I've created new django app in a subdirectory using the command:
python manage.py startapp appName subFolder/appName
but if I try to add this app to INSTALLED_APPS at the end of the list I see the following error:
ImportError: No module named appName
Does anyone know what I am doing wrong?
You need to include the subfolder when you add the app to INSTALLED_APPS, for example:
'subFolder.appName',
or
'subfolder.appName.apps.AppNameConfig',
I tried different options but no one can solve that issue.
Finally, I found a solution.
Simply go to the subFolder/appName/app.py file and
replace this line name = 'appname' with name = 'subfolder.appname'
And then you can simply add to the installed apps list.
'subfolder.appname'

Running Unit Test w/ App Directory In Django 1.6+

I have my Django project structured so all the apps are in the apps directory.
/manage.py
/apps/events/tests.py
/apps/contacts/tests.py
This worked until Django 1.6, but now when I try to run
./manage.py test events
I get the following error:
File "/Users/josephmisiti/mathandpencil/projects/xxxx/lib/python2.7/site-packages/django/test/runner.py", line 63, in build_suite
tests = self.test_loader.loadTestsFromName(label)
File "/usr/local/Cellar/python/2.7.6_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 91, in loadTestsFromName
module = __import__('.'.join(parts_copy))
ImportError: No module named events
I my settings.py, I have the following under INSTALLED_APPS
'apps.contacts',
'apps.events',
Anyone know how to fix this one ?
In 1.6 the command is:
$ ./manage.py test events
The docs assume your folder structure has every app in the root of the project folder.
But if you have structured your folders differently you will need to match that in the command.
So from your project root, if you have a folder named apps and in there an app named events your command will look like:
$ ./manage.py test apps.events

How do I get all files for my django app into one folder?

I currently have a Django app named reserve. In the folder named "reserve" I have most of the content of my app (views.py, urls.py, models.py, templates folder). However, I have a folder outside of "reserve" named "booking" that has only my settings.py. I tried consolidating by putting the settings.py in "booking" into "reserve" but I seem to be getting an error. Any advice on how to have only one folder with all contents?
The error I get is: ImportError: Could not import settings 'booking.settings' (Is it on sys.path?): No module named booking.settings
overall project folder
booking folder
settings.py
init.py
reserve folder (the app)
views.py
admin.py
models.py
...
what version of django are you using? what command are you running ? assuming that you are just trying to run a command using manage.py you could just chnage manage.py to reflect this if you are going to permanently keep this project structure
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
you should change os.environ.setdefault to the correct directory for your settings.py
I'm assuming you have manage.py at the overal project directory? I'm also assuming you want to just runserver.
It seems you moved the settings.py file outside the overal project directory. If you do this, you'll have to specify the settings file path when you do runserver, or you can also export the django settings module to point to your new settings path (using dot notation). In this case, I think it would be (in the command line):
export DJANGO_SETTINGS_MODULE=<overal-project-folder-name>.booking.settings
python manage.py runserver

Categories