Setting up Python in Windows 7

An all-wise journalist once told me that “everything is easier in Linux,” and after working with it for a few years I’d have to agree — especially when it comes to software setup for data journalism. But …

Many newsroom types spend the day in Windows without the option of Ubuntu or another Linux OS. I’ve been planning some training around Python soon, so I compiled this quick setup guide as a reference. I hope you find it helpful.

Set up Python

Get started:

1. Visit the official Python download page and grab the Windows installer. Choose the 32-bit version. A 64-bit version is available, but there are compatibility issues with some modules you may want to install later. (Thanks to commenters for pointing this out.)

Note: Python currently exists in two versions, the older 2.x series and newer 3.x series (for a discussion of the differences, see this). This tutorial focuses on the 2.x series.

2. Run the installer and accept all the default settings, including the “C:\Python27″ directory it creates.


3. Next, set the system’s PATH variable to include directories that include Python components and packages we’ll add later. To do this:

  • Right-click Computer and select Properties.
  • In the dialog box, select Advanced  System Settings.
  • In the next dialog, select Environment Variables.
  • In the User Variables section, edit the PATH statement to include this:

 

C:\Python27;C:\Python27\Lib\site-packages\;C:\Python27\Scripts\;

4. Now, you can open a command prompt (Start Menu|Accessories or Start Menu|Run|cmd) and type:

 

C:\> python

That will load the Python interpreter:

 

Python 2.7.3  (default, Apr 10 2012, 14:24) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or license for more information.
>>>

Because of the settings you included in your PATH variable, you can now run this interpreter — and, more important, a script — from any directory on your system.

Press Control-Z to exit the interpreter and get back to a C: prompt.

Set up useful Python packages

1. setuptools offers the helpful easy_install utility for installing Python packages. Grab the appropriate version for your system and install.

2. pip is another package installer that improves on setuptools. Having pip and setuptools will cover most of your installation needs, so go ahead and add pip. Now that you’ve installed setuptools, you can add pip by typing the following at any Windows command prompt (not in the Python interpreter):

 

C:\> easy_install pip

Notice that easy_install executes without needing to be told where on the system it’s located. That’s the benefit of adjusting your PATH variable earlier.

3. Mechanize and BeautifulSoup are must-have utilities for web scraping, and we’ll add those next:

 

C:\> pip install mechanize
C:\> pip install beautifulsoup4

4. csvkit, which I recently covered here, is a great tool for dealing with comma-delimited text files. Add it:

 

C:\> pip install csvkit

You’re now set to get started using and learning Python under Windows 7. If you’re looking for a handy guide, start with the Official Python tutorial.

76 comments on “Setting up Python in Windows 7” »

  1. Any other useful utilities to add?

  2. You probably ought to tell ‘em specifically to download the older, 2.7 version, as 3.1 is listed as well.

    Looks awfully useful, though — thanks!

  3. Duly noted, Mike. Thanks!

  4. Thanks Anthony! As a long time Mac/Linux user, this tutorial was incredibly helpful in setting me up with a Python environment for my fresh Windows7 install.

  5. Glad it was helpful! A bunch of people are getting ready for some Django training at work this week, and someone passed this link around:

    http://docs.python-guide.org/en/latest/starting/install/win/

    Some good tips in there too.

  6. My python compiler is showing message:
    Traceback (most recent call last):
    File “F:\Python\latex2wp.py”, line 657, in
    s=extractbody(s)
    File “F:\Python\latex2wp.py”, line 140, in extractbody
    for i in range(1,(len(L)+1)/2) :
    TypeError: ‘float’ object cannot be interpreted as an integer
    >>>

    Please help

  7. Sumit,

    It seems that your code is attempting to load an object of type “float” into an integer. I’m guessing that’s due to the division operation occurring in the range method.

    You may want to explore StackOverflow or scan the Python docs for handing type casting.

  8. I recommend installing 32 bit, unless you need 64 bit. No, you probably don’t need 64 bit.

  9. Another way to install and run Python code on Windows is to download and install the Zeus IDE, create a new Python file and then use the run menu to run the script. And here is a video to see for yourself – http://www.youtube.com/watch?v=9vchdcLFW54

  10. Joshua: Thanks. You’re right, and I’ve updated the post to point people to 32-bit.

  11. Or you can use npackd to install and optionally upgrade if need ever arises.

  12. For some reason the python set-up universe eludes me. For instance I was trying to run easy_install in python. Once I realized that the emphasis was cmd prompt, not python prompt it fell in place. A note to that effect might really help the uninitiated.

  13. Ken: Thanks; that’s a good point about differentiating between the Windows cmd prompt and the prompt inside the Python interpreter. I added some words to emphasize that.

  14. I installed python on my computer, I have windows 7, but when i write python on the command prompt it displays the following message: ‘python’ is not recoghnizes as an internal or external command, operable program or batch file. How can I fix this issue?
    Thanks

  15. Marion, you need to follow Step 3 in my tutorial above.

  16. I’m getting this error when installing mechanize module in python 3.2
    ——-
    C:\>pip install mechanize
    Downloading/unpacking mechanize
    Downloading mechanize-0.2.5.tar.gz (383Kb): 383Kb downloaded
    Running setup.py egg_info for package mechanize

    Installing collected packages: mechanize
    Running setup.py install for mechanize
    File “C:\Python32\Lib\site-packages\mechanize\_beautifulsoup.py”, line 267

    raise AttributeError, “‘%s’ object has no attribute ‘%s’” % (self.__clas
    s__.__name__, attr)
    ^
    SyntaxError: invalid syntax

    … snip …

    Successfully installed mechanize
    Cleaning up…
    ——-
    it finished saying “successfully installed mechanize”. When importing mechanize in a script it triggers an error, any clue?

  17. Henry,

    You’re getting the error because Mechanize does not support Python 3.x at this time.
    I’ve edited your comment for readability.

    See http://wwwsearch.sourceforge.net/mechanize/faq.html

  18. Hi Anthony,

    I have tried to install python 2.7 on my windows7 following the steps but it failed and the following message pops up. An error occurred during the installation of assembly ‘ Microsoft.VC90.CRT, version = ‘9.0.21022.8’ publickeytoken=’1fc8b3b9a1e183b”,processorArchitecture=’x86”,type=win32”

    I noticed that I have the 2.5 version and I assume this is while I was installing ARCGIS on my pc. I wouldn’t mind using the 2.5 version the only problem is I can’t get the langague interpreter run as per your steps…I get the following message ‘python’ is not recoghnizes as an internal or external command, operable program or batch file.Any insight??

  19. Meklit,

    Sounds like your system may have had a problem with a prior Windows install. A little Googling turned up this: http://support.microsoft.com/kb/970652

    That said, if you want to use the Python 2.5 install that ESRI includes with ArcView, you should be able to by modifying the system PATH I mention in Step 3 above to point to the required Python components.

    The only issue is that, at least on my system, I see that ESRI adds a directory between the Python folder and the components. So, for me, I’d have to set the PATH to:

    C:\Python26\ARCGIS10.0;C:\Python26\ARCGIS10.0\Lib\site-packages\;C:\Python27\ARCGIS10.0\Scripts\;

  20. hi ,
    i trying to learn Python by self.I amusing widows -7 32 bit machine ,i am facing some problem while installing it.Can anyone please gove a proper link and proper setup which will exactly sit on my machine properly. I will be reallt greatful. Thank you.

  21. shubhasmita,

    Without more details I don’t think I can help.

  22. I just wanted to add a note to let you know that I am new to the whole idea of programming and am trying to take a class that required python. I can’t tell you how helpful this little tutorial was. Thank you so much for taking the time to put this up here.

    sincerely,
    Dusti
    To get something you have never had,
    you must do something you have never done.

  23. Dusti,

    Really glad it helped!

  24. Hi Anthony,
    I installed python 2.7.3 (python-2.7.3.msi for windows) on my windows 7 machine. However, I could not run python scripts from Python GUI IDLE. I am getting an error “There’s an error in your program: invalid syntax” each time I try to run my script. I tried to run the same script from python command prompt, it worked. What could be the possible reason of this issue? As I am not very comfortable working on command line, it would be a great help if you could offer a solution. Thanks.

  25. Lubaba,

    I never use IDLE so I don’t have a lot to offer for troubleshooting. I found this from a quick Google search:

    http://stackoverflow.com/questions/6513967/running-python-script-from-idle-on-windows-7-64-bit

  26. i downloaded python and changed the path variable to C:\Python27;C:\Python27\Lib\site-packages\;C:\Python27\Scripts\;
    and i go on cmd prompt and type C:\> python and i get this
    ‘C:\’ is not recognized as an internal or external command, operable program or hatch file
    plz help!
    thanks

  27. Matthew,

    Just type “python”. In the example, the “C:\” represents the prompt itself.

  28. ok thanks you are a life saver

  29. Hi Anthony

    I installed python 2.7.3 in my box running windows 7 32 bits. Everything was fine until I wanted to print from my program, Then I got : “ImportError: no module named win32print”.

    This is what I get about my path:

    >>> import sys
    >>>> print sys.path
    ['', 'C:\\Python27\\Proyectos\\%PYTHONPATH%', 'C:\\My_python_lib', 'C:\\Windows\
    \system32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python
    27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\
    lib\\site-packages']
    

    please any suggestion/advice.

    thanks in advance

  30. Dacio,

    Haven’t run into that one. Perhaps this helps?
    http://www.blog.pythonlibrary.org/2010/02/14/python-windows-and-printers/

  31. Anthony

    Thank you very much this was a very easy to follow and very useful tutorial. You have saved hours of web browsing.

  32. I’ve followed your direction installing Python 2.7.3 32 bit on Win7 64 bit HP. Python starts in command prompt but IDLE will not start from Start/All Programs/python27. Any ideas?

  33. h-man,

    Hmm. That’s one of those situations that’s difficult to diagnose because it’s probably related to something specific on your machine. I do see that others have run into the problem — this thread discusses a firewall as being an issue: http://bit.ly/S8xRRQ

  34. Do you have a recommendation for a text editor? It would be useful to have a color coded script…

  35. Jeff,

    I particularly like Sublime Text 2, which you can try for free. Buying a license allows for installation on multiple machines.

    A lot of coders swear by Vim, which has its own paradigm for editing and requires some time to learn. I’ve used it — and it’s a must if you’re going to work on remote servers — but I still like Sublime better.

  36. I have set up the path to C:\Python27\Lib\site-packages\;C:\Python27\Scripts
    and I cant run the scrip by > python full\path\to\myscript.py command on window 7
    so what shall i do
    thnaks

  37. John,

    What error message do you receive?

  38. Hi Anthony,

    Thank you very much for the clear and to-the-point tutorial :-) It has saved me a lot of searching and headache.

  39. This was SO SO SO SO SO SO helpful!!!! thank you so much!

  40. Iris, csbellina,

    You’re both very welcome. It’s been rewarding to be able to help people. Ironically, I recently moved from Windows to OS X for my daily work — and found myself frantically searching for good Python setup guides!

  41. Here is a very important one for ArcGIS desktop.
    Besides setting your PATH environment variable, add a new variable “PYTHONPATH” if it is missing, and set/add “;%ArcGISHome%\bin”.
    In my case, it looks like: PYTHONPATH=C:\ArcGIS\bin
    when it type >set PYTHONPATH in a cmd window.

    For some reason this was missing when I installed ArcGIS desktop 9.3.1 in Windows 7.

  42. I’ve been searching for a tutorial that in a really concrete and easy way describes the process to install python and pip. This is just what I was searching for! Thanks man!

  43. Dear Anthony

    I have struggling to load the interpreter – section 4 – system throws up an error message

    windows cannot find c:\> python directory

  44. Windmill,

    Make sure you’re only typing “python” at the C: prompt and not “C:\> python”.

  45. Thank you Anthony it was very helpful for windows refugee(me)

  46. Excellent article mate !! Got me up and running with python in no time . Keep up the good work.

  47. i already installed python 2.7.3 on my system(window 7)
    i can run simple python code
    but,i face a problem when i want to run script that contain import beautifulsoup
    do i need to install all those setuptools,pip and mechanize to make its working??
    i have googled for the solution for days about how to install the beautifulsoup unto my python,but,still i couldn’t find any right solution..
    i keep uninstalling if it does not working
    could u please help me how can i make it work on my system??

  48. Thanks a lot for the article!

  49. wani,

    I am confident that if you install setuptools and then pip, you can install Beautiful Soup with pip as I mention above. The Mechanize library is not a prerequisite for Beautiful Soup.

  50. tq so much anthony…so helpful!!

  51. hello, i installed python 3.3 please and it seem this article talks abt 2.7 only..any help regarding python 3.3 as i am a novice in programming..i have installed the python 3.3 and when i type “python” python at start, the only programs i see are python(command line) and IDLE(python GUI). I can’t find anything like python interpreter and i don’t know how to go about writing and running the python codes. The little amount of python that i learnt was learnt online and the interpreter was online too, so now, i want to start soding offline. Help please

  52. tunde,

    You should set your PATH variable according to the directory where Python 3.3 was installed. It would probably be something like:

    C:\Python33;C:\Python33\Lib\site-packages\;C:\Python33\Scripts\;

    Then, you should be able to type “python” at a command prompt to launch the interpreter. Beyond that, the various Python libraries I mention here may or may not be available under 3.3 — you’ll have to do some research.

  53. I am having trouble with python, django, heroku install.

    ive run:

    python winservice_install

    because djcelery has to be run as a web service on windows 7 (64 bit)

    but when i run: python manage.py winservice_install

    it tells me it can’t find win32api

    pprint(sys.path) includes

    D:\\apps\\python\\lib\\site-packages\\win32′,
    D:\\apps\\python\\lib\\site-packages\\win32\\lib’,

    and one of these directories has: win32api.pyd is that what it wants?

    I’m a beginner with python, heroku, and django: it’s been an entire day and I would like to know how to have python find win32api

    thanks!

  54. Jeff,

    Sorry, but I don’t readily have the answer to your question. But I do have a broader suggestion:

    If you’re going to be developing a Django app, I’d strongly suggest you do so on Linux or Mac OS X. Is it possible to develop a Django app on Windows 7? Sure. But I’ve found over time that installing Python libraries and getting everything working well is much more complicated compared with running Python under Linux.

    You don’t need to have a separate computer, either. Go install VirtualBox (for free) and download Ubuntu (for free). Guaranteed to make life simpler in the long run.

    https://www.virtualbox.org/wiki/Downloads

  55. YOU SIR ARE A HERO! Thank you so much I have been trying to figure this out as a newbie for a long time and you explain things step by step in easy to follow instructions for us newbies.

  56. Installation of “pip” for Windows users (I am on Win 7 (x64):

    1. cd to the directory where the pip Win installer downloaded from https://pypi.python.org/pypi/pip was downloaded to.

    2. In a Administrator’s command prompt do the following
    > easy_install .
    Note:there is a full-stop after the word easy_install

    Now you can run commands like
    >pip install mechanize

    Have fun! :)

  57. Thank you Anthony.

    Nice work!

    :)

  58. Thank you for this whole article. Basically, I am using python for over 6 months on windows and I always used those python extension installers from unofficial sites. Sometimes I couldnt even find some important extension as an installer. It felt really limited….
    And today after I found your article and did all the steps, the whole process felt like a breeze..
    No nerdy compilations, nothing.. Again, thanks…
    I am actually looking at the date of the post and thinking how some basic posts can help others for a long time ever after. And btw, now I’ll be hunting your site for other python-related articles..
    You just earned yourself a loyal reader…
    Again, thanks from the bottom of my heart..

  59. Thank you very much for this excellent article. I have been using Python in Linux for quite some time and wanted to try it on Windows. You article made my work so easy. I haven’t found such a detailed and perfect installation guide ever. Thanks Again.

  60. Making a path environment variable to the folder containing Python.exe is easy. What you do not explain is how to make Python locate a Python script from a path setting. For instance I have this Python script in the path C:\Program Files\pyurlsnooper\pyurlsnooper-gtk.py, but I have to change into the pyurlsnooper directory to run the script. Adding a Windows path to C:\Program Files\pyurlsnooper does not make the script accessible to Python anywhere other than from it’s installation folder. I tried adding this path to the registry at HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.7\PythonPath but still Python cannot locate the script anywhere except from it’s installation directory. Could there be a problem with the space in ‘Program Files’?

    Also what is the point of making WIndows paths to C:\Python27\Lib\site-packages and C:\Python27\Scripts if these folders do not contain Windows executables?

  61. Maurice,

    The reason I add the \site-packages\ and \Scripts\ subdirectories to my PATH is because I occasionally find that certain Python libraries seem to need that reference to function. For example, the csvkit library I recommend in this post installs Windows .exe files in the \Scripts\ subdirectory.

    For the other issue you mention, these might help:
    http://docs.python.org/2/install/#inst-search-path
    http://stackoverflow.com/questions/3701646/how-to-add-to-the-pythonpath-in-windows-7

  62. So, I installed Python 2.7 into Win 7 and added the variable as outlined in step 3. Does it matter what the variable is named? I have tried Python and python, but when I go to a command prompt, I get the not recognized as a program…etc message. What am i doing wrong?

  63. I posted a bit ago, looks like it was removed. Anyway, I figured out why it wasn’t working. the Script directory isn’t created automatically. Presumably, it does after the setuptools gets installed, but if one if following step by step, the Scripts directory must be manually created for step 3 to actually work.

  64. Rich,

    It’s true that the Scripts directory is not created until SetupTools is installed, but that shouldn’t prevent you from executing python from the command line. One thing you must do after Step 3 is quit and restart your command terminal for the new path to be recognized.

  65. Hi Anthony,
    I’m using Python 2.7.2 version / pyodbc 32 bits installed in a Windows 2003 server, which also has IIS server configured for sending mails. I’m trying to connect through my Excel to Teradata and refresh the graphs / charts / Table. For the same I have written macros to refresh the table / charts .Through Python I’m retying to open the Excel get a snapshot and send it an email

    I’m getting the following error
    import win32com.client as win32;
    File “c:\Python27\lib\site-packages\win32com\__init__.py”, line 5, in

    import win32api, sys, os
    ImportError: DLL load failed: %1 is not a valid Win32 application.

    When I search for solution it looks like I have to install 64 bit Python. But I’m worried whether my other setups will get disturbed. Is there a way that I can open the Excel / Macro using 32 bit?

    Your help is much appreciated. Thanks in advance

  66. Hmm. I was restarting the command shell, but as soon as I created the scripts directory, it worked, even before installing the setup tools. Anyway, besides that, I got it up and running. Thank you much!

  67. Raj: Unfortunately, I don’t have enough experience to offer a meaningful answer.

  68. Many thanks for this. It made the whole installation process much easier.

  69. This is awesome!!!! Thanks so much for putting this together.

    Much love

    Ned

  70. hi. i have a problem . i dont know which version i shoulde use for windows 7. please help me . i really want to know python but i’m a begginer .i only know visual basic. help me. which version?? thanks

  71. mahdiye,

    If you’re just getting started, I’d suggest Python 2.7.5, which is the latest version in the 2.x series. Python 3 is rapidly gaining traction, but there are still a number of helpful libraries that aren’t yet ported to version 3 yet. You also can make the transition later to 3 if your needs change.

  72. Anthony,
    thanks Anthony . i downloaded it . i really want to learn python then i downloaded some flv files for learn. the name is : CBT Nuggets – Python Programming Video Tutorials
    i watched some of these videos then i started to writting codes but i had some problems .i followed the codes that i learned but i saw errors like syntax error etc.
    look at this :

    Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32
    Type "copyright", "credits" or "license()" for more information.
    >>> def gensq(n):
    	for i in range(n):
    		yield i**2
    
    >>> a=gensq(4)
    >>> a
    
    >>> type(a)
    
    >>> next(a)
    0
    >>> next(a)
    1
    >>> next(a)
    4
    >>> next(a)
    9
    >>> next(a)
    
    Traceback (most recent call last):
      File "", line 1, in 
        next(a)
    StopIteration
    >>> def itsq(n):
    	result=[]
    	for i in range (n):
    		result.append(i**2)
    		return result
    
    >>> b = itsq (4)
    >>> b

    ………………………………
    i know they are ridiculous its because i dont know anything about python and just followed the codes.
    and i have a problem. i cant find graphycal user interface of python.
    …………………………………
    sorry my english is not so good . i’m a iranian 16 girl ^^
    . help me :(

  73. mahdiye,

    There are some good books here that might help you get started:
    http://pythonbooks.revolunet.com/

Trackbacks

    Fresh from #nicar12, here are curated notes to set up a Windows 7 Python environment so I can practice at work | Madison.com Labs Blog

    [...] following are bullet points gathered from walkthroughs created by Anthony DeBarros and the Kenneth Reitz’s Python Guide to get a Python development environment up and running [...]


    Designer Learns to Code: Pt. 1 (Struggles) | The Nostalgia Machine

    [...] wish I read this quick and clear guide on how to set up Python in Windows 7 before I started diving through Python’s [...]


    LoopTools: Flask – Microframework : quadloops technologies

    [...] version, as flask has not yet been ported to python 3. Here is a nice and simple guide to do that, here  ( Thanks to Anthony DeBarros for the guide. You can follow [...]




Comments? Questions?