Tag Archives: python

Heroku updates to python

If you see a message like this during one of your heroku builds/pushes, here’s what you should do:

remote: -----> Python app detected
remote:  !     Python has released a security update! Please consider upgrading to python-3.7.7

Take a look at what your runtime.txt file looks like. Mine looks like:

$ more runtime.txt
python-3.7.6

So, fire up your favorite editor and make it match.

Then commit that file, and push it back up to your repo and heroku.

$ git push heroku master
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 300 bytes | 12.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Python app detected
remote: -----> Found python-3.7.6, removing
remote: -----> No change in requirements detected, installing from cache
remote: -----> Installing python-3.7.7
remote: -----> Installing pip
remote: -----> Installing dependencies with Pipenv 2018.5.18…
remote:        Installing dependencies from Pipfile.lock (a33c91)…

Generating requirements.txt from Pipfile.lock

I have been using pipenv to help me manage my python virtual environments.

I also use Heroku for some of my sites…and Heroku relies on the requirements.txt file rather than Pipfile/Pipfile.lock that pipenv uses.

Here is an easy way to generate a requirements.txt file from a Pipfile.lock

jq -r '.default
| to_entries[]
| .key + .value.version' \
Pipfile.lock > requirements.txt

NOTE: You will need the program ‘jq‘ (like sed for JSON data) installed, which can be done via:

sudo apt install jq

I believe that I learned about this method inside the issues tracker for pipenv on GitHub.

Adding django via pipenv

Pip environments with python and django have evolved over the years, but once constant has remained: virtual environments to run the code and packages in. The latest consensus is to use pipenv to keep things separated. Let’s see how.

First, I’ll assume that you already have python 3.7 installed. If you need help, check out this other post I wrote: https://markcerv.com/2019/03/adding-python-3-7-to-ubuntu-18-x/

Next:

sudo apt install python-pip
sudo pip install pipenv

pipenv --python 3.7 install django

In order to use pipenv, you will need to cd to the directory where you want to put your code, and then:

cd  somename
pipenv --python 3.7 install django django-cors-headers \
         djangorestframework pytz
# a bunch of things will happen, including that a virtualenv
# will get get created like:
# ~/.local/share/virtualenvs/somename-oYSXMPkF

# to actually fire up the environment
pipenv shell

Next, you’ll need to setup a minimum of files to get django going. :

django-admin startproject test2

cd test2

python manage.py migrate     #will setup the needed database stuff
python manage.py createsuperuser    # lets you make a u/p for login

./manage.py  runserver  0.0.0.0:8000

You can now jump over to a browser, and visit http://127.0.0.1:8000/admin/ and sign in to your app. It’s pretty boring right now, but you can fix that.

Adding python 3.7 to ubuntu 18.x

If you quickly want to get the latest version of Python installed on Ubuntu and get future updates automatically, then you can install it from the below third-party PPA repository…

To do that, run the commands below to add the PPA.

First install Ubuntu software properties package if it’s not already installed on your system..

sudo apt update
sudo apt install software-properties-common

After that run the commands to add the PPA..

 sudo add-apt-repository ppa:deadsnakes/ppa 

Finally, run the commands below to install Python 3.7

sudo apt update
sudo apt install python3.7

That’s it!