I have changed the name of an app in Django by renaming its folder, imports and all its references (templates/indexes). But now I get this error when I try to run python manage.py runserver
Error: Could not import settings 'nameofmynewapp.settings' (Is it on sys.path?): No module named settings
How can I debug and solve this error? Any clues?
Delete the database tables of the app to be renamed. Rename the folder of the app. Change any references to your app in their dependencies, i.e. the app's views.py , urls.py , 'manage.py' , and settings.py files. If your models.py 's Meta Class has app_name listed, make sure to rename that too (mentioned by @will).
Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged. So, 1 and 3 are both valid, but 3 would be the recommended approach.
Rename the folder which is in your project root. Change any references to your app in their dependencies, i.e. the app's views, the urls.py and settings.py files. Edit the database table django_content_type with the following command: UPDATE django_content_type SET app_label='' WHERE app_label=''
Follow these steps to change an app's name in Django:
views.py
, urls.py
, manage.py
, and settings.py
files.django_content_type
with the following command: UPDATE django_content_type SET app_label='<NewAppName>' WHERE app_label='<OldAppName>'
ALTER TABLE <oldAppName>_modelName RENAME TO <newAppName>_modelName
. For mysql too, I think it is the same (as mentioned by @null_radix).django_migrations
table to avoid having your previous migrations re-run: UPDATE django_migrations SET app='<NewAppName>' WHERE app='<OldAppName>'
. Note: there is some debate (in comments) if this step is required for Django 1.8+; If someone knows for sure please update here.models.py
's Meta Class has app_name
listed, make sure to rename that too (mentioned by @will).static
or templates
folders inside your app, you'll also need to rename those. For example, rename old_app/static/old_app
to new_app/static/new_app
.models
, you'll need to change django_content_type.name
entry in DB. For postgreSQL, use UPDATE django_content_type SET name='<newModelName>' where name='<oldModelName>' AND app_label='<OldAppName>'
__pycache__/
folder inside the app must be removed, otherwise you get EOFError: marshal data too short when trying to run the server
. Mentioned by @Serhii KushchenkoMeta point (If using virtualenv): Worth noting, if you are renaming the directory that contains your virtualenv, there will likely be several files in your env that contain an absolute path and will also need to be updated. If you are getting errors such as ImportError: No module named ...
this might be the culprit. (thanks to @danyamachine for providing this).
Other references: you might also want to refer to the below links for a more complete picture:
New in Django 1.7 is a app registry that stores configuration and provides introspection. This machinery let's you change several app attributes.
The main point I want to make is that renaming an app isn't always necessary: With app configuration it is possible to resolve conflicting apps. But also the way to go if your app needs friendly naming.
As an example I want to name my polls app 'Feedback from users'. It goes like this:
Create a apps.py
file in the polls
directory:
from django.apps import AppConfig class PollsConfig(AppConfig): name = 'polls' verbose_name = "Feedback from users"
Add the default app config to your polls/__init__.py
:
default_app_config = 'polls.apps.PollsConfig'
For more app configuration: https://docs.djangoproject.com/en/1.7/ref/applications/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With