How to Solve Django Error Message : ImportError: No module named module_name

Posted on

As shown in the title of this article, it is actually displaying a clear message about what is the content of the article is written about. The article is about how to solve the error message happened upon executing a Django script file which is based on Python. The script itself is a script named urls.py. The error message is shown as follows :

ImportError: No module named module_name

It is in the line script defined in a file named urls.py. The content is actually shown in the following snippet code :

from module_name import views

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.home, name='home'),
]

It is actually importing views.py file from a module named module_name. The module_name is actually represented by a folder. It is actually triggering an error message since the module_name doesn’t really exist. The folder’s structure is shown as follows using the tree command  :

user@hostname:/home/user/myproject/myapp# tree -L 2 . 
.
├── bin
│   ├── activate
│   ├── activate.csh
│   ├── activate.fish
│   ├── activate_this.py
│   ├── easy_install
│   ├── easy_install-2.7
│   ├── pip
│   ├── pip2
│   ├── pip2.7
│   ├── python
│   ├── python2 -> python
│   ├── python2.7 -> python
│   ├── python-config
│   └── wheel
├── myapp.sublime-project
├── myapp.sublime-workspace
├── include
│   └── python2.7 -> /usr/include/python2.7
├── lib
│   └── python2.7
├── local
│   ├── bin -> /home/user/myproject/myapp/bin
│   ├── include -> /home/user/apps/myapp/include
│   └── lib -> /home/user/myproject/myapp/lib
├── src
│   ├── db.sqlite3
│   ├── myapp
│   ├── manage.py
│   ├── mycontact
│   └── myprofile
└── static

14 directories, 18 files
user@hostname:/home/user/myproject/myapp# 

Inside the src folder, there isn’t any folder named module_name. For an additional information, the src folder is not ‘src’ originally. But it is actually a folder named ‘myproject’. It is changed to make it easy to point out that every application or module created after will be located in that folder as a source code folder. In order to be able to remember it easily, just change the folder name into ‘src’.  Back to the discussion on solving the problem,  in order to do it, the actual solution is for this error message generated can be listed as follows :

1. Creating a folder of application named myapp located inside the apps folder where it is actually has been defined as the virtual environment for python based application. To know further about virtual environment, just read an article titled ‘How to Create an Isolated Python Environment’ in this link. So, in the above context, there is a folder named ‘myapp’ which is actually a folder created from a virtual environment.

2. Just change the line of code for importing the module_name used the folder or the one actually created inside the folder named src. As shown in the above output command, it can be ‘myapp’, ‘mycontact’ and also ‘myprofile’. Those folder is created through the execution of a command for creating a new application or module which can be seen in the article titled ‘How to Create a New Application using Django Web-based Framework in Ubuntu Linux via Command Line’ in this link.

One thought on “How to Solve Django Error Message : ImportError: No module named module_name

Leave a Reply