Can't runserver, ModuleNotFoundError[Django] - django

I'm trying to work with django, but I can't runserver it shows an error like this
"ModuleNotFoundError: No module named 'music.apps. MusicConfig'; 'music.apps' is not a package"
Note: I'm using python3.

Probably your music/apps directory doesn't contain __init__.py file. Create and empty one and apps will become a package

Related

Django: I get a [relation "auth_group" does not exist] error after syncdb

I started a new Django 1.8 project and realized that I missed something (i had done the initial migrations). I dropped the database (postgreSQL) and deleted migration folders from all my apps in order to start from scratch.
Now, when I 'syncdb' I get this error:
django.db.utils.ProgrammingError: relation "auth_group" does not exist
and when I makemigrations I get this:
No changes detected
What am I doing wrong?
Probably you should try to create migrations modules (folders named migrations with empty file named __init__.py inside of each directory) for your apps. And then run manage.py makemigrations again.
The problem is on no changes detected. Please execute these commands with your app name. I guess you didn't add it (just like the mistake I did):
python manage.py makemigrations myappname
python manage.py migrate myappname
The above error occurs when you have django.contrib.admin among the installed applications.
Run these commands in their respective order.
**
./manage.py makemigrations
./manage.py migrate auth
./manage.py migrate**
That worked for me perfectly.
Doing ./manage.py migrate auth first didn't work for me, and every ./manage.py command was throwing this error. My problem was that I was doing stuff with the Group manager in module scope.
If you have code like this in module scope:
customers_group = Group.objects.get(name='customers')
Move it inside a function that is called at runtime instead.
def xyz():
...
customers_group = Group.objects.get(name='customers')
I had the similar problem with Django2.2 migrations. I will post what helped in case someone is looking to fix this.
I commented out all urls to apps(like my_app.urls, your_app.urls) in main project urls.py and then ran makemigrations, it worked.
I think this error is due to some forms/views referring to model/fields that are not yet created. It seems django traverses urls.py to before making migrations
It can be either:
one of the pip dependencies from requirements.txt was using South
had this error when running tests which do migration in Django 1.8. Found the lib with issue by running tests in verbose mode. Consider upgrading the library to newer version.
manage.py test -v 3
one of the /migrations folder might still has old South migrations files.
It can be because others are still adding migrations when you are trying to upgrade Django. Use the following to make sure that the expected migrations files are present in each app.
manage.py showmigrations
One of your paths ("pointing urls.py on your core folder along with the settings.py") makes that problem occur importing django.contrib.auth and directly using methods and properties of "auth" after calling those views
Remove all migrations except "init.py" of each apps
Go to projects urls.py and comment out all the paths
run "heroku run python manage.py makemigrations"
run "heroku run python manage.py migrate"

Cannot start any django app

I am a newbie at Django and everytime I try to run
python panel/manage.py startapp %app% (panel is my project) it gives me the error:
Error: '%app%' conflicts with the name of an existing Python module and cannot be used as an app name. Please try another name.
Am I doing something wrong?
Surely companies or contacts or stats is not the name of an existing Python module?
This is a fun one - your project and your app need to have different names. You probably created a project, then tried to startapp with the same name.
I was confused as well, until I realized that a Django project is a container for applications; this sequence makes it a bit clearer:
# first create a Project (container).
django-admin.py startproject Project
# create multiple apps
cd Project
python manage.py startapp polls
python manage.py startapp hello
...
Perhaps you need to
cd panel
python manage.py startapp yourappname
I'm not sure running the command from a directory above your project will work properly.
I had the same issue because I was trying to "restart" my app after carrying out changes, but startapp is meant to be used once to create a new app. To view changes, syncronize app with DB with python manage.py migrate and restart the server with python manage.py runserver instead.
From the django-admin docs
(manage.py does essentially the same thing as django-admin)
startapp <app_label> [destination]
django-admin startapp
Creates a Django app directory structure for
the given app name in the current directory or the given destination.
By default the directory created contains a models.py file and other
app template files. (See the source for more details.) If only the app
name is given, the app directory will be created in the current
working directory.
If the optional destination is provided, Django will use that existing
directory rather than creating a new one. You can use ‘.’ to denote
the current working directory.
For example:
django-admin startapp myapp /Users/jezdez/Code/myapp
This message is displayed if you run "startapp" twice with the same app name. As pointed out above by the OP it doesn't reload the app, it creates one.
You should choose different names for your project and app in Codes:
django-admin startproject **my_project**
python manage.py startapp **my_app**
You need to create the directory before using the commands. Suppose you want a polls app inside apps folder.
mkdir apps apps/polls
python manage.py startapp polls apps/polls
I guess maybe you have already created the app's dir in panel dir manually. The command 'startapp' is to create an app automatically. If you already have one there, it fails.
I reproduced the issue and there's actually something not working as I expected.
I wonder if we stumbled upon a Django's bug, or a limitation that I don't understand.
Having a project called "project" and an empty folder app/newapp
…I tried:
python manage.py startapp newapp apps/newapp
It returns:
CommandError: 'newapp' conflicts with the name of an existing Python module and cannot be used as an app name. Please try another name.
But if I target ANY other route in which the last folder is not called the same name as the app I'm starting, it works.
So I ended up doing:
python manage.py startapp newapp apps/main
Using Django 2.1.3.
if you want to make an empty directory that will contain your new app
project-dir
└── blog
├── __init__.py
├── ...
├── blog-ext #this empty dir that will contain the new app
└── views.py
so instead of typing :
python manage.py newapp blog/blog-ext
it should be :
django-admin startapp newapp blog/blog-ext
Try classic "mysite" or "myproject". You can delete it anytime you want, so if it will accepted, then all your privious ideas conflict with Python modules.
Edit: I tried all your ideas, there was no error for me. So, if you installed support libraries or modules for django, then some of them can contains such names.
this error is because of the name conflicts between the app name and project name.you had given same name for your app and project .your project and app need to be different name .if you had given the same name the above mentioned error will occur .
understand the difference between app and project
Projects vs. apps
What’s the difference between a project and an app? An app is a Web application that does something – e.g., a Weblog system, a database of public records or a simple poll app. A project is a collection of configuration and apps for a particular Web site. A project can contain multiple apps. An app can be in multiple projects.
first create the project.
then create the app.
NOTE: name for app and project should be different
first create a project with projectname
django-admin.py startproject Projectname .
Then create app with appname. (to create your app make sure you are in the same directory manage.py and type this command)
python manage.py startapp Appname
It's the process how I got my doubt clear.
First, I created a directory inside my project directory and put __init__.py, models.py, admin.py, apps.py & views.py.
Then I ran python manage.py runserver & It work well.
Then as suggested on that page I used startapp command. I got this error :
CommandError: 'ucportal' conflicts with the name of an existing Python
module and cannot be used as an app name. Please try another name.
After that I deleted that directory and ran startapp command with same name and it worked fine.
So 'startapp' command is to create an app automatically. If you already have one there, it fails.
Answer given by #DAG worked for me.
I ran into this issue while trying to set up a Wagtail project.
Before creating the app, I had created and activated a virtualenv (using virtualenvwrapper) with the same name: $APPNAME. When I then ran wagtail start $APPNAME, Django looks for naming conflics in the $PYTHONPATH which in this instance points to /Users/User/.virtualenvs.
Naturally, this results in a conflict as /Users/User/.virtualenvs/$APPNAME already exists.
None of these answers helped me. In the end I ended up creating an app with a different name and then just renaming the directory to the app name I wanted all along. Note that you also will need to change the class name in apps.py to match your app name.
Just Simply Use This command
for Django Project Creation
python -m django startproject name_of_django_Project
for Django App Creation
python -m django startapp App_name
I had the same issue when working with wagtail cms. I got this error even there is no such a created app. This occurs when there is an app already that has the same name you need to create inside the site-packages directory.
Once you get this error, you need to check the following directory,
C:\Users\{user}\AppData\Local\Programs\Python\Python38-32\Lib\site-packages
If there is a package with the name same you want to create then you need to remove that package. Also make sure to check that package is important or not before deleting.
The application directory should be created first.
Example: apps/practice
The command appears to be duplicated, but it is correct.
Example: python manage.py startapp practice ./apps1/practice

Django App not working: "Error: No module named app_name"

I'm new to Django, and I can't figure out why my app isn't loading. I've created all the files: models.py, views.py, urs.py with default code in. But when I run python manage.py runserver, it says the following:
Error: No module named app_name
What am I doing wrong?
Did you remember to include __init__.py in your folder?
If you did, make sure the permissions on the file are also correct. There's also a similar question here.
Just an additional hint: Instead of manually creating the files you can use django-admin.py startapp APPNAME to automatically create a directory with all necessary files for a new app.
I got your point the init file thing also dosen't work out for me as well. Just ensure that are you writing the proper command inside the proper directory while creating the app, using the terminal.
Like while writing the command "django-admin startapp APP_NAME", ensure that the command is written inside your root configuration directory (which gets created after you typein command "django-admin startproject PROJECT-NAME"), not anywhere else. Then mention the app name inside the settings.py file, under the INSTALLED_APPS[ ] list. Then finally run the command "python manage.py runserver" in the same root configuration directory. I assure you, this will work for sure and the "No modules found" thing will disappear. Have a try and tell me if it don't. Thank you.

Imports failing for pylint

I'm testing my project using pylint and currently getting fatal error when importing the internal apps into the project using.
According to pylint, the import should be something like from <appname>.models import ...
as opposed to what I currently have: from <projectname>.<appname>.models import My problem is that when I use the recommended style, the project can't find/import the app. What am I missing here?
Your apps are not in the python path.
Seems like you have a folder for apps, like apps/registration, apps/contact_form etc. and your manage.py is in the folder on top of that one, which has just the project folder.
manage.py does some "magic" by putting all the apps into the python path before starting the server.
If you have custom folder structure, you should edit the manage.py to include your custom apps folder in the python path, preferably as the first element of the python path.

Unable to start a django server in my computer

I exported the path of my django project by
$ export DJANGO_SETTINGS_MODULE=/Users/masi/Documents/Test/djangobook/ch3.settings
I run unsuccessfully
$ django-admin.py runserver
Error: Could not import settings '/Users/masi/Documents/Test/djangobook/ch3.settings' (Is it on sys.path? Does it have syntax errors?): Import by filename is not supported.
How can you start Django server without the error message?
Your $DJANGO_SETTINGS_MODULE should just be set to ch3.settings. Just make sure that the ch3 app is in your $PYTHONPATH, too.
For example, if your app is at /Users/masi/Documents/Test/djangobook/, then set $DJANGO_SETTINGS_MODULE to ch3.settings, and make sure your $PYTHONPATH includes /Users/masi/Documents/Test/djangobook.
$ export PYTHONPATH=/Users/masi/Documents/Test/djangobook/
$ export DJANGO_SETTINGS_MODULE=ch3.settings
From the django docs on django-admin.py and manage.py:
django-admin.py is Django’s command-line utility for administrative tasks.
In addition, manage.py is automatically created in each Django project. manage.py is a thin wrapper around django-admin.py that takes care of two things for you before delegating to django-admin.py:
It puts your project’s package on sys.path.
It sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file.
Generally, when working on a single Django project, it’s easier to use manage.py
So, if your directory structure looks like:
djangobook/
ch3/
settings.py
Do the following and you can ignore all DJANGO environment variables (unless you have some really weird install):
$ cd /Users/masi/Documents/Test/djangobook/ch3/
$ python manage.py runserver
For those that come across the same error, when trying to run something similar:
python manage.py runserver --settings=settings_dev
When the settings file is within an app directory, like so:
mysite/
settings.py
settings_dev.py
requirements.txt
manage.py
You don't have to specify $PYTHONPATH (at least not four years on) you just need to make sure your --settings value contains the folder name — you also need to use dot notation, slashes will not do.
python manage.py runserver --settings=mysite.settings_dev
It is the same story when exporting a $DJANGO_SETTINGS_MODULE value:
export DJANGO_SETTINGS_MODULE=mysite.settings_dev
Might save someone else the time that I lost working that out.
You can also try manage.py.
From your project directory, run
$ python manage.py runserver
Even though it's just a wrapper, manage.py always works for me while django-admin.py doesn't. Obviously we're both doing something wrong (I just got started with Django), but this should get you going at least.