Python Development Environment: Django with Postgre on CentOS 7

My thought process and steps taken to setup a development environment for learning django/python development.

My focus for the past year and a half has been mostly on networking, security, and systems administration / break fix type topics. However I have recently decided to dive into programming. I decided to start the journey into python, and I wanted to combine that with django for web app development. I quickly found how important it is to setup a Python Development Environment.

The Goal:

My end goal is to produce a site where users sign up/create accounts, upload audio files, and have those audio files populate a web/mobile compatible audio player that can either be embedded on the user’s own sites or linked to on the parent site.

Now this is an ambitious goal for a programming newb, but I’ve learned a lot along the way already. Mainly, how important a ‘Python development environment’ is.

So far I figured that I want to use Django for the backend, and per recommendation of the internet masters, postgre is a better database than sqlite, which is what comes default in Django. Getting in to why is beyond the scope of this post, but I wanted to know how to integrate postgre with django from the jump.

I also did not want to program in windows, as I am much more comfortable with navigating the command line on Linux based OS’s. Using VMBox to create a virtual machine, I decided on using CentOS minimal because it is a lightweight command-line-only OS that does not need much in the way of computing resources.

Dependency Hell

Along this journey so far, I have already come to learn about the phenomenon known as dependency-hell. My first attempts at getting python 3 working with django, postgre, and psycopg2 (needed to integrate postgre with django), left my head hurting and me feeling like maybe I better stick to break-fix.

CentOS is an operating system focused on reliability/stability, so it ships with the older Python 2. Removing this or upgrading this in the global environment will break the entire system. Luckily I discovered the practice of using virtual environments.

Version Control

Backtracking a bit here, I knew that I also wanted to have some form of “version control”.

This will serve as a way to go back to working versions of my code as I inevitably break everything.

Also,I will be able to develop my app from work during down time, as well as at my home on my personal PC.

Having dabbled in programming here and there over the years, I know that git / github are excellent ways to do this. This led to another whole challenge as git comes with its own commands and best practices and whatnot.

After installing package after package, getting error after error…fumbling through the creating your first django app tutorials, breaking CentOS and deleting the whole VM to start over multiple times…. I finally came to the following learning goals:

  1. Create a stable “development environment” on CentOS 7 that uses Python 3, Django, and Postgre
  2. Be able to push/pull the code to a Github repository, and have the code ‘just work’ on multiple systems

From here on out in this post, I will outline the process I came up with to do this. I’m not sure if it’s good, I’m sure it can use improvement, and I’m posting this to hopefully get some critiques and advice.

How I Created my ‘ Python Development Environment’

Setting Up the ‘Global Environment’

First was creating a VM in VirtualBox. I grabbed the CentOS 7 minimal .iso and created a VM. I am using a bridged network adapter because that was the easiest way for me to access the internet from my VM and access my VM from my Host PC on my network.

Once that was up and running, I followed this tutorial to get a version of Python 3 running on CentOS, which as I mentioned ships with the older Python 2 version for stability purposed. (Side Note: I love DigitalOcean). This tutorial was for an older version of CentOS and python, but I just replaced the numbers with what I needed.

Next up, installing pip. Once again I followed the tutorial above for this.

After pip was in my global environment, I installed virtualenv.

That does it for the global environment, because installing things there can really make you want to smash your head against a pane of glass as I learned the hard way.

Setting Up the ‘Virtual Environment’

Now it was time to create my virtual environment for my first Django App.

First I created a directory for my code from my non-root admin user’s home directory.

mkdir code && cd code

At this point, I created my virtual environment.

virtualenv audio

This created my virtual environment, so the next step is “activating” it which I did by executing the following command:

source ./audio/bin/activate

Now, it’s time to start installing dependencies. First up: Postgre. I basically followed this tutorial, while making some changes based on what I had setup. Because I already had development tools in my global environment, I just issued the following:

sudo yum install postgresql-server postgresql-devel postgresql-contrib gcc

After this I needed to setup the PostgreSQL configuration. Once again, I followed the tutorial but basically it consisted of initializing the Postgre Db (sudo postgresql-setup initdb), starting the service, and editing the pg_hba.conf file. Blindly following tutorials, we all start somewhere, right?

After the config, I restarted the service and configured it to start automatically at boot (systemctl enable postgresql).

Finally, I created a database for my project and a user.

Django:

Now it was time to isntall Django:

pip install django psycopg2

Next I had to change the settings.py file from django to use the postgre database. I followed the final steps in this tutorial and replaced the values with my specifics.

Next, I know from past experience I had to open a port in the CentOS firewall to allow connection to Djangos development web server:

$ sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent
$ sudo firewall-cmd --reload 

Again, based on experience I knew that I had to add my VM’s IP address to the “Allowed Hosts” part of Django’s settings.py file.

ALLOWED_HOSTS = ['8.8.8.8']

Replace that with your IP obviously. Now I should be able to run the server and access the pages.

python manage.py runserver 0:8080

Now I opened a browser on my Host machine, navigated the MY.IP.ADDRESS.HERE:8080 and voila, it works!

Cool, I have the basics of a Django app here, now how to implement version control?

Version Control

First, it was time to create the requirements.txt file. I know this may not be the best and most recent way to do this, but it is a start for me. I issued the following:

pip freeze > requirements.txt

Now, Git is installed on my CentOS machine, and I created a repo on Github under my account there.

Next I navigated to my project directory in the virtual environment. I then issued the following commands to get my local repo going:

git init 

#ignore the virtual environment directory
echo 'audio' > .gitignore 

#add all files to commit staging
git add . 

#commit
git commit -m "First commit of Django App"

#add remote repo
git remote add origin https://my link here 

#push

git push -u origin master 

Consequently, it showed up in the repo on Github.com. Cool!

Next I created a new virtual environment, cloned the repo, and did a pip install -r requirements.txt.

Started the django server from this new env, and boom, it works in my browser!!!!!

Conclusion:

Finally, I have a fuzzy understanding of everything I’ve done here, and I have at least an understanding of WHY I’m doing it. I will be revising this post as I get more experience, and adding clarifications.

Honestly, I’m not sure if this post will help anyone gain an understanding of a python development environment or just confuse people more.

Still, I hope that some experienced developers can chime in and critique my thinking and process.

Seems like my next step in this adventure will be creating a user signup for the app.

Stay tuned and thanks for reading!

Want to work together? Contact me!

Also, make sure to check out my other blog posts if you found this interesting 🙂

Alex Gray

Author: Alex Gray

Aspiring IT professional focusing on networking, network security, advanced troubleshooting, web development, consulting, and more.

Leave a Reply

Your email address will not be published. Required fields are marked *