Reputation: 3605
I changed the name of my django project from oldname
to newname
using Pycharm's refactor > rename. I have scoured through the project and it seems to have changed the name everywhere. But when I try runserver, here's what I get,
Traceback (most recent call last):
File "/Users/melissa/Dropbox/newname/manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/Users/melissa/Dropbox/newname/env/lib/python3.7/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/Users/melissa/Dropbox/newname/env/lib/python3.7/site-packages/django/core/management/__init__.py", line 317, in execute
settings.INSTALLED_APPS
File "/Users/melissa/Dropbox/newname/env/lib/python3.7/site-packages/django/conf/__init__.py", line 56, in __getattr__
self._setup(name)
File "/Users/melissa/Dropbox/newname/env/lib/python3.7/site-packages/django/conf/__init__.py", line 43, in _setup
self._wrapped = Settings(settings_module)
File "/Users/melissa/Dropbox/newname/env/lib/python3.7/site-packages/django/conf/__init__.py", line 106, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'oldname'
Can someone help me with this?
Upvotes: 21
Views: 25634
Reputation: 8566
I got the same error when changing my project name, the problem is that you're running the server using PyCharm, isn't it? Hopefully, you created your project using PyCharm with Django Support. First of all add the below settings to your wsgi.py
, asgi.py
, and manage.py
files,
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'oldname.settings')
to
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'newname.settings')
Then go to your settings.py
and change the below settings,
ROOT_URLCONF = 'oldname.urls
WSGI_APPLICATION = 'oldname.wsgi.application'
to
ROOT_URLCONF = 'newname.urls
WSGI_APPLICATION = 'newname.wsgi.application'
If you didn't get PyCharm's Django Support, this solution should be performed otherwise you will get the same error again. You can try this by using some other terminal without getting PyCharm's Django Support and running your server with python manage.py runserver
command. If your project doesn't have any other errors you can see this solution is working. Then I realized something wrong with PyCharm's Django Support, the help that I have provided is only for those who are using PyCharm's Django Support.
Go to your Project and click the Edit Configuration Settings.
You can see the Environment variable still has the previous values, just click the icon in the corner to edit the DJANGO_SETTINGS_MODULE
variable.
Then add your new project name newname
, which contains the settings.py
file.
That's it.
Upvotes: 20
Reputation: 798
The best way to achieve this consist of renaming all the occurences of the project name in the wsgi.py
, asgi.py
, settings.py
and manage.py
file.
I have created a simple Python script that handles that for any project. This allows for a quick and no mistake process.
Just place the rename.py
script inside the main folder of the Django project (same level as manage.py
file) and run the script.
Upvotes: 1
Reputation: 3385
I just had this issue when I renamed my project. Turned out that I'd renamed everything correctly. It was just that my environment had retained the old environment variable so I had to reset it.
So, for anybody finding this question that thinks they've renamed manage.py
, all their settings.py
, wsgi.py
and asgi.py
etc.:
run env | grep DJANGO
, and make sure DJANGO_SETTINGS_MODULE
is either not set at all, or set to the new value. Most likely this is your issue.
Upvotes: 1
Reputation: 1152
I have manually changed the project name. If that is fine then checkout my answer https://stackoverflow.com/a/63303738/9384511
Upvotes: 0
Reputation: 1008
Same issue happened in my project. I had to change my project_old_app_name to project_new_app_name. When i change the old to new by using visual studio code replace all method.
i got this error
...
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'project_new_app_name'
then i came to realised that i have to change the folder/directory app name of my project_old_app_name to project_new_app_name, then it work.
Note:- I got problem of not matching table name as it was named to old_app_table_name. So, In this case (i used sqlite db) i had to do python manage.py migrate
and to insert data again. Or We can change all the old_app_name_tablenames to new_app_name_tablenames in sqlite browser.
Upvotes: 0
Reputation: 1026
I am using VSCode an in that I simply made a search of the name I want to change on the project level and replaced it with another name(new name I wanted)!.
It worked without a hiccup.
Upvotes: 0
Reputation: 4592
Instead of going through all the steps to change a project, just create a new one. This worked for me using PyCharm on MacOS on a project with no database configured.
Beside being easier to do, the method doesn't touch your existing project. If it doesn't work, just delete it.
Upvotes: 1
Reputation: 372
You can just run this command. for example if I want change old_project_name to new_project_name In the path that I am
python manage.py rename old_project new new_project
Upvotes: 0
Reputation: 523
I have a solution with Django 2.2 working perfectly. You have renamed the old project to example django_master
. Then you need to change in the list of file:
wsgi.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_master.settings')
application = get_wsgi_application()
## django_master.settings
settings.py
#line 54
ROOT_URLCONF = 'django_master.urls'
#line 72 django 2.2 default code
WSGI_APPLICATION = 'django_master.wsgi.application'
After this three change your project now run python mange.py runserver
If still not working find with old project name in project directory:
Hope it helps?
Upvotes: 1
Reputation: 896
You need to also change the pycharm settings .iml and .xml references to ensure your project can read & execute manage.py tasks.
Upvotes: 0
Reputation: 444
Look inside "Run/Debug Configurations, "Environment", and under "Environment Variables". oldname might be found in one of those.
Upvotes: 1
Reputation: 142
make sure you change the name in settings.py , wsgi.py and manage.py
Upvotes: 10
Reputation: 1400
Theres one main place where a Django app gets its app name and its in
myappname/apps.py
from django.apps import AppConfig
class StorefrontConfig(AppConfig):
name = 'myappname'
Then in settings.py
you can find
INSTALLED_APPS = [
'myappname',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
Once you change the name of your app you want to change it in settings.py
and in apps.py
Upvotes: 3