- Do you need to work on any other computers than yours in which you do not want to (cannot) install everything you want?
- Do you want to bring with you on a USB stick your working Django development environment and your projects?
Then, follow this tutorial. My goal was to be able to bring with me my django development environment and my projects on a USB stick everywhere, fully working on any Windows computers without any setup.
It includes:
- Python
- Django
- Git
- Bash Shell (shipped with Git)
- virtualenvs fully working
- pip available to install packages
First of all, let me explain the global structure of the folders:
|
1 2 3 4 5 6 7 |
development/
|-- launch.bat # use to launch the Bash shell with the right variables
|-- projects/ # store all your projects
|-- tools/
|-- PortableGit/
|-- PortablePython/
|-- virtualenvs/ # store all the virtualenvs you create |
1. Install Python and Git
(These steps must be done just once.)
- Download the zip on GitHub containing the global structure and start from there: https://github.com/YAmikep/django-portable
- Install Portable Python
Unzip it in development/tools/PortablePython
- Install Portable Git
Get it from http://code.google.com/p/msysgit/downloads/list
Unzip it in development/tools/PortableGit
2. Setting up virtualenv and virtualenvwrapper
- Launch the Bash shell by double clicking on launch.bat
- Install pip: (see more on the pip website)
curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python
- Install virtualenv & virtualenvwrapper via pip:
pip install virtualenv virtualenvwrapper
- Modify tools\PortablePython\App\Scripts\virtualenvwrapper.sh:
Look for “function workon“.
Add the following code after this block:
|
1 2 3 4 5 6 |
activate="$WORKON_HOME/$env_name/$VIRTUALENVWRAPPER_ENV_BIN_DIR/activate"
if [ ! -f "$activate" ]
then
echo "ERROR: Environment '$WORKON_HOME/$env_name' does not contain an activate script." >&2
return 1
fi |
(You can use the file development\tools\virtualenvwrapper.sh_modification.txt shipped with the zip to copy/paste the code.)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
right_python_root=$WORKON_HOME/$env_name/$VIRTUALENVWRAPPER_ENV_BIN_DIR
right_python_bin_path=$right_python_root/python.exe
# Change the python interpreter in all .py files to use the right one
for f in $right_python_root/*.py
do
#echo "----"
#echo file=$f >&2
first_line=`head -n 1 $f`
#echo first_line=$first_line >&2
case "$first_line" in
\#!*python.exe* )
# Change slash by backslash
new_path=`echo $right_python_bin_path | awk '{gsub("/","\\\")}1'`
#echo new_path=$new_path >&2
# Remove first backslash
new_path=${new_path/\\/}
# Replace second backslash by colon and backslash
new_path=${new_path/\\/:\\}
# Add #! at the top
new_line="#!"$new_path
#echo new_line=$new_line >&2
# Rewrite first line
sed -i "1s,.*,${new_line//\\/\\\\}," "$f"
esac
done |
3. Setting up Git
- Modify tools\PortableGit\etc\git-completion.bash:
Add at the end of the file:
(You can use the file development\tools\git-completion.bash_modification.txt shipped with the zip to copy/paste the code.)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
MY_PATH=`pwd`
right_python_root=$MY_PATH/tools/PortablePython
right_python_bin_path=$right_python_root/App/python.exe
# Change the python interpreter in all .py files to use the right one
for f in $right_python_root/App/Scripts/*.py
do
#echo "----"
#echo file=$f >&2
first_line=`head -n 1 $f`
#echo first_line=$first_line >&2
case "$first_line" in
\#!*python.exe* )
# Change slash by backslash
new_path=`echo $right_python_bin_path | awk '{gsub("/","\\\")}1'`
#echo new_path=$new_path >&2
# Remove first backslash
new_path=${new_path/\\/}
# Replace second backslash by colon and backslash
new_path=${new_path/\\/:\\}
# Add #! at the top
new_line="#!"$new_path
#echo new_line=$new_line >&2
# Rewrite first line
sed -i "1s,.*,${new_line//\\/\\\\}," "$f"
esac
done
#
# Define some alias
#
alias shell="python manage.py shell"
alias dbshell="python manage.py dbshell"
alias shell_plus="python manage.py shell_plus"
alias syncdb="python manage.py syncdb"
alias runserver="python manage.py runserver"
alias cleanpyc="find . | grep '.pyc$' | xargs rm -f"
# Nice shortcut to load the virtual env and go to the project folder
#alias go="workon ;cd projects/project_name/"
export WORKON_HOME=$MY_PATH/tools/virtualenvs
source "$right_python_root/App/Scripts/virtualenvwrapper.sh" |
4. Creating a virtual env
- Launch the Bash shell by double clicking on launch.bat
- Use mkvirtualenv:
mkvirtualenv <VIRTUALENV_NAME>
Use the name you want for you virtual env.
Make sure to use the same <VIRTUALENV_NAME> in the next steps.
By the way, you might see the following error which also appears when you launch the .bat:
bash: mktemp: command not found ERROR: virtualenvwrapper could not create a temporary file name.
No worries, I could not get rid of it but everything will work anyway.
- Modify launch.bat:
Uncomment the following line and change <VIRTUALENV_NAME> by the name you chose for your virtual env:
echo %PYTHON_ROOT%\App > %VIRTUALENVS_ROOT%\<VIRTUALENV_NAME>\Lib\orig-prefix.txt
- Modify tools\virtualenvs\<VIRTUALENV_NAME>\Scripts\activate:
Look for “export VIRTUAL_ENV” and replace it by:
export VIRTUAL_ENV=$MY_PATH/tools/virtualenvs/<VIRTUALENV_NAME>
Do not forget to change <VIRTUALENV_NAME> by the name you chose for your virtual env.
You are now able to use your virtual env:
workon <VIRTUALENV_NAME>
You will see the name of your virtual env between parenthesis when you are working on it.
You can now install almost any packages you want into your virtual env by using pip. (lxml cannot be install via pip, check out my article about Installing lxml in a simple and quick way on Windows)
For example:
pip install django ipython yolk
To exit it, just type: deactivate
This is the end of the tutorial, I hope some people will find it useful.
Feel free to leave comment and/or send me your feedback to improve this “homemade” solution.
I wanted to keep this tutorial as simple as possible so I did not talk about PostgreSQL but if you are interested about using PostgreSQL on this portable environment, let me know.
NB: This tutorial has been tested with these versions:
- PortablePython 2.7.3.1
- PortableGit-1.7.11-preview20120710
