Django: ModuleNotFoundError: no module named 'Parser' [duplicate] - python

This question already has answers here:
Django importing another file from another package
(2 answers)
Closed 2 years ago.
In my project, I have an app called pages:
apps/pages/experiments
Within experiments, the relevant files are:
models.py
a folder called parser (which includes an empty file init.py (with 4 underscores)
The folder called parser has python code that I would like to import in models, and use there. I have a line of code:
from parser import Tables
However, when I run the command
python manage.py runserver
I get the following error:
from parser import Tables
ModuleNotFoundError: No module named 'XML_parser'
edit: I just realized that If I remove imports of my own code, and simply try to import models.py within views.py, then I also get the same error, but now "models.py" instead of parser

as it turns out, i have to start importing from the base of my django project:
Django importing another file from another package

Try to run makemigrations and migrate after defining your app name in settings.INSTALLED_APPS
INSTALLED_APPS = [
# ...
'Abc.apps.AbcConfig' // Replace your app's name with 'Abc'
python manage.py makemigrations
python manage.py migrate

Related

ModuleNotFoundError : no module found named <module_name>django

I am trying to make a web application using django. so in the website folder, I made another app named U0.
I made sure to add it in the settings section of the website. but when I try to migrate the manage.py file it throws an error saying, No Module found named U0django
In order to create a new app in Django, you have to use the Django builtin command to create the correct structure of an app for you like this:
django-admin startapp u0
btw I believe if you want to create it on your own you have to create a file named apps.py in your app folder and add these lines to that to help Django identify it as an app :
from django.apps import AppConfig
class U0(AppConfig):
name = 'u0'

ModuleNotFoundError: No module named 'apps.news'; 'apps' is not a package

I have a Django project with the following structure:
project
apps
news
__init__.py
models.py
hose
tasks.py
Within tasks.py, I have this import
from apps.news.models import Company
When I try to run tasks.py, I get the following error:
ModuleNotFoundError: No module named 'apps.news'; 'apps' is not a package
How do I fix this error? I don't understand why this import does not work.
Reference: https://docs.python.org/2/tutorial/modules.html#packages
You need to add an empty __init__.py (4 underscores in total) file in the apps folder for it to be recognized by Python as a package.
In case anybody else is still struggling with this. I had the __init__.py in the app folder but it still didn't work. Running the following command in app parent folder worked for me.
export PYTHONPATH=$PWD

Django - app in subfolder

I've created new django app in a subdirectory using the command:
python manage.py startapp appName subFolder/appName
but if I try to add this app to INSTALLED_APPS at the end of the list I see the following error:
ImportError: No module named appName
Does anyone know what I am doing wrong?
You need to include the subfolder when you add the app to INSTALLED_APPS, for example:
'subFolder.appName',
or
'subfolder.appName.apps.AppNameConfig',
I tried different options but no one can solve that issue.
Finally, I found a solution.
Simply go to the subFolder/appName/app.py file and
replace this line name = 'appname' with name = 'subfolder.appname'
And then you can simply add to the installed apps list.
'subfolder.appname'

from gcm.models import get_device_model ImportError: No module named models

I got a sample Django Rest project. When I run:
python manage.py makemigrations
I get the error:
from gcm.models import get_device_model
ImportError: No module named models
in line
from gcm.models import get_device_model
What is the problem with this line? please help. I am a newbie in python as well as to Django
It means that the gcm directory doesn't have a models.py defined.
Somehow you have an issue with your paths.

Trouble running example django code

Im trying to learn Django by looking at examples, but Im having a bit of a problem running the examples I find.
I downloaded 'cheeserator' from https://github.com/jacobian/cheeserater
and I tried running it with python manage.py runserver
but I get the following error -
Error: Can't find the file
'settings.py' in the directory
containing 'manage.py'. It appears
you've customized things. You'll have
to run django-admin.py, passing it
your settings module. (If the file
settings.py does indeed exist, it's
causing an ImportError somehow.)
What am I doing wrong?
You need to have a settings.py file.
As per the instructions in the link provided:
Then you'll want to create a settings.py file in this directory containing::
from settings_template import *
# Override any settings you like here.
Or if you don't want to override anything rename settings_template.py to settings.py

Categories