Flask Biography Tutorial Part III-Addendum : Preparing Local Python Environment in Windows and Mac OS X

Python server running in Windows 7 Git Bash environment
Our Python Application Server Running in Windows 7 Git Bash environment

As I write my 4th article talking about using Bootstrap 3 and Jinja for application layout and templating, I realize that in my last post, I neglect a clear and complete instructions of preparing local Python environment both for Windows and OSX users. I only focusing on Linux users (or, Ubuntu/Debian derived distro to  be precise). As I use those three OSes in a VirtualBox environment, I feel like I had to write it. Let's begin with the most irritating development environment to work with : Windows. Laughing



Preparing Python Environment for Windows Users

I will make this as a list, to display a clear and concise step by step way of doing it :

  1. Download Python 2.7.5 MSI installer. Run it.  If you feel like need to select other version (such as a 64bit version), go to its download page here.
  2. Upon installing, it will not add python directory to Windows path environment. Add it manually by opening System Properties (right click on My Computer icon on desktop, and choose Properties). Choose Advance System Settings => Environment variables => System variables => Path. Click edit, and append it to the path list. Also important to add besides C:\Python27, is the path C:\Python27\Scripts, as it's where setuptools and pip will be installed in the next step. You will use pip command often when working with a Python project.
    Path for Python27
  3. Install setuptools and pip, by downloading ez_setup.py and get-pip.py, and running them. In case you wondering how to run them, open command prompt/powershell/git bash prompt, and issue the command python ez_setup.py and python get-pip.py, of course with full path to where you download those two files. It will install setuptools and pip into the default C:\Python27\Scripts file.
  4. Install virtualenv by issuing command pip install virtualenv.
  5. DONE. Cool

Now, you are in the same starting position as your Linux fellow in wandering your way in the amazing Python environment. For a test, if you haven't done so, try to create a local working Python environment for our Openshift's bio application as explained in my previous article

Note that, if you use Git Bash Prompt, activating a virtualenv will be the same like if you work in *nix environment. That is, you can just issue the command source env/Scripts/activate. (note the different location of the activate scripts. In *nix environment it will be in env/bin/activate). But if you use plain Command Prompt, you must execute env/Scripts/activate.bat.

Preparing Python Environment for Mac OS X Users

I am using OSX Mountain Lion for these instructions, which means it may need adjustment for your OSX version. I haven't tested the newly launched Maverick for this!  

OSX embrace Python very well. In Mountain Lion, it already came with Python 2.7.2 found in /usr/bin/python. Many tools available for OSX user in case you want more control of your packages. For this, you may want to check homebrew. Using it, you can update your Python to 2.7.5 or later. In these instructions, I will stick with the included Python 2.7.2. 

  1. Install pip by opening Terminal, and issuing command sudo easy_install pip. This will install pip as system wide command, so you can issue it from anywhere within Terminal.
  2. Install virtualenv by issuing command sudo pip install virtualenv.
  3. DONE. Cool

Compare those instructions with the Windows counterpart. Haven't I told you that Windows is the most irritating development environment to work with? I am not kidding didn't I? Wink

Reusing Packages Already Downloaded for a Virtualenv instance

As I work simultaneously in OSX, Windows 7 and Linux using VirtualBox, I found that it will be useless if for each virtualenv for the same application, I re-download the packages I already download. But the solution is quite simple. First, you still have to create virtualenv for each instance of your python application. Activating it, but don't issue any pip install command yet. You just have to copy all files and directory from within env/lib/python2.7/site-packages/* to your other virtualenv instance. Now the same libraries will be available to all virtualenv instance and your application will run without changes.

Note that, for Windows the site-packages folder will be in  env/lib/site-packages/

UPDATE : Another thing to note, some package that depend on a certain Operating System may failed to run if you try to use it in different Operating System where it first installed. For example pyscopg2 package that I install in Linux will be useless if I try to use it in Windows. The workaround is to remove psycopg2 in Windows, and redownload it again. Letting pip redownload and rebuild it for your particular Operating System.

Leave comments

  • Glad that it helps you :)

    • eko
  • this is really help me preparing my current project using flask

    • rooco

Copyright(c) 2017 - PythonBlogs.com
By using this website, you signify your acceptance of Terms and Conditions and Privacy Policy
All rights reserved