Python development on Windows [Part 1]:Installing Python

 

Welcome to the first installment of "Python development on Windows". In this series I am merely trying to document the steps I constantly perform to get python things going nicely on windows whenever I have to use it.
 

Install Python

Of course before you can setup a decent development environment, you have to install the python runtime. It's an exciting time in python land right now, we have a brand spanking new Python 3, however, a lot of the libraries you may wish to use may not have been ported yet. Python 2.6 might work for you but the last time I tried it, some libs weren't ready for it. This will obviously change over time. For the purpose of this guide we'll use the latest version of the 2.5 release. At this time it is 2.5.4

All releases can be found here.

 

2.5.4 direct link.

After it's done downloading, double click to run the installer.

Selecting the default options should be fine in most situations. Unles you tell the installer otherwise it will install everything in c:\Python2.5  . After the installer completes, you can run a little test to make sure everything is ok. Open up a Dos Prompt/Cmd window or whatever it's called now.  And type "python" at the prompt.

[figure 1]

As you can see, you are blessed with a friendly error message alerting you to the fact that windows does not know what this "python" is. NO MATTER, we will fix this in the next step by setting an environment variable, and adding it to the path. 

PYTHON_HOME environment variable

Out of habit left over from the days where I had to install a bunch of java stuff on windows, I create a SOMETHING_HOME variable for various things i need for software development and then add the variables to the system path. The advantage of using variables is you can change stuff easily without breaking your path. For example, if I suddenly wanted to switch to Python3.0 I would merely change the variable and expect everything to work as I had configured it. You may find that adopting a similar convention yourself, reduces number of ways you can confuse yourself when you are busy version hopping.

A quick note on environment variables... In DOS, or whatever they call it now, you refer to an environment variable by surrounding it with '%" . to try this out, in the cmd window you have open from the previous step, type "echo %PATH%" hit enter and note what you see...

[figure 2] 

Voila, you should see a ";" delimited set of paths that make up your systems PATH variable. In case you don't know, when you type a command(or double click an icon) in the event that the command does not include the absolute path(or relative path), windows will search the paths in your PATH variable to find the command to run. Right now, the directory where you installed Python is likely not in your path which is why Windows responded with "...is not recognized as an internal or external command..." We will soon fix this for good.

If you've never setup an Environment variable before, you are in for a treat(not really). There are several ways to do it, but for our purposes, going through the GUI is easiest. Whenever you want to manage your environment variables, you can do so by navigating to System Properties-->Advanced Tab-->Environment Variables.(right click "My Computer " select "properties", "Advanced Tab" "Environment Variables" Button).

[figure 3]

Note that you can see variables that are user specific, and variables that are system wide. For simplicity's sake, we'll add to the system variables by selecting the "New" button and adding the variable.

[figure 4]

After clicking "OK" to dismiss the "New System Variable" window and "OK" to dismiss the "Environment Variables" window, you would think you could type "echo %PYTHON_HOME%" and actually get the path you set in your currently open command window. YOU WOULD BE WRONG. From what I can tell the environment is read when the command window starts up. So in order to see what you just effected you need to open up a new one. Do that now and type "echo %PYTHON_HOME%"

Your system should respond by spitting the path you specified back at you. To insure the path is valid you could type "cd %PYTHON_HOME%" and your system should drop you in the c:\python2.5 directory, or whatever directory you setup in your environment variable.

[figure5]

Adding PYTHON_HOME to path

So now you have a PYTHON_HOME environment variable. big deal right? if ou type "python" in a command window, it still won't launch python. That is because we still have to add PYTHON_HOME to the path. When we do that, Windows will be able to locate python and run it.  

PATH is an environment variable that windows uses  as mentioned before, so as you might guess you can change this variable in the same screens as you did to create the PYTHON_HOME variable.  REMEMBER that there should be a ";" between each path.

[figure 6]

After adding %PYTHON_HOME% preceded by a ";" to your path variable,dismissing the "Edit System Variable",window and dismissing the "Environment Variables"  open up another cmd window and type "python"

[figure 7] 

As you can see Windows response is now dramatically different(for the better), it launched the python interpreter. And then I wrote a little helloworld program. To exit the interpreter type CTRL+z.

OK, so what have we accomplished? We installed Python for windows, setup an Environment variable called "PYTHON_HOME"  whose value was the path where we installed python, and then added the variable to our system path.  Just so windows knows that when you type "python" it knows to launch the python.exe found in your Path somewhere.

In Part 2 we will setup easy_install which is where the real fun begins.

 

Stay Tuned!!!

Adding Python to Path on Windows 7 error

Hello, I have just followed the guide for adding the path to be able to use python from the command line. I don't understand why is it not working. I have python 2.6 and I'm running Windows 7. I added C:\Python26 the path as explained in this guide. When I open a command prompt I type python and it doesn't work. I have to 'cd' my way to C:\Python26 and that is the only place where typing python works. Now I want to continue programming a project I have. I have everything in a folder called ver3. Inside that folder is my 'src folder' and inside srs I have a file called main.py and two more folders where the rest of the classes are located, f1 and f2. I put the complete 'ver3' folder in the C:\Python26 directory. But from the command line when I am in the src folder to run the program typing 'python main.py', I get the error "python is not recognized as an internal or external...etc" IN fact as mentioned before the only place where typing 'python' works is when I cd to C:\Python26. How can I fix this? Is there something else I can do besides what is told in this guide to fix my problem, I don't want to need to put all the files scattered all over inside C:\Python26 to be able to run the program. I would really appreciate any information to help me! thanks.

try this in the command

try this in the command prompt. echo %PATH% and look at the output, if you don't see c:\Python26, then c:\Python26 is not on your path according to windows. It works fine on Windows 7 I set somebody up this weekend without issue. I was kind of impressed. :)

Thanks for sharing this

Thanks for sharing this article on Python. I am using python but not used like every features that you have listed. It was very nice. Looking for more

Thank you

Gotcha. It's working now =D

A lifesaver

I really needed to use easy_install on my Windows box, and this tutorial helped a good bit. The environmental variable tweaks were nice as well.

ActivePython

ActivePython automatically adds both C:\Python26 and C:\Python26\Scripts to your PATH environment variable. It also adds %APPDATA%\Python\Scripts (for use with the PyPM installer) though that requires you to log-off and login again. Further, if you have multiple versions of ActivePython installed (say, 2.6 and 2.7), you could type either "python27" or "python26" to get to the right interpreter (similar to the way it works on *nix). These are all unfortunately lacking in the python.org installer. Python 2.6 might work for you but the last time I tried it, some libs weren't ready for it. What libs were they? I doubt that there are any popular modules (other than Google App Engine) that are not ported to 2.6 yet.

Great point

ActivePython has been a lifesaver for me on the craptastic redhat boxes they give me at work. Without activestate I'd be stuck on 2.4 . Thanks for the info.

Thanks for informative article

Thanks for sharing this article on Python. I am using python but not used like every features that you have listed. It was very nice. Looking for more..................Please continue.

it worked - thanks!

thanks for the post, it helped me setup python26 on my new system (win7)

Thanks, but the las part

Thanks, but the las part don't work for me in windows 7.

edit: I solve the problem, i

edit: I solve the problem, i was adding the python_home in user variable not in system variables.

Well I'm glad to hear it

Well I'm glad to hear it works on Windows 7. I haven't even tried it yet. If you get a chance, let me know how well virtualenv works. I've heard there were some problems with Vista.

Hint

Nice post, to make it even more easier to get to the user variable context menu try windowskey + pause. Always hated it going there using the GUI :) I am a little python geek myself and if you look for some pyhton resources or scripts you may want to check out http://normankosmal.com/ :) so long, raz

Development environment

Good Job, thank you.

Portable Python

I remember seeing a PortablePython somewhere so the installation is not needed. Does somebody knows about this? Thx

You can actually roll your

You can actually roll your own. I did one on a 4gb usb drive, it took up <200MB. Perhaps I should blog that at some point.

portable python

http://www.portablepython.com/

after hours and hours of searching, i found it

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><p> <h1><h2><h3><h4><h5><h6> <img>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • E-Mail addresses are hidden with reCAPTCHA Mailhide.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.