Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing a project name in django

Tags:

python

django

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?

like image 951
Melissa Stewart Avatar asked Oct 27 '18 18:10

Melissa Stewart


People also ask

Can I rename a Django project?

Search for your current project name. In the replace box enter the new project folder name that you want. Now the code editor will automatically search in the whole project folder and will replace the current project name with the new name. Now just rename the main app name which is in your project folder.

How do I rename a Django project in PyCharm?

If the project name is the same as the name of its root folder, select Rename directory. PyCharm will perform the Rename refactoring so that all the references to the directory throughout the code remain valid. If the project name is different from the name of its root folder, select Rename project.

How do I change app label in Django admin?

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=''


1 Answers

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 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 who are using PyCharm's Django Support.

  1. Go to your Project and click the Edit Configuration Settings.

    Pycharm Project Configuration

  2. 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.

    enter image description here

  3. Then add your new project name newname, which contains the settings.py file.

    enter image description here

That's it.

like image 52
Kushan Gunasekera Avatar answered Sep 23 '22 12:09

Kushan Gunasekera