====== Python Development Environment ====== As most of the time, we'll start with our [[:vms:debian|Debian Template VM]] ===== Python Versions ===== Although Python 3 is the current version, many applications and scripts are still depending on Python 2. Also Python 2 is the standard installed version on Debian 8 Jessie > python -V Python 2.7.9 To make things clearer one could use the ''python2'' command, to have a look at the available commands: > ls -la /usr/bin | grep -ie python -rwxr-xr-x 1 root root 1056 Mar 16 2015 dh_python2 lrwxrwxrwx 1 root root 23 Jun 29 2016 pdb2.7 -> ../lib/python2.7/pdb.py lrwxrwxrwx 1 root root 9 Mar 16 2015 python -> python2.7 lrwxrwxrwx 1 root root 9 Mar 16 2015 python2 -> python2.7 -rwxr-xr-x 1 root root 3781768 Jun 29 2016 python2.7 lrwxrwxrwx 1 root root 29 Mar 16 2015 pyversions -> ../share/python/pyversions.py It seems that, contrary to most other distributions, having Python (2.7) installed on Debian doesn't imply ''pip'' being available, let's install it: === PIP (2) === > sudo apt-get install python-pip python-dev build-essential > pip -V pip 1.5.6 from /usr/lib/python2.7/dist-packages (python 2.7) === Python 3 + PIP (3) === > sudo apt-get install python3 python3-dev python3-pip We now have the Python and pip versions available: > python -V Python 2.7.9 > pip -V pip 1.5.6 from /usr/lib/python2.7/dist-packages (python 2.7) > python2 -V Python 2.7.9 > pip2 -V pip 1.5.6 from /usr/lib/python2.7/dist-packages (python 2.7) > python3 -V Python 3.4.2 > pip3 -V pip 1.5.6 from /usr/lib/python3/dist-packages (python 3.4) Notice that ''python'' and ''python2'' are the same, this is also true for ''pip'' and ''pip2''. ===== Virtual Environments ===== [[http://joebergantine.com/blog/2015/apr/30/installing-python-2-and-python-3-alongside-each-ot/]] Virtual Environments are used to isolate python versions and package dependencies for each specific project. We'll begin by creating specific users for python: > adduser py2 ... ... > adduser py3 ... ... # To act as py2: > su py2 We'll install ''virtualenv'' for User only in order to allow coexistence of Python2 and Python3. As the virtualenv binary will bear the same name for 2 & 3, we'll move them on the go: === Virtualenv 2 === > pip install --user virtualenv > mv ~/.local/bin ~/.local/lib/python2.7/ > nano ~/.bashrc ADD: # USER INSTALLED BINARIES [virtualenv(wrapper)] export PATH=$PATH:~/.local/bin/ > source ~/.bashrc > virtualenv --version 15.1.0 To create a **new directory** for a virtualenv, and then activate it: > virtualenv2 env_name > source env_name/bin/activate > cd env_name [> deactivate] To do the same in an **existing directory**, first move to this location and: > cd /path/to/my/env > virtualenv2 . > source ./bin/activate [> deactivate] === Virtualenv 3 === Please note that in the case of ''virtualenv 15.1.0'', the version seems to be compatible with both Python2 and Python3, so it might not be necessary to duplicate the operation, but we'll go for it anyways. > pip3 install --user virtualenv > mv ~/.local/bin ~/.local/lib/python3.4/ > nano ~/.bashrc ADD: alias virtualenv3='~/.local/lib/python3.4/bin/virtualenv' > source ~/.bashrc > virtualenv3 --version 15.1.0 ==== Virtualenvwrapper ==== [[https://virtualenvwrapper.readthedocs.io/en/latest/]] === Installation & Configuration === Install virtualenvwrapper for python3 for the user only: > pip3 install --user virtualenvwrapper > mv ~/.local/bin/* ~/.local/lib/python3.4/bin/ > rm ~/.local/bin > mkdir -p projects/code > nano .bashrc ADD: # USER INSTALLED BINARIES [virtualenv(wrapper)] export PATH=~/.local/bin:$PATH # VIRTUALENVWRAPPER export WORKON_HOME=$HOME/.virtualenvs export PROJECT_HOME=$HOME[/code] export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python[2|3] export VIRTUALENVWRAPPER_VIRTUALENV=$HOME/.local/bin/virtualenv source $HOME/.local/bin/virtualenvwrapper.sh > source .bashrc virtualenvwrapper.user_scripts creating /home/dev/.virtualenvs/premkproject ... ... virtualenvwrapper.user_scripts creating /home/dev/.virtualenvs/get_env_details > mkvirtualenv temp Using base prefix '/usr' New python executable in /home/dev/.virtualenvs/temp/bin/python3 Also creating executable in /home/dev/.virtualenvs/temp/bin/python Installing setuptools, pip, wheel...done. ... ... (temp) > === Projects management === [[http://wiki.strategicz.com/vhyper/doku.php?id=vms:python:django#using_a_shared_folder_to_host_your_projects]] > VBoxManage setextradata VBoxInternal2/SharedFoldersEnableSymlinksCreate/ 1 > nano /etc/fstab # automount shuup project space /home/dev3/projects vboxsf defaults,uid=1001,gid=1001,dmode=775,fmode=774,umask=002 0 0 [[https://virtualenvwrapper.readthedocs.io/en/latest/projects.html#project-management]] ===== Django ===== [[http://wiki.strategicz.com/vhyper/doku.php?id=vms:python:django]]