Upload Django app to Heroku. ModuleNotFoundError:No Module named 'Name of Project" - django

I can not upload Django App to Heroku by command: git push heroku master. After uploading I have error: ModuleNotFoundError: No module named 'bot_diller'
Procfile:
web: gunicorn bot_diller.wsgi --log-file -
enter image description here

From the directory structure the root of your Git project is not entirely clear to me.
It seems you reference bot_diller as python module probably in an import statement in one of your Python files, which is outside of the scope of the code you push to Heroku.
The Django project seems to be inside bot_diller/new_project, where you have the top-level files like Procfile, requirements.txt and manage.py.
The Django app you created seems to be called main, which is also a Python module and beside that you have a module named new_project, which probably contains the Django settings.
So inside your Django project you can write import statements like
from main.models import MyModel
and the import error probably originates from a statement like
from bot_diller.new_project.main.models import MyModel

Related

While importing 'myapp.app' an import error was raised

I'm building a flask application, located in a subdirectory within my project called myapp. Running gunicorn --bind 0.0.0.0:$PORT myapp.app:app works fine, no errors, regardless of what FLASK_APP is set to. When I try to use Flask's CLI, however, it fails to find my app (reasonable), but when I set FLASK_APP to myapp.app., it appears to be doubling up the import path, resulting in an import error:
FLASK_APP=myapp.app flask run
* Serving Flask app 'myapp.app' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
Usage: flask run [OPTIONS]
Try 'flask run --help' for help.
Error: While importing 'myapp.myapp.app', an ImportError was raised.
How can I solve this? Is this a bug in Flask?
There was a __init__.py in my project directory that was causing this.
I dug into the source code of flask in cli.py and found the following code in prepare_import:
# move up until outside package structure (no __init__.py)
while True:
path, name = os.path.split(path)
module_name.append(name)
if not os.path.exists(os.path.join(path, "__init__.py")):
break
Because I had an __init__.py in my project directory (also called myapp), this made flask try to import myapp.app from the ancestor of my project, resulting in myapp.myapp.app.

How to add site models to Cookiecutter Django project

I have a Cookiecutter Django project in which I'd like to add a "SiteSettings" model to control constants. My first instinct was to run manage.py startapp site. However I received the following message:
CommandError: 'site' conflicts with the name of an existing Python module and cannot be used as an app name. Please try another name.
Upon closer inspection, it appears that Cookiecutter already created a sites module within contrib, but with no models.py file. So I decided to make a models.py with the SiteSettings model in this directory.
However when I run makemigrations it says: No changes detected in app 'sites'
How can I add my desired model to this module and get migrations working for this module?
You will run into countless problems if you follow this path. Just use another name and do manage.py startapp newname.

Running Gunicorn and Django

I don't know how to use gunicorn with django. Could you give me some help?
This is how I run the server with django. It is https
python3 manage.py runsslserver xx.8x.x3.x4:443 --certificate /etc/letsencrypt/live/callservicesvps.online/fullchain.pem --key /etc/letsencrypt/live/callserv.com/privkey.pem
And in the gunicorn documentation it is mentioned that it must be executed as follows:
gunicorn myproject.wsgi
And here I have 2 questions. What is myproject.wsgi? Where Can I find it? Because if I look in the directory where the django project is, the only thing I find with wsgi is a file called wsgi.py
Running the server as follows gives me an error
gunicorn /home/proyectdirectory/wsgi.py
It also gives me an error if I put:
gunicorn /home/proyectdirectory/wsgi:Some_directory_where_the_proyec_is
What is myproject.wsgi?
myproject.wsgi IS the wsgi.py file you have located inside your project.
[End of answer]
But a further explanation will clear up why this is..
I imagine most people will look atmyproject.wsgi and see a file with an extension file type .wsgi but this is just because of the way importing of modules is written in python.
I want to clarify what a module and a package before continuing an explanation.
What is a Module
From the jargon heavy python docs
A module is a file containing Python definitions and statements.
Put simply, a module in python is just a python file containing any sort of functions, variables, or classes etc.
What is a Package
A package is just a collection of modules. The most simplest example, a special directory containing python files. In order to tell python that a directory is a package it must have a file named __init__.py inside of it. You will find a few of these inside different directories inside your django project. This is why they are there.
Now, I can say what I want to say which is the structure of the module namespace in python.
package.subpackage.module
If you look inside your wsgi.py file you'll see a good example of importing from django's own wsgi module.
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '<project>.settings')
application = get_wsgi_application()
More specifically
from django.core.wsgi import get_wsgi_application
If you look inside the django package you can see how this works.
django (package) core (subpackage) wsgi (module)
Remember, the command gunicorn myproject.wsgi should be run inside the base directory of your django project. I always remember this as the directory containing the manage.py file. That is how gunicorn can find the wsgi.py file using the module namespace in this way. gunicorn is written in python afterall.
gunicorn /home/proyectdirectory/wsgi.py will error because python module imports don't contain / and even if you tried gunicorn home.proyectdirectory.wsgi home and proyectdirectory are not python packages.
Now hopefully this makes sense:
"If gunicorn myproject.wsgi is referring to the wsgi.py file why not just put gunicorn myproject.wsgi.py?"
You can't put a .py extension because this will refer to a module inside the wsgi subpackage with a filename py.py!

ImportError: No module named allauth

I have installed django-allauth for using social accounts in my django app but every time I try
manage.py syncdb
It gives me an error
ImportError: No module named allauth
I tried to add even
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
to
manage.py
but its still not working.I am unable to debug about how to add path to allauth to include it?
go to the root of the folder where you have your virtual environment and type:
source bin/activate

Django can't load app

I'm using django 1.4, after i created the project using django-admin, I create an app, add it to settings.py in INSTALLED_APPS but then when I do manage.py runserver I get Error: no module named myapp
Does anyone know why?
Make sure you have two things:
correct path. You should be able to do ./manage.py shell and then import 'myapp' if not try 'myproject.myapp'
models.py in your app folder and __init__.py