Django cannot find module - python

EDIT
So this was a Docker issue, not a Python issue. Thank you everyone for your help!
I am working on a Django project. I have several apps within the project and everything has been working fine. I recently added a new app called files through django-admin and for some reason, Django cannot find this module. This is my project structure
core/
__init__.py
settings.py
urls.py
wsgi.py
api/
migrations/
__init__.py
admin.py
apps.py
models.py
urls.py
views.py
files/
__init__.py
apps.py
forms.py
init_s3.py
urls.py
views.py
The apps.py for the files module reads:
from django.apps import AppConfig
class FilesConfig(AppConfig):
name = 'files'
Which is almost word-for-word how the AppConfig for the api module is written:
from django.apps import AppConfig
class ApiConfig(AppConfig):
name = 'api'
I have the api and files app installed in the INSTALLED_APPS setting:
INSTALLED_APPS = [
...other apps...
'api.apps.ApiConfig',
'files.apps.FilesConfig'
]
Django has no problems locating the api app, but cannot find the files app. When I run the server, I get the following error:
ModuleNotFoundError: No module named 'files'
What's going on here? What am I missing?
Any help is appreciated! Thanks!

Try putting the app name to INSTALLED_APPS:
INSTALLED_APPS = [
...other apps...
'files'
]

Have you checked your project's urls.py file? it should read:
urlpatterns = [
....
path('files/', include('files.urls')),
]

Related

No module named myapp

I have this project directory structure:
.
.pylintrc
|--myproj .
|--myapp
|--myproj (settings.py is here)
__init__.py
manage.py
.
In settings.py, INSTALLED_APPS I have the first entry 'myapp'.
From the root folder (containing .pylintrc), I call
$ DJANGO_SETTINGS_MODULE=myproj.myproj.settings pylint myproj --load-plugins pylint_django
However, I get error no module named 'myapp'. If I change the INSTALLED_APPS entry to 'myproj.myapp', then it is able to continue, but now I'm unable to start the project normally with manage.py runserver.
pastebin myproj.settings
What am I doing wrong, and what can I do to proceed?
Likely your settings.py is improperly configured, you need to do something like this:
settings.py
INSTALLED_APPS = [
...
'myapp.apps.MyappConfig',
...
]
If you open the apps.py file in your myapp directory you'll see the config class that should be included in INSTALLED_APPS
The same problem happened to me and I find my solution.
In My Case, I missed my urls.py file.
re_path(r"^chaining/", include("smart_selects.urls")),
to
re_path(r"^chaining/", include("packages.smart_selects.urls")),

Issue importing model class into a populating script

I am quite new to Django so bear with me, I have read a few Stack Overflow answers on similar but they seem to be much more complicated than what I am trying to do. Any help would be appreciated.
I have followed a tutorial on Udemy which built a website with some models, and then populated them with data using a python script.
From this course; https://www.udemy.com/course/python-and-django-full-stack-web-developer-bootcamp
I am trying to do the same and have followed the steps as analogously as I can. I have;
declared the models in app_name/models.py
imported them into app_name/admin.py and registered them
included app_name in INSTALLED_APPS in settings.py
Currently when I log onto the admin of my server I can see the models and add to them. What I cannot seem to do is import the models into my populate.py script which is located in the top level folder.
The script is as follows;
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_name.settings')
import django
django.setup()
import pandas as pd
import glob
from app_name.models import Model_Name
I am getting the following error;
RuntimeError: Model class app_name.models.Topic doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
I am not sure whether I have posted all of the relevant information, so do let me know if I can add anything.
File structure is a follows;
project_name
__pycache__
__init__.py
asgi.py
settings.py
urls.py
wsgi.py
app_name
__init__.py
admin.py
apps.py
models.py
tests.py
views.py
db.sqlite3
manage.py
populate.py
The contents of INSTALLED_APPS is the following
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app_name'
]
Did you try to populate with ORM I mean django shell? In your case I would create custom command instead of populate.py file. It would be pretty easier for you to use.

Django project cannot find application module

I am following the Tango with Django book's tutorial.
I tried to do:
from rango import views
in tango_with_django_project/urls.py where rango is the application. The IDE I use is pycharm and it cannot find or doesn't recognize rango.
The folder hierarchy is as follow:
rango
__init__.py
...
views.py
tango_with_django_project
__init__.py
...
urls.py
I already added 'rango' in settings.py
INSTALLED_APPS = [
.......
'rango',
]
Any help or comment is greatly appreciated. Thank you.
You said
pycharm ... cannot find or doesn't recognize rango
but you don't say if you have actually tried running the application. It could be just that pycharm doesn't know where to look for the code.
Try setting the top level folder of your project (the one you have the tango_with_django_project folder in) and mark it as a "Sources Root" by right-clicking on the folder and selecting the option from the "Mark Directory As" menu.
If that doesn't work, run your django app and add any error messages to your question.
include rango in INSTALLED_APPS within settings.py file as mentioned in below snippet
INSTALLED_APPS = [
.......
'rango',
]
The problem has to do with the level of directory I opened in PyCharm.
When I had the problem, I opened it from the topmost directory - which was why PyCharm couldn't find it.
rangoFolder
tango_with_django_project
rango
__init__.py
...
views.py
tango_with_django_project
__init__.py
...
urls.py
When I opened the project from
tango_with_django_project
rango
__init__.py
...
views.py
tango_with_django_project
__init__.py
...
urls.py
It found the module rango.
Go to Preferences > Project Interpreter and set the project interpreter and path mapping. Also set the Project Structure accordingly.

Inconsistent __name__ when importing packages in Python (Django)

I have a Django project with the following structure
project/
package1/
__init__.py
api.py
views.py
package2/
__init__.py
api.py
views.py
__init__.py
urls.py
wsgi.py
In package1/api.py I perform the following import:
from .views import my_func
When I do this, the __name__ value in my package1/views.py module is project.package1.views, this is exactly what I want.
However when I do the same thing in package2/api.py:
from .views import my_func2
I get package2.views for __name__, it's missing the project prefix.
I can fix this by using the full from project.package2.views path, but I don't understand why it would work for one package but not another.
I don't have anything at all in my __init__ files that would alter how the imports work, either.

Django Custom Template Tags In Google App Engine

I am trying to include the following Tag In Google App Engine Web Application:
http://www.djangosnippets.org/snippets/1357/
Is there any configuration of this file to make it work with Google App Engine?
Cause I followed the Django Template tutorials: http://docs.djangoproject.com/en/dev/howto/custom-template-tags/
and have this structure:
templatetags/
__init__.py
range_template.py
in the Template file, I have {%load range_template%}
But I am getting the error:
TemplateSyntaxError: 'range_template' is not a valid tag library: Could not load template library from django.templatetags.range_template, No module named range_template
The other thing that might be a problem why this ain't working is, the INSTALL_APPS settings.py file. Not sure how to configure it.
I have a settings.py file in the root of my application and included this:
INSTALLED_APPS = ('templatetags')
Any advice would be greatly appreciated.
try doing the following:
$ python ./manage.py startapp foo
Add foo to installed apps:
INSTALLED_APPS += ('foo',)
And move your templatetags directory into your foo app. Something like:
./djangoproject
__init__.py
settings.py
urls.py
etc..
foo/
__init__.py
templatetags/
__init__.py
range_template.py
Django convention is that template tag code resides in apps, in directories named templatetags (see docs). I assume the same would be true for GAE.
In case someone searches for this, I wrote a small article in 2008 about this: http://daily.profeth.de/2008/04/using-custom-django-template-helpers.html
Please make sure to restart the development server after following the above step

Categories