Thursday, February 8, 2018

Windows 10 Linux Subsystem, Flask with PyEnv Setup

The new Linux Ubuntu subsystem in Windows 10 is making quite a good impression, especially for developers. Also, using PyEnv with Virtual Environments allows greater flexibility in setting up development projects.

Linux Setup

Lets first prepare the Linux distribution for development:
    sudo apt-get update
    sudo apt-get upgrade

Development Baseline

Setup the development foundation:
    sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl libncursesw5-dev xz-utils tk-dev

Install PyEnv

Let's install the python version manager:
    curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash

Add the needed  environment variables:
    ~/.profile
    export PATH="/home/user/.pyenv/bin:$PATH"
    eval "${pyenv init -}"
    eval "${pyenv virtualenv-init - }"

Now update the running environment:
    source .profile

Create Project

Download the latest Python 3 stable available version:
    pyenv install 3.4.6

Create the virtual environment with corresponding Python version:
    pyenv virtualenv 3.6.4

Now install the rest of the tools needed!

Thursday, January 21, 2016

Ubuntu 15.10 64 bits with Node.js and Loopback

Well the time came to meet Node.js again and install the latest version 5.x as well as Loopback due to projects needs, here is the recipe:
  1. sudo apt-get update
  2. sudo apt-get upgrade
  3. sudo apt-get install -y build-essential python python-software-properties g++ make git
  4. curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash -
  5. sudo apt-get install -y nodejs
Once all the previous has successfully installed, we install Loopback, and take our time for coffee, since it takes a while:
  1. sudo npm -g install strongloop

Wednesday, November 25, 2015

VirtualBox Guest: Ubuntu 15.10 Python Development Server

I am now using Ubuntu 15.10 and Python 3 as a professional developer. The development environment is located in a VirtualBox running Ubuntu 15.10 Server almost bare-bones, ONLY pre-installed with OpenSSH server.

Let's prepare the development environment with the latest Django 1.9 using Python 3.5, GIT and VirtualEnvWrapper. I recommend using MobaXtexm to login into your Vbox using SSH keys.

Update Ubuntu to the latest patches and security updates and improvements:
sudo apt-get update; sudo apt-get upgrade
sudo apt-get install build-essential python2.7 python2.7-dev python3.4 python3.4-dev python3.5 python3.5-dev
sudo apt-get install mysql-server mysql-client
sudo systemctl status mysql
sudo apt-get install apache2
sudo systemctl status apache2
sudo apt-get install python2.7-mysqldb python3.4-mysqldb python3.5-mysqldb libmysqlclient-dev
sudo apt-get install sqlite3 libsqlite3-dev libreadline6-dev libgdbm-dev
sudo apt-get install zip zlib1g-dev libbz2-dev
sudo apt-get install python-pip python3-pip
sudo apt-get install git virtualenv virtualenvwrapper
Add python alternatives for more flexibility:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 10
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.4 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2
Make sure all is in it's place:
sudo apt-get update; sudo apt-get upgrade
Modify file .bashrc for VirtualEnv use:
vim ~/.bashrc
if [ -f /usr/local/bin/virtualenvwrapper.sh ]; then
    export WORKON_HOME=$HOME/.virtualenvs
    source /usr/local/bin/virtualenvwrapper.sh
fi
source ~/.bashrc
Now let's create the working environment:
mkvirtualenv some-project-name
pip3 install Django==1.9
Make sure we are running the intended version, start a python 3.5 session, import django and check the version:
python3.5
>>> import django
>>> print(django.get_version())
1.9
Prepare to create the initial Django setup:
mkdir src; cd src
django-admin startproject some-project-name
cd some-project-name
python3.5 manage.py migrate
python manage.py runserver 0.0.0.0:8080
We point out browser to the needed site to check (Virtual Box was previously setup for port forwarding): http://127.0.0.1:8888
Finally we setup a basic Git repo:
cd some-project-name
git init
git config --global user.name 'Your Name'
git config --global user.email your.address@your.provider

Sunday, October 12, 2014

Ubuntu 14.04 Django 1.7 Development With VirtualEnvWrapper and Git

I am now using Ubuntu 14.04 and Python3 as a professional developer. Now I need to setup a development environment using the latest Django 1.7 and also use Git and VirtualEnvWrapper, here is how I did it:

Update Ubuntu to the latest, from the command line:
sudo apt-get update
sudo apt-get upgrade


Lets add Git for source code versioning:
sudo apt-get install git


Ubuntu 14.04 already comes with Python 3 pre-installed, so all we need is to add the pip3 command:
sudo apt-get install python3-pip


In order to work with python3 and pip3 more as if it were the default python, add the following to the .bash_aliases file and source the file to added these new aliases:
python='python3'
pip='pip3'


Now all we need is to add is a development sandbox:
sudo pip install virtualenv
sudo pip install virtualenvwrapper


Add the virtualenvwrapper settings to the .bashrc file and source the file to setup the environment for use:
# VirtualEvnWrapper settings:
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/dev
source /usr/local/bin/virtualenvwrapper.sh


Let's create the working environment:
pip install Django==1.7


Finally we setup Git:
git config --global user.name 'Your Name'
git config --global user.email your.address@your.provider


One thing to notice, is that the current Django 1.7 driver for MySQL is not working: MySQLdb. Yet the python connector supports Django and works fairly well:
pip install --allow-all-external mysql-connector-python

DATABASES = {
    'default': {
        'ENGINE': 'mysql.connector.django',
        'NAME': 'YourDatabaseName',
        'USER': 'YourUserName',
        'PASSWORD': 'YourPassword'
    }
}

Monday, May 7, 2012

Python for Android, Part 1

We are now in the stage where we will start our learning curve on Android, and first things first. We need to prepare our development platform.

My assumptions are the following:
So we begin by installing Java, in this case Open Java JDK. Please follow the next steps (taken from Ubuntu Community Documentation Java):
  1. On a terminal window add the following commands:
    sudo apt-get install openjdk-6-jre
    sudo apt-get install icedtea6-plugin
    sudo apt-get install openjdk-6-jdk

  2. Download and Install the Android Development Kit:
    Download the Android SDK Starter Package
    Instructions on Installing the SDK

  3. Untar, unzip, and install (this is the latest version to the date of this post):
    cd
    tar -xzvf android-sdk_r18-linux.tgz
    sudo mv android-sdk-linux/ /opt
    cd /opt
    sudo ln -s android /opt/android-sdk-linux
    sudo chown -R (your-user-id):(your-user-group-id) 
    *

    NOTE I recommend to install in /opt (which is what I did), yet you can do it where ever you may want.

  4. Now execute the installer:
    cd tools
    ./android

  5. Select Android 2.2 latest revision and the Android SDK download will begin!

This is it, now stay tuned for Part 2 coming soon!

Sunday, April 29, 2012

Learning Python

So far so good! I have now managed to create my 1st webapp following the HF Python book, and one neat feature to know (though I come from a Django background, is the use of the CGI debugger feature!

If your like me, learning the ins and outs of the Python foundation, this will allow you to get more information on the standard error, streamed at the browser, for debugging purposes (HF Python, page 248). Add the following two lines of code to your main webapp script and let Python take care of showing you the details of the errors it has found:



import cgitb
cgitb.enable()

So now we go on to Python for Android and I'm sure you will like these two links:

Friday, April 27, 2012

Python 3

I have now formally started the python 3 learning curve, and have to say that Head First Python is a must read for any beginner or mid-level. Stay tuned for news and highlights on this new phase!

Friday, September 5, 2008

Thank you

It's been a while since I come in and do some writing; any ways, I would like to thank the following person, who inspired me to create a blog and keep it going:

Sandeep Krishnamurthy, Ph.D.

Friday, April 11, 2008

InnoTek Share From Linux Host to Linux Guest

I am using Ubuntu 7.10 and have created an InnoTek Guest virtual machine with Ubuntu 6.10, this due to project constraints. And so I needed a way to share between both my machines in an easy and practical way, thus I followed the instructions I found at:


How do I automatically mount my shared folder


The short story is:


1) Create a virtual machine with Shared Folders


2) Install the InnoTek Guest Additions on the given guest machine

3) Check what is the "name" of the shared folder, in my case it was "share"


4) Start your virtual machine and login


5) From a terminal console, edit the /etc/modules file and add at the end of the file: vboxvfs, vboxadd



# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.

lp
vboxvfs
vboxadd





6) Also modify the /etc/fstab file to mount the share



# /etc/fstab: static file system information.
#
#
proc /proc proc defaults 0 0

/dev/hdc /media/cdrom0 udf,iso9660 user,noauto 0 0
/dev/ /media/floppy0 auto rw,user,noauto 0 0
share /home/fbenavides/Share vboxsf defaults 0 0

Now, reboot and the share is working properly; otherwise, follow the previous link for more hints as to solve this issue.


Monday, March 31, 2008

Agua de Horchata

El agua de horchata, es algo muy Mexicano, aquí les paso una receta.

Ingredientes:
1/2K Arroz largo
2L Agua tibia

1 Varita de canela entera
1 Ralladura de piel de naranja
1 Lata de leche evaporada
Azúcar al gusto

Preparación
Remojar el arroz en agua tibia toda la noche, junto con la canela y la ralladura de naranja.
Al día siguiente, colar y conservar el líquido; licuar el arroz y la canela con un poco del agua del remojo anterior.
Colar de nuevo.
Agregar todo el líquido de conservado del remojo anterior, agregando la leche evaporada y endulzando con el azúcar al gusto.
Servir bien fría.
NOTA: Para endulzar, también puede usar la leche condensada.

Here is the recipe for the Mexican Horchata water:

Ingredients:
1/2K Large size rice
2L warm water (not so warm)
1 Straw of cinnamon
1 Orange shell grating
1 Can of evaporated milk
Sugar at will to sweeten

Preparation
Leave the rice in the water all night, along with the straw of cinnamon and the orange grating.
Percolate keeping the water for later.
Liquefy the rice, cinnamon and orange grating with a little of the previously saved water.
Percolate again.
Add the previously saved water, the evaporated milk and sugar at will.
Serve cold.
NOTE: You can sweeten the beverage using sweeten condensed milk as well.

Tuesday, March 25, 2008

Django Template Encoding

When using templates, and we need to use character which are not included in the ASCII character set, like the Spanish accentuated characters, Djanfgo must be informed about such a thing in order to properly handle these characters.

To this end, we need add the following at the first or sencond line of code of the Django source code file:

# coding: utf-8

This will allow rendering Spanish characters like te following, without any problem:

á é í ó ú ü ñ Á É Í Ó Ú Ñ

Here is a good place to start and further understand what we have so far explained here:
Defining Python Source Code Encodings

Wednesday, March 19, 2008

Django Development Server Style Sheet Configuration

When using style sheets (css), specially when using the Django development server, some adaptations are needed in get both the style sheets and the images served. For this, we need use the media path as defined for the framework.

settings.py
First the media path must be defined and must differentiate it self:

MEDIA_ROOT = '/path/to/media/'
MEDIA_URL = ''
ADMIN_MEDIA_PREFIX = '/amedia/'

urls.py
Now we add/link the path to the media path itself:

if settings.DEBUG:
urlpatterns += patterns('', (r'^media/(?P.*)$', 'django.views.static.serve', {'document_root':'/home/fbenavides/Documents/workspace3dot3/txm/src/txm/media/', 'show_indexes': True}), )

index.html
In the given HTML file header (index.html is just an example file), the style sheet link must be added:

<link href="/media/css/bluebusiness.css" rel="stylesheet" type="text/css" />

It is assumed that the media path is as follows:

/media/
/media/css/
/media/images/

Thus when calling other style sheets and/or images, these must follow the proper call path.

Friday, March 14, 2008

Eclipse Django Debugging

In order to debug, the preferred way for me, is to do it through the Eclipse with PyDev configured for Django. To do this, we need a couple more of adjustments in order to get the django runserver running properly.

We go the main menu and we select the Open Run Dialogue... window, there we select on the left Python Run and on the Main tab to the right we select the project where we are going to make the debugging: /some/path/to/sources/manage.py.

In the Arguments tab we add the Program Arguments: runserver --noreload and then we finally select the Environment tab where we add the
DJANGO_SETTINGS_MODULE set to value settings and PYTHONPATH set to value $PWD. Click Apply and Run.

If you look at the console view, you will see the runserver output which indicates that all is well:


Validating models...
0 errors found

Django version 0.97-pre-SVN-unknown, using settings 'txm.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.


The catch here, is that in order to have the server pick up the changes we make, we will need to stop it an rerun it every time.

So far so good, till next time.

Eclipse Django Environment

I'm a newbie in Django and Eclipse with PyDev, have only used Eclipse very superficially with C/C++, which is still my favorite language. Still, as time goes on, more and more needs to be done in a way that will allow rapid development and ease of installation and maintenance. Here is where I meet Django, my new job requires me to be as efficient as can possibly be, thus using Eclipse is a good alternative, among various others that can be found on the net. I will not go into the details of installing neither Eclipse nor Django, this exercise only focuses and the issue of preparing the Eclipse platform to allow us to be more productive in the both programming and debugging this mature Web Framework.

I am using Eclipse 3.3 with CDT, JDT, and MyLynt development, we need the Python and XHTML support, for this we will add the Amateras HTML plugin and the PyDev module.
WARNING, my system currently uses Python 2.5 and I am using the Django 0.97 developing branch which better supports the newforms.:
After downloading, extract the compressed file to where ever you may want to, and copy to the Eclipse path. I am using Ubuntu 7.10 and what I did is manually install Eclipse in /opt/eclipse. Thus I copied the resulting files from Amateras and PyDev accordingly:

Amateras decompressed folder

cd /some/path/to/Amateras/decompressed/folder/eclipse

sudo cp -R plugins/* /opt/eclipse/plugins/

sudo cp -R features/* /opt/eclipse/features/

PyDev decompressed folder

cd /some/path/to/Pydev/decompressed/folder/plugins

sudo cp -R * /opt/eclipse/plugins/

Once the files have been copied, we need to configure the Eclipse, so it knows where the python executable lives as well as the django installation, for that we go to the main menu and Windows>Preferences>PyDev>Interpreter Python and we Add the location of the python executable, in my case: /usr/bin/python. Then we add the New Folder for the PYTHONPATH to Django: /usr/lib/python2.5/site-packages/django. Click Apply/OK and were done.

With this we are done setting up our development environment, hope this helps any one.