2

I have made a code with python flask and I am following the next steps to deploy it:

Deploying to Heroku (takes 7 minutes)

  1. Install heroku (if you don't have it yet)

    $ npm i heroku -g
    
  2. Login to heroku on the command line (if you have not already)

    $ heroku login -i
    
  3. Create an application (if you don't have it already)

    $ heroku create <your_application_name>
    

Enviroment Variables (takes 2 minutes)

Now navigate to your heroku dashboard and look for your application settings, we have to manually add our environment variables into heroku:

You cannot create a .env file on Heroku, instead you need to manually create all the variables under your project settings.

Open your .env file and copy and paste each variable (FLASK_APP, DB_CONNECTION_STRING, etc.) to Heroku.

Deploying your database to Heroku (takes 3 minutes)

You local MySQL Database now has to be uploaded to a cloud, there are plenty of services that provide MySQL database hosting but we recommend JawDB because it has a Free Tier, its simple and 100% integrated with Heroku.

  1. Go to your heroku project dashboard and look to add a new heroku add-on.

  2. Look for JawDB MySQL and add it to your project (it may ask for a Credit Card but you will not be charged as long as your remain within 5mb database size, enough for your demo.

  3. Once JawDB is added to your project look for the Connection String inside your JawDB dashboard, something like:

    mysql://tqqa0ui0cga32nxd:eqi8nchjbpwth82v@c584md9egjnm02sk.5btxwkvyhwsf.us-east-1.rds.amazonaws.com:3306/45fds423rbtbr
    
  4. Copy the connection string and create a new environment variable on your project settings.

  5. Run migrations on heroku: After your database is connected, you have to create the tables and structure, you can do that by running the pipenv run upgrade command on the production server like this:

    $ heroku run -a=<your_app_name> pipenv run upgrade
    

    :warning: Note: Notice that you have to replace <your app name> with your application name, you also have to be logged into heroku in your terminal (you can do that by typing heroku login -i)

    Push to the Heroku codebase

    Commit and push to heroku, make sure you have added and committed your changes and push to heroku

    $ git push heroku main hh
    

    That is it!

Now the problem is when I run this command:

heroku run -a=<your_app_name> pipenv run upgrade 

And the respons is:

bash: pipenv: command not found 

This is my Pipfile.txt:

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
flask = "*"
sqlalchemy = "*"
flask-sqlalchemy = "*"
flask-migrate = "*"
flask-swagger = "*"
psycopg2-binary = "*"
python-dotenv = "*"
mysql-connector-python = "*"
flask-cors = "*"
gunicorn = "*"
mysqlclient = "*"
flask-admin = "*"
cloudinary = "*"
flask-login = "*"
pipenv = "*"

[requires]
python_version = "3.8"

[scripts]
start="flask run -p 3000 -h 0.0.0.0"
init="flask db init"
migrate="flask db migrate"
upgrade="flask db upgrade"
deploy="echo 'Please follow this 3 steps to deploy: https://github.com/4GeeksAcademy/flask-rest-hello/blob/master/README.md#deploy-your-website-to-heroku' "

These are the commands I run before deploying:

pipenv install;
mysql -u root -e "CREATE DATABASE example";
pipenv run init;
pipenv run migrate;
pipenv run upgrade;

If I don't run the upgrade in heroku this is what Release Log in heroku:

sqlalchemy.exc.DatabaseError: (mysql.connector.errors.DatabaseError) 2003 (HY000): Can't connect to MySQL server on 'localhost' (111)
(Background on this error at: http://sqlalche.me/e/14/4xp6)
Ruli
  • 2,592
  • 12
  • 30
  • 40

1 Answers1

1

It seems you are missing the pipenv tool or missing in PATH. You may install it using:

$ pip install pipenv

If pipenv already is installed check the PATH variable and if you are able to locate pipenv using $ which pipenv. Have a look at the docs regarding PATH:

IODEV
  • 1,706
  • 2
  • 17
  • 20
  • i have pipenv installed, the commands pipenv run init, pipenv run migrate and pipenv run upgrade work perfectly as well as pipenv --version, it is just in heroku run that its not responding. I have already hosted this site and all went well, but its my first time so i make a mistake and had to start all over from cero, make the exact same steps and now that specific command is not working – boris bruno Apr 20 '21 at 03:31
  • Ok, but it quite obvious that something is missing here regarding the search path otherwise you wouldn't get the error `pipenv: command not found`. The big challenge is to find out why and where. – IODEV Apr 20 '21 at 06:20
  • What do you get from: `$ heroku run echo $PATH`? and `$ heroku run which pipenv`? – IODEV Apr 20 '21 at 08:56
  • 1
    Also have a look at these: - https://stackoverflow.com/questions/53164484 - https://stackoverflow.com/questions/51948753 - https://stackoverflow.com/questions/56037512 – IODEV Apr 20 '21 at 09:04
  • Btw, check if you've pushed everything to heroku. What does the heroku logs say? – IODEV Apr 20 '21 at 09:40
  • i get this on heroku run echo $PATH: `/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/mysql/bin/:/usr/local/mysql/bin/` – boris bruno Apr 20 '21 at 21:09
  • from heroku run which pipenv i get nothing and when i push the logs says: `sqlalchemy.exc.DatabaseError: (mysql.connector.errors.DatabaseError) 2003 (HY000): Can't connect to MySQL server on 'localhost' (111) (Background on this error at: http://sqlalche.me/e/14/4xp6)` – boris bruno Apr 20 '21 at 21:20
  • 1
    #SOLVED it is all solved, thanks for the support, i solved with a friend and it was the requirements.txt that i had to add it because the code already had a file Procfile for heroku to run the commando pipenv run upgrade, i didnt had to run it by myself. – boris bruno Apr 20 '21 at 23:02