# 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.

Oct 15, 2011 | Programming, Python

### 120 Responses to “Setting up Python in Windows 7”

1. Anthony says:

Any other useful utilities to add?

2. Mike Stucka says:

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. Anthony says:

Duly noted, Mike. Thanks!

4. Ian says:

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. Anthony says:

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. [...] 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 [...]

7. Sumit Kumar Jha says:

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
>>>

8. Anthony says:

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.

9. Joshua Grigonis says:

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

10. Jussi Jumppanen says:

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

11. Anthony says:

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

12. Tom says:

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

13. Ken Pierce says:

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.

14. Anthony says:

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.

15. Marion says:

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

16. Anthony says:

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

17. Henry says:

I’m getting this error when installing mechanize module in python 3.2
——-
C:\>pip install mechanize
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?

18. Anthony says:

Henry,

You’re getting the error because Mechanize does not support Python 3.x at this time.

19. Meklit says:

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??

20. Anthony says:

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\;

21. shubhasmita sahani says:

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.

22. Anthony says:

shubhasmita,

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

23. Dusti says:

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.

24. Anthony says:

Dusti,

Really glad it helped!

25. Lubaba says:

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.

26. Anthony says:

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

27. Matthew says:

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

28. Anthony says:

Matthew,

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

29. Matthew says:

ok thanks you are a life saver

30. Dacio says:

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']


31. Anthony says:

Dacio,

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

32. Karl says:

Anthony

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

33. h-man says:

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?

34. Anthony says:

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

35. Jeff says:

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

36. Anthony says:

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.

37. John says:

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

38. Anthony says:

John,

What error message do you receive?

39. Iris says:

Hi Anthony,

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

40. csbellina says:

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

41. Anthony says:

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!

42. dran001 says:

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.

43. Alejandro says:

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!

44. windmill says:

Dear Anthony

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

windows cannot find c:\> python directory

45. Anthony says:

Windmill,

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

46. Sandepp says:

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

47. Andrew says:

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

48. wani says:

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??

49. Igor says:

Thanks a lot for the article!

50. Anthony says:

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.