Deploying to Heroku: ImportError:Couldn't import Django - python

When I want to deploy my code to Heroku using
git push heroku master
I have ImportError:Couldn't import Django.
So I try python manage.py runserver to see what is going on and it shows this in the terminal.
Full description of ImportError:Couldn't import Django.
Before I tab all the process for deploying my code to Heroku,python manage.py runserver works well (the system gives me a link to open my Web page).
Here is my requirements.txt
requirements.txt

Usually, when you deploy a python app to Heroku, you must have a requirements.txt file. I had this same error and when I added all of my dependencies to a requirements.txt file, it all worked. Best way to do this is if you are running on a virtual environment is
pip freeze > requirements.txt

Related

Django app while dockerizing gives "Starting development server at http://0.0.0.0:8000/" but doesn't show up on browser

So I am a beginner in docker and Django. What I have here is a django app which I am trying to dockerize and run. My requirements.txt has only django and gunicorn as the packages.
I am getting the below in terminal after building and running the docker image:
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
August 26, 2021 - 06:57:22
Django version 3.2.6, using settings 'myproject.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
Below is my Dockerfile:
FROM python:3.6-slim
ENV PYTHONUNBUFFERED=1
RUN mkdir /Django
WORKDIR /Django
ADD . /Django
RUN pip install -r requirements.txt
EXPOSE 8000
CMD python manage.py runserver 0.0.0.0:8000
The commands I am using are:
docker build . -t myproj
docker run -p 8000:8000 myproj
I have tried adding allowedhosts = ['127.0.0.1'] in settings.py but still I am getting "The site can't be reached. 127.0.0.1 refused to connect.
Not able to see the "Congratulations" screen.
Please help me out with this.
P.s: I am using windows machine
Updates
I tried running the below line and got the following output:
docker exec 8e6c4e4a58db curl 127.0.0.1:8000
OCI runtime exec failed: exec failed: container_linux.go:349: starting container process caused "exec: \"curl\": executable file not found in $PATH": unknown
Without your settings.py, this can be hard to figure out. You say you have allowedhosts = ['127.0.0.1'] in there and that should definitely not be necessary. It might actually be what's blocking your host, since requests from your host come from a different IP address.
I've made the following Dockerfile that creates a starter project and runs it
FROM python:latest
RUN python -m pip install Django
RUN django-admin startproject mysite
WORKDIR /mysite
EXPOSE 8000
CMD python manage.py runserver 0.0.0.0:8000
If I build it and run it with
docker build -t mysite .
docker run -d -p 8000:8000 mysite
I can connect to http://localhost:8000/ on my machine and get the default page (I'm on Windows too).
I hope that helps you to locate your issue.
PS: Your curl command fails because curl isn't installed in your image, so it failing has nothing to do with your issue.

Trying to run a python file in flask

I am trying to learn flask. I created a venv and installed flask. I set the flaskblog.py as FLASK_APP. But it says serving flask app 'app.py' and it fails. why is this?
I'm not sure if the image is visible. So, I'll write what I've tried below,
In the windows powershell, I cd to the directory where my venv and python script is, then
$ . vnev\Scripts\activate
$ pip install flask
$ set FLASK_APP=flaskblog.py
$ flask run
it gives me the error saying,
Serving Flask app 'app.py' (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: Could not import 'app'.
What is wrong here? I have also tried $ setx FLASK_APP "flaskblog.py and still get the same result. I think i created this app.py using pycharm's virtual environment and now, I cannot set other files to FLASK_APP.
What should I do?

Heroku not reading requirement.txt file

I am trying to deploy my python flask app to heroku but it keeps crashing and complaining about bash: gunicorn: command not found. I have my requirement.txt file in the root folder where my Procfile is also located. My python code is located in src/server/
Procfile contains: web: gunicorn --pythonpath src/server/ route:app --preload
I have gunicorn in my requirements file:
Is there something i'm missing?
gunicorn==19.8.1
Flask==0.12.2
Flask-Cache==0.13.1
Flask-Cors==3.0.2
Flask-MongoAlchemy==0.5.1
flask-mongoengine==0.9.5
Flask-PyMongo==0.5.2
Flask-WTF==0.14.2
gevent==1.2.1
greenlet==0.4.12
pymongo==3.6.1
folder structure;
I faced a similar problem with my app. I just reset all my requirements by sending in an empty requirements.txt file. Then built my app. Then sent the original file. I don't understand why, but it worked.
I am also having a similar problem like this on Heroku. I created an IDENTICAL new project and magically it works. Heroku has some bug related to ignoring requirements.txt
I fixed it by creating a new environment in Heroku. The previous environment seems not to be reading the requirement.txt file, I couldn't figure why.

Couldn't find that process type, Heroku

I'm trying to deploy a simple python bot on Heroku but I get the error
couldn't find that process type
When I try to scale the dynos. I already made a procfile and it looks like this:
web: gunicorn dep:app, where "dep" is the name of my python code
What could be the reason?
This may happen if your procfile is misspelt, such as "procfile" or "ProcFile" etc. The file name should be "Procfile" (with a capital P).
sometimes changing the file name is not anough, because git wouldn't spot the change. I had to delete the Procfile completely, then commit the change, than add it again with the right name, and then commit that again:
remove your procfile
git commit
add a new procfile with the exact name "Procfile"
commit again
git push heroku master (or main - new heroku projects now uses main)
should work!
Make Sure Procfile should not have any extension like .txt
otherwise this will be the error
remote: -----> Discovering process types
remote: Procfile declares types -> (none)
To create file without extension type following in cmd
notepad Procfile.
Now add web: gunicorn dep:app and save
Now when you will git push heroku master the above lines will be like
remote: -----> Discovering process types
remote: Procfile declares types -> web
And the error is gone when you will run
C:\Users\Super-Singh\PycharmProjects\URLShortener>heroku ps:scale web=1
Scaling dynos... done, now running web at 1:Free
Ensure that the Procfile is in the root directory of your repository.
In my case I had initially kept the Procfile in a subdirectory. Moving it to the root directory solved the problem.
For people who are trying to deploy a django web app, take note that the above step may cause another issue - of heroku unable to reach till the wsgi file residing in the subdirectory.
I solved it by referring to the below thread -
How can I modify Procfile to run Gunicorn process in a non-standard folder on Heroku?
The following worked for me.
As per this Heroku help page:
To fix:
Remove the existing buildpacks with heroku buildpacks:clear.
You will need to add an empty commit and redeploy for the changes to
take effect:
git commit --allow-empty -m "Adjust buildpacks on Heroku"
git push heroku master
You might check your python version. I tried to deploy my Django project so my procfile looks like this web: gunicorn blog.wsgi --log-file - and I also got the same error couldn't find that process type. and I found that Heroku only support python-3.6.4 and python-2.7.14 while I just had python3.5. You can type:
python -V
to see what python version you are using now. if not, you can download python 3.6. I followed this How do I install Python 3.6 using apt-get?
Ubuntu 14.04 and 16.04
If you are using Ubuntu 14.04 or 16.04, you can use Felix Krull's
deadsnakes PPA at
https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa:
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.6
Alternatively, you can use J Fernyhough's PPA at
https://launchpad.net/~jonathonf/+archive/ubuntu/python-3.6:
sudo add-apt-repository ppa:jonathonf/python-3.6
sudo apt-get update
sudo apt-get install python3.6
and remember to keep you python 3.5. Don't remove it. and specify your python version in the runtime.txt file: python-3.6.4 and run:
heroku ps:scale web=1 --app [my app's name]
and the problem solved. I hope my answer might help you.
In My case the error was solved by just creating space between web and gunicorn
Before:
web:gunicorn --pythonpath app app.wsgi
After:
web: gunicorn --pythonpath app app.wsgi
While it's not Python, in my case, I had heroku/java followed by heroku/pgbouncer. In Heroku's Settings, I switched them so heroku/pgbouncer was on top. This fixed the issue. Perhaps your buildpacks need to be ordered differently if you are using multiple.

Flask database migrations on heroku

With my app I am using flask-script and flask-migrate for database migrations, everything works locally. When, I run
heroku run python manage.py db init
It creates this output:
Running python manage.py db init on ⬢ fpds-scheduler... up, run.1290 (Free)
Creating directory /app/migrations ... done
Creating directory /app/migrations/versions ... done
Generating /app/migrations/README ... done
Generating /app/migrations/script.py.mako ... done
Generating /app/migrations/alembic.ini ... done
Generating /app/migrations/env.py ... done
Please edit configuration/connection/logging settings in '/app/migrations/alembic.ini' before
proceeding.
But when I run heroku run python manage.py db migrate I get an error
alembic.util.exc.CommandError: Path doesn't exist: 'migrations'. Please use the 'init' command to create a new scripts folder.
When I run heroku run bash and look at my directory I can see that there is no migrations folder...
I have tried running giving the command --app fpds-scheduler but that doesn't seem to be working either.
I am not sure what is going wrong?
You must not create the migrations on Heroku itself. The filesystem is ephemeral and anything written programatically will be lost between invocations.
You need to create the migrations locally, commit them to version control, deploy, and only then run them on Heroku.

Categories

Resources