How I start a new Django Rest Framework project in macOS Mojave.

Recently I cleaned my macbook, I erased all disc and I installed macOS Mojave, so I will install django rest framework and I’ll start a project from zero.

So, I started checking default python version:

$ python --version
Python 2.7.10

That's not the python version that I want to use for this new project, first because I have been working with python 3.x version and I want to continue working with it and second I would like to use the latest version that Heroku support for 3.x. (3.7.0)

To solve this issue, I'm going to use pyenv, so I need to install it through brew:

$ brew install pyenv
$ brew install pyenv-virtualenv
$ brew install pyenv-virtualenvwrapper

As you can see, I'm going to use virtualenv and virtualenvwrapper too, so it is necessary to add a couple of lines to .bash_profile

$ vim ~/.bash_profile
# pyenv, virtualenv and virtualenvwrapper settings
export WORKON_HOME=$HOME/.virtualenv
export PROJECT_HOME=$HOME/Code
eval "$(pyenv init -)"

So before to to reload my .bash_profile file, I need to be sure that directories listed in previous settings are already created:

$ mkdir -p ~/.virtualenv
$ mkdir -p ~/Code

Now I can reload my .bash_profile file:

$ source ~/.bash_profile

OK, no problems so far, now I need to install 3.7.0 using pyenv (Spoiler alert: You'll probably face with an error related to zlib):

$ pyenv install 3.7.0

After, some seconds I get an error related to inexistent zlib package, I already installed xcode, so problem shouldn't be related to it, the error says:

zipimport.ZipImportError: can’t decompress data; zlib not available

So, after google some possible solutions, I found that error was related to 
CFlags, LDFlags and unicode, I run these lines and solve the problem (read it on pyenv github issues):

$ CFLAGS=”-I$(brew — prefix readline)/include -I$(brew — prefix openssl)/include -I$(xcrun — show-sdk-path)/usr/include” \
LDFLAGS=”-L$(brew — prefix readline)/lib -L$(brew — prefix openssl)/lib” \
PYTHON_CONFIGURE_OPTS= — enable-unicode=ucs2 \
pyenv install 3.7.0

Problem solved!, I checked python versions installed available:

$ pyenv versions

And as I want to use 3.7.0 as my global version, I run:

$ pyenv global 3.7.0

Great!, I have almost everything to start, I need to add one final line to my .bash_profile: pyenv virtualenvwrapper_lazy, so final version of this file looks like:

# pyenv, virtualenv and virtualenvwrapper settings
export WORKON_HOME=$HOME/.virtualenv
export PROJECT_HOME=$HOME/Code
eval "$(pyenv init -)"
pyenv virtualenvwrapper_lazy

I found this post, with a similar approach if you need more examples or future reference.

I’m ready to start working with this new project which I will call: Bacardi (I will explain name in other post in the future)

$ mkproject bacardi

Last command creates an empty directory inside ~/Code with the name: bacardi, I will specify python version with .python-version file

$ echo "3.7.0" > .python-version

I like to work with pipenv also, in order to handle dependencies, so I run:

$ brew install pipenv

I need to create a Pipfile and Pipfile.lock, running:

$ pipenv install

Finally I need to install django, django rest framework and django rest swagger

$ pipenv install django==2.1.2
$ pipenv install djangorestframework==3.9.0
$ pipenv install django_rest_swagger==2.2.0

And that's it, I'm ready to start bacardi project.