ModuleNotFoundError: No module named 'impassionuser' - python

I'm using django on vscode, and typed this on terminal:
(impassion) Subinui-MacBook-Pro:impassion_community subin$ python3 manage.py makemigrations
but can't use makemigrations
got this error message
ModuleNotFoundError: No module named 'impassionuser'
how can I solve this problem?
I'm using MacOSX, VSCode, django
and setup virtualenv
I expected to see follow messages
Migrations for 'impassionuser':
impassionuser/migrations/0001_initial.py
-Create model impassionuser
In settings.py, I already added 'impassionuser'
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'impassionuser',
'board'
]

Did you register your model in admin.py.
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
from .models import mymodel
admin.site.register(mymodel)
Hopefully this will help.
Also check this Django Tutorial

Related

Getting Relative Import Error While Registering Models Django

I was following the django documentation tutorial to make my first app, and then I reached the admin.py part and was trying to register my models, but then I got this error:
Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
My Folder Structure:
├───Poll
│ └───__pycache__
└───polls
├───migrations
│ └───__pycache__
└───__pycache__
Visual Representation:
My Code in admin.py:
from django.contrib import admin
from models import Question, Choice
admin.site.register(Question)
admin.site.register(Choice)
Settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'members.apps.MembersConfig',
]
I could see one problem i.e. import should be either from current location, using . or specify some app name.
Through current location:
from .models import Question, Choice
Through app name:
from some_app_name.models import Question, Choice
Figured it out after many frustrating hours. Basically in admin.py you register the model and instead of running the file, type py manage.py runserver.
Here's an example:
Admin.py:
from django.contrib import admin
from .models import Person
admin.site.register(Person)
Instead of running the file separately, do this:
CMD:
cd yourproject
py manage.py runserver
And It works.

Can't import rest_framework in Django despite it being installed in my venv

Following tutorials and I cannot import rest_framework.
I've activated my venv, run python and import rest_framework as suggested in many discussions on stack overflow, though no errors are thrown. I am fairly confident that djangorestframework is installed as it is in the environment directory:
venv dir shows rest_framework installed
I also restarted VS Code and my venv as suggested to no avail.
The error I receive in VS Code:
Import "rest_framework" could not be resolvedPylancereportMissingImports
Settings.py:
INSTALLED_APPS = [
'corsheaders',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'EmployeeApp.apps.EmployeeappConfig',
'rest_framework',
]
Immediately below INSTALLED_APPS:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
),
}
After setting up this project on my desktop it seems I have gotten it working. Though I selected my Python Interpreter path on my laptop, it seems that I may have done something wrong there on my laptop.
ctrl + shift + p
Python: Select Interpreter
select the path which includes the environment you are working with.

Issue importing modules with Django, but runs fine when I run it independently using '-m' flag

Currently I have a python project with the following directory structure.
I can run the demo.py file directly fine (without importing it in my Django project) from the outermost directory shown above using the following command:
python -m ObjectSegmentation.segmentation_api.apps.tools.demo
However, in my Django project, I need to import the demo.py file in my django views.py file, but when I do so, I get the following error:
from ObjectSegmentation.segmentation_api.apps.tools.test import *
ModuleNotFoundError: No module named 'ObjectSegmentation'
These are the first few lines from demo.py (including line 4) where the error occurs.
My installed apps in settings.py for the whole project look like this:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'apps.tools.demo',
'rest_framework'
]
How can I make it so that the imports work with both the project run independently and imported in my django project?

Django app not recognized when inside another module

I have a project with a structure like this:
my-django-project/
venv/
.gitignore
README.md
my_django_project/
db.sqlite3
manage.py
my_django_project/
settings.py
my_first_app/
__init__.py
inside_app/
__init__.py
apps.py
serializers.py
models.py
views.py
...
In settings.py i loaded the app like this:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'my_first_app.inside_app',
]
Running py manage.py runserver gives the following error:
File "C:\Users\\Documents\GitHub\tin_api_v2_test\venv\lib\site-packages\django\apps\config.py", line 246, in create
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Cannot import 'inside_app'. Check that 'my_first_app.inside_app.apps.InsideAppConfig.name' is correct.
However, it works fine when I don't use the Virtualenv. I've also tried putting the venv inside the django project folder to see if that would change anything but it didn't work.
Solved it..
In apps.py of inside_app i had to change from:
from django.apps import AppConfig
class InsideAppConfig(AppConfig):
name = 'inside_app'
to
from django.apps import AppConfig
class InsideAppConfig(AppConfig):
name = 'my_first_app.inside_app'

Django application database error: No module named

I was trying to connect my database application to project in django. Succeded on connecting psycopg and database. Then I started to build application for it by
python manage.py startapp books
and wrote some code in models.py.
But when I tried to put application in INSTALLED_APPS, it caused on error.
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'tut1.books',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
So, I'm in project's folder and type in Command Line:
%>python manage.py syncdb
ImportError: No module named 'tut1.books'
If I remove my app from INSTALLED_APPS, everything goes fine.
I suspect, there gotta be different syntax for linking application.
Tried to change 'tut1.books' to just 'books'. Causes another error.
%>python manage.py syncdb
AttributeError:'module' object has no attribute 'URLFIELD'
Files and folders in project /tut1/
manage.py
views.py
/tut1/
/templates/
/books/
Files in /books/
__init__.py
modules.py
views.py
tests.py
Files in /tut1/tut1/
__init__.py
settings.py
urls.py
views.py
wsgi.py
init.py is empty by default.
Checked PYTHONPATH for this project. Nothing odd...
>>> import sys
>>> for i in sys.path:
print(i)
C:\study\django\tut1
C:\Python33\Lib\idlelib
C:\Python33\lib\site-packages\setuptools-1.1.6-py3.3.egg
C:\Python33\python33.zip
C:\Python33\DLLs
C:\Python33\lib
C:\Python33
C:\Python33\lib\site-packages
It can be a problem with models.py.
You seem to write code as follows.
foo = models.URLFIELD()
"models" module has no URLFIELD().
You should change it to URLField().
You'll need to add an __init__.py file (it can be empty) to the folder tut1.books before python can import it. See this answer: What is __init__.py for?

Categories