Installing Python 2.7, pip, mysql-python on Windows 7

This will guide you through the process of installing Python 2.7 on Windows 7, something I had to do recently that proved more annoying that it should have been.  By putting this on the searchable internet, it might prove useful to someone else.

Install Python 2.7

Download Python 2.7 (I prefer 2.7 over 3.x because of fewer dependency issues, but if you’re coding in your own environment, give 3.4 a try).

Once it is downloaded, you generally have to go to Control Panel > System > Advanced System Settings > Advanced Tab > Environment Variables > Path > Edit.  Here you need to add the Windows path of your Python installation. As an example, mine is “C:\Python27”.  If you don’t add this to the Path variable, you won’t be able to run Python from the command line and it’ll be annoying to test things. Also all cool programmers use the command line.

And if you need it, here are some Windows command line shortcuts:

  • cd ——- Change Directory
  • dir ——- list all contents of this directory
  • start <program> <file> —— useful for starting text editors (i.e. “start notepad++ myprogram.py”)

More can be found here.

Install pip

Next, we should install pip, the Python package manager (some of this section is from experience some from this stackoverflow page). In case you were wondering, Python 3.4 already has pip installed with it!  But if you don’t have it there are a few options.  If you have easy_install on your machine, you can simply navigate to the Python27\Scripts directory and type:

easy_install.exe pip

This should install pip for you.  If you don’t have easy_install, you can download a python script from here.  Make sure to save that get-pip.py file as a .py file not .txt.  You can then run the script:

 python get-pip.py

But on your Windows machine, sometimes the automatic installers just won’t work.  If that happens, your back up is the repository made by Christoph Gohlke.   Make sure you install both pip and setuptools.

Once pip is installed, you can add the directory where pip is to your path like we did before (probably something like “C:\Python27\Scripts”).  Then you should be able to type:

 pip install mysql-python

Be warned though, if you have Strawberry Perl on your computer because you were messing around with Perl earlier, your command line will use Strawberry Perl’s pip command instead of the Python package manager. Either use the pip.exe file directly (located in Python27\Scripts) or get rid of Strawberry Perl.

Install mysql-python/MySQLdb

First, try:

pip install mysql-python

You may get the dreaded Unable to find vcvarsall.bat either when installing pip or other packages (I got it from trying to install mysql-python package). This means Windows can’t find a C/C++ compiler (it’s looking for Visual Studio) which pip uses to install packages and install itself. Stack Overflow recommends installing MinGW which I have tried. I still was never able to get mysql-python to use the gcc compiler I installed with MinGW.  I kept getting errors such as “gcc file not found” and “gcc.exe exited with error code 1”.  Eventually I gave up and used Christoph Gohlke’s site.  I would advise you do the same.

You can check if mysql-python is correctly installed by going to the command prompt and typing:

C:\> python
>>>import MySQLdb

Make sure you capitalize MySQLdb or it won’t import. If you don’t get an error you’ve succeeded! And there you should have Python 2.7, pip, and any other package you need (including mysql-python) on a Windows 7 machine.  I’m sure a lot of this would be similar on a Windows 8 machine, but I would warm users that I have not tried it yet. Hope this helps anyone who has been having issues with it.