When I am deploying my Django application on Heroku, the application crash and Heroku's logs notify me about this error :
2020-12-07T12:55:55.982458+00:00 app[web.1]: ModuleNotFoundError: No module named 'WebSite'
The thing is, WebSite is not a Python module, but a folder in my Django application (Achievement Comparer being my main application name).
AchievementComparer/
AchievementComparer/ ← My Django project
static/
staticfiles/
WebSite/ ← A Django app
__init__.py
manage.py
wsgi.py
Procfile
requirements.txt
runtime.txt
Of course, if I start my application locally, everything works just fine (running py manage.py runserver on Windows, as gunicorn is only available on unix).
The current issue is that you have an extra directory layer:
AchievementComparer/ ← This shouldn't be here
AchievementComparer/
WebSite/
manage.py
…
Procfile
requirements.txt
runtime.txt
Move everything that's currently in the top-level AchievementComparer/ directory up a level and delete the now-empty old AchievementComparer/ directory:
AchievementComparer/
asgi.py
settings.py
wsgi.py
WebSite/
manage.py
Procfile
requirements.txt
runtime.txt
A few other issues that are likely to bite you next:
The only wsgi.py file you should need is the one that's currently in your AchievementComparer/ project directory, beside your settings.py. Delete the other one.
Your Procfile is hard-coding a port. You'll need to bind to whatever port Heroku assigns you via the PORT environment variable.
You don't need --pythonpath in your Procfile, but you should update the module to use the full module name AchievementComparer.wsgi.
All in all, try changing your Procfile to something like this:
web: gunicorn AchievementComparer.wsgi:application --bind=0.0.0.0:$PORT --log-file -
Related
I have following project structure
In Procfile i difine web as web: gunicorn george_paintings.wsgi
When i deploy project on Heroku i see that Heroku identified my Procfile
Starting process with command `gunicorn george_paintings.wsgi`
But when i got an error ModuleNotFoundError: No module named 'george_paintings'
`
How to properly set up wsgi in Proclife in context of my project structure?
Your ModuleNotFoundError: No module named ... error is generally fixed by adding an empty __init__.py file to the folder. See python docs
The init.py files are required to make Python treat directories containing the file as packages
What I would suggest is to remove the application folder and move Procfile, README.md and requirements.txt to the outer george_paintings and use that as your root folder.
Then web: gunicorn george_paintings.wsgi should work as expected.
I have uploaded by git repo to Heroku, but I am having a hard time finding out the gunicorn command to put in the Procfile. This is my repo path to the wsgi.py
So what should I put in my Procfile.
web: gunicorn image_editor.wsgi --log-file -
is my current Procfile but there is a module not found error. Thanks
Edit: Error is this ModuleNotFoundError: No module named 'image_editor'
Do pip install gunicorn in your local environment and add it to requirements.txt
I had to change the project structure, Procfile should be with manage.py
I have my existing Django application running locally on my MacBook. It's directory structure looks something like this:
myproject/
mySite/
__init__.py
settings.py
urls.py
wsgi.py
myApp1/
__init__.py
models.py
views.py
manage.py
requirements.txt
Up until now, I have been using the Django toy webserver to run my app: ./manage.py runserver 0.0.0.0:8000. But now I want to use gunicorn instead. So I'm following the instructions here.
I do source myVirtualenv/bin/activate && cd myproject && gunicorn mySite.wsgi. I get the following error:
File "/usr/local/Cellar/python/2.7.12_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "myproject/MyApp2/models.py", line 11, in <module>
from caching.base import CachingManager, CachingMixin
ImportError: No module named caching.base
When I run ./manage.py runserver 0.0.0.0:8000 from the same location it works perfectly fine.
Why? Am I doing something wrong?
Does Django-Cache-Machine not work with Gunicorn/WSGI? How to work around this issue?
To run your project using gunicorn, try the following:
activate your virtualenv
go to your project's directory
run gunicorn mySite.wsgi:application --bind 127.0.0.1:8000
If the commands work fine, than my you are setup. Otherwise, try the following tutorial. I always use this tutorial myself, when setting up a new project for production. Try it. Setting up Django with Nginx, Gunicorn and Supervisor
You seem to have installed gunicorn globally rather than within the virtualenv, so the executable is pointing to the global Python and its site-packages directory rather than the one within the virtualenv. Reinstall gunicorn locally.
I have my project based on the django startproject: https://github.com/lincolnloop/django-startproject/
Here is my procfile:
web: gunicorn <myproject>.wsgi:application
Right now I have my set up like this:
[myproject]
---[conf]
------[local]
---------settings.py
------[qa]
---------settings.py
---[server_configs]
------[local]
---------django.wsgi
------[qa]
---------django.wsgi
My django.wsgi looks like this:
import os
import sys
import django.core.handlers.wsgi
sys.path.append('/app')
sys.path.append('/app/myproject')
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.conf.qa.settings'
application = django.core.handlers.wsgi.WSGIHandler()
I want to use the qa/django.wsgi for Heroku, but I am getting an error saying:
ImportError: No module named [myproject].wsgi
I have already gone through and tried solutions in this post: Configuring gunicorn for Django on Heroku but with no luck on my end.
Right now my PYTHONPATH is /app and have already tried Gunicorn+Django+Heroku. Python Path Issue with no luck either.
I had a similar problem once. Basically, you're running gunicorn from your root directory, but your wsgi is in [my_project]/[server_configs]/[qa]/.
There are two ways to go about it. If each of those directories has an init.py file, you can call it like a normal python module. Use:
web: gunicorn [myproject].[server_configs].[qa].django.wsgi:application
If they don't have init.py files (I didn't), you'll need your Procfile to switch into the proper directory. Try:
web: sh -c 'cd [server_configs]/[qa] && gunicorn django.wsgi:application'
Basically this is running a command in your shell to (1) change directories and (2) run gunicorn.
Heroku allows you to use environment variables in your Procfile. If you define an environment variable on Heroku, like so: heroku config:set WSGI_PATH=path/to/my/file/wsgi.py, then in your Procfile do: gunicorn $WSGI_PATH:application you should be good to go!
Introduction:
I am following the Getting Started with Django on Heroku quick start guide.
I intend to apply the Two Scoops of Django book philosophy about working with virtualenvs and projects. Audrey Roy and Daniel Greenfeld (authors) like to put all the environments in one directory and all the projects in another.
I also intend to apply the Two Scoops of Django book philosophy about placing what is generated by the django-admin.py startproject management command inside another directory which serves as the git repository root (a.k.a. three-tiered approach).
The layout at the highest level:
repository_root/
django_project_root/
configuration_root/
Locally:
OS X 10.8.4
pip==1.4.1
virtualenv==1.10.1
virtualenvwrapper==4.1.1
wsgiref==0.1.2
.bashrc contains:
export WORKON_HOME=$HOME/Envs
export PROJECT_HOME=$HOME/Projects
~/Envs
~/Projects
Inside hellodjango_venv:
Django==1.5.2
dj-database-url==0.2.2
dj-static==0.0.5
django-toolbelt==0.0.1
gunicorn==18.0
psycopg2==2.5.1
static==0.4
From Terminal:
mac-pol:~ oubiga$ mkdir -p Projects/hellodjango_rep/hellodjango
mac-pol:~ oubiga$ cd Projects/hellodjango_rep/hellodjango
mac-pol:hellodjango oubiga$ mkvirtualenv hellodjango_venv
New python executable in hellodjango_venv/bin/python2.7
Also creating executable in hellodjango_venv/bin/python
Installing Setuptools..................................
...
..................................................done.
(hellodjango_venv)mac-pol:hellodjango oubiga$ pip install django-toolbelt
...
Successfully installed django-toolbelt django psycopg2 gunicorn dj-database-url dj-static static
Cleaning up...
(hellodjango_venv)mac-pol:hellodjango oubiga$ django-admin.py startproject hellodjango .
(hellodjango_venv)mac-pol:hellodjango oubiga$ touch Procfile && open Procfile
Edit Procfile:
web: gunicorn hellodjango.wsgi
Start the process:
(hellodjango_venv)mac-pol:hellodjango oubiga$ foreman start
01:30:37 web.1 | started with pid 638
01:30:37 web.1 | 2013-09-04 01:30:37 [638] [INFO] Starting gunicorn 18.0
01:30:37 web.1 | 2013-09-04 01:30:37 [638] [INFO] Listening at: http://0.0.0.0:5000 (638)
01:30:37 web.1 | 2013-09-04 01:30:37 [638] [INFO] Using worker: sync
01:30:37 web.1 | 2013-09-04 01:30:37 [641] [INFO] Booting worker with pid: 641
CTRL+C
So far, so good.
At the moment my Projects tree is:
~/Projects/
hellodjango_rep/
hellodjango/ cwd
manage.py
Procfile
hellodjango/
__init__.py
settings/
urls.py
wsgi.py
and my Virtualenvs tree is:
~/Envs/
get_env_details
initialize
postactivate
postdeactivate
postmkproject
postmkvirtualenv
postrmproject
postrmvirtualenv
preactivate
predeactivate
premkproject
premkvirtualenv
prermproject
prermvirtualenv
hellodjango_venv/
bin/
include/
lib/
But, do you remember the three-tiered approach? How can I achieve it?
I am quite sure hellodjango_rep/ later on will contain at least: .git, .gitignore and requirements.txt
Research:
On the #django IRC channel, Jacob Kaplan-Moss answered:
...the Procfile needs to be at the top-level of your git repo...
Neil Middleton, Engineer at Heroku, answered this question "Procfile declares types -> (none) in Heroku" in SO:
...your Procfile needs to be in the root of your git repository that
is pushed to Heroku...
From Heroku Dev Center:
...Process types are declared via a file named Procfile placed in the
root of your app...
As a First Attempt:
I move Procfile up one folder level.
(hellodjango_venv)mac-pol:hellodjango oubiga$ cd ..
At the moment my Projects tree is:
~/Projects/
hellodjango_rep/ cwd
Procfile
hellodjango/
manage.py
hellodjango/
__init__.py
settings/
urls.py
wsgi.py
I start the process again:
(hellodjango_venv)mac-pol:hellodjango_rep oubiga$ foreman start
I'll get ImportError: No module named hellodjango.wsgi
Debugging:
The ImportError seems to come from /Users/oubiga/Envs/hellodjango_venv/lib/python2.7/site-packages/gunicorn/util.py
def import_app(module):
parts = module.split(":", 1)
if len(parts) == 1:
module, obj = module, "application"
else:
module, obj = parts[0], parts[1]
try:
__import__(module) # <-- line 354
except ImportError:
if module.endswith(".py") and os.path.exists(module):
raise ImportError("Failed to find application, did "
"you mean '%s:%s'?" % (module.rsplit(".", 1)[0], obj))
else:
raise
...
No module named hellodjango.wsgi
As a Second Attempt:
Procfile comes back at the previous level. Procfile and manage.py belong together again.
(hellodjango_venv)mac-pol:hellodjango_rep oubiga$ foreman start
I'll get ERROR: Procfile does not exist. It's quite obvious why it happens.
Hypothesis:
From Django documentation:
...manage.py is automatically created in each Django project.
manage.py is a thin wrapper around django-admin.py that takes care of
two things for you before delegating to django-admin.py:
- It puts your project’s package on sys.path.
- It sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file...
I'm not sure if the problem is related to this.
So:
Is it possible to have a Procfile and a manage.py file in different folder level ?
What is wrong with this approach?
Thank you in advance.
Firstly I have to say, that I haven't done so wide research and have used Heroku about 3 times. But:
(Your Seccond Attempt):
Procfile really should be in the top level directory. If you move the Procfile deeper gunicorn can't find it.
(Your First Attempt):
And having your Procfile on the top level directory you should state path to wsgi. Which is not possible yet. So you shoud make __init__.py file in the first "hellodjango" directory, to mark it as "module". Then you shoud change the path in Procfile to: hellodjango.hellodjango.wsgi
Hope this helps.
I ran into this using a Two Scoops layout, and my solution was to keep the standard layout (Procfile in repository_root, manage.py in project_root) and to make Procfile a script that changes directory into project_root before running gunicorn.
e.g., in Procfile:
web: sh -c 'cd ./mysite/ && exec gunicorn mysite.wsgi --log-file -'
You could equally have Procfile call an external script:
web: bash scripts/heroku_run
I prefer this to adding additional module paths as it can mess up imports in settings.py and urls.py, etc.