How to setup Mercurial 1.8 HgWeb Repository hosted on IIS 7.5 with Windows Server 2008 R2 (x86 | x64 bit) and Python 2.6 or Python 2.7 with ISAPI or CGI
After reading existing tutorials Setting up Mercurial server in IIS7 using ISAPI-WSGI and
Setting up a Mercurial server on IIS7 via Fast CGI then receiving a number of Python errors including Failed to import callback module ‘hgwebdir_wsgi’ I decided to author this updated guide to setting up Mercurial 1.8 on IIS 7.5 (x86 or x64) using Python 2.6 or Python 2.7.
There are a number of problems with the existing instructions due to changes in Mercurial 1.7+. Unfortunately all other existing instructions fail under Mercurial 1.7.2+ as well they force you to install 32 bit versions of Mercurial / Python even on 64 bit systems – this is no longer an issue, so enjoy true 64 bit goodness!
This guide will show you how to successfully setup Mercurial 1.7 / 1.8 (either 64 bit or 32 bit versions) HgWeb hosted repositories on IIS7 and will allow you to use either Python 2.6 or the current version, Python 2.7.2 – look no further – this guide works
Please donate your mouse button to support this information
How To Install and Setup Mercurial 1.8 on IIS 7
-
How to Install Python (64 bit or 32 bit)
-
Install PyWin32
-
Mercurial IIS 7 Installation
-
Configure IIS 7 as Repository Host
-
IIS ISAPI Configuration
-
IIS Fast CGI Configuration
-
How to Setup Python on Windows Server 2008 R2
- Choose a Python version (2.6 or 2.7) and architecture (32bit or 64bit)
- It’s very important to be consistent when choosing a Python version (2.6 or 2.7) & architecture (x86 or x64).
- I suggest using Python 2.7.2+
Note: Python 3.X is NOT supported by Mercurial - If you are on a 64 bit version of Windows then I suggest you take advantage of x64 bit. Otherwise you will have to choose 32 bit.
- Download Python Installer
- Run Python Installer
- Accept the default installation path of C:\Python2X
- Add Python to Environment Path
- Follow this step to ensure python.exe can run without it’s full path c:\python2X\Python.exe
- Bring up System Window (Right Click My Computer -> Properties or Control Panel – >System and Security -> System)
- Click on Advanced System Settings -> then Environment Variables in popup window
- Under system variables -> Click to edit ‘Path’
- Append the Python path to existing values such as ;c:\python26\; or ;c:\python27\; – note values are separated with a ; (semicolon).
- Test the python executable is in the environment path by opening a DOS prompt and typing ‘Python’ then CTR-Z to quit out of Python.
Install Python for Windows Extensions (PyWin32)
- Choose a PyWin32 Build (Build216 has been tested and verified)
- It’s very important to be consistent choosing the PyWin32 version matching your Python version & architecture (x86 or x64).
- For example if you installed the 64 bit version of Python 2.7 then you need to download the PyWin32 64 bit installer for Python 2.7
- Download PyWin32 Installer (Pay attention to match step 1)
- PyWin32 for Python 2.6
- 64 Bit – Download Pywin32 64bit MSI Installer
- 32 Bit – Download PyWin32 x86 MSI Installer
- PyWin32 for Python 2.7
- 64 Bit – Download Pywin32 64bit MSI Installer
- 32 Bit – Download PyWin32 x86 MSI Installer
- PyWin32 for Python 2.6
- Run PyWin32 Installer
- This will install into the \Lib\site-packages\ directory
Install & Build Mercurial Source Code
- Download the Mercurial 1.8.4 Source Code Release
- Don’t waste time monkeying around with the installers. You need a version of Mercurial matching your Python install so get the source code!
- You will need to unzip the pain in the ass .tar.gz compressed source code file. This is easy on windows using 7zip
- You should now have opened the source code using 7Zip and copied to a folder such as c:\mercurial-source-1.8.4\
- Build Mercurial Source Code
- This is done as per the “pure install” instructions
- Open a dos window and browse to the source folder (c:\mercurial-source-1.8.4\ in this example)
- Run the following commands:
python.exe setup.py --pure build_py -c -d . build_ext -i build_mo --forcepython.exe setup.py --pure install --force- This will build and install Mercurial into the Python install folder eg. C:\Python27\Lib\site-packages\mercurial
- This is done as per the “pure install” instructions
- You now have a working Mercurial install – however you will want the hg command to work from anywhere. Currently you have to call the hg command directly via
c:\Python2X\Scripts\hg. This can be fixed a few ways.- Add the c:\python2x\scripts\ path to the environment variable similar to the python steps above
- Copy the c:\python2x\scripts\hg.bat file into the c:\python2X\ folder which is already added to windows Path var – then update the hg.bat file as follows:
-
@echo off
rem Windows Driver script for Mercurial
setlocal
set HG=%~f0
rem Use a full path to Python (relative to this script) as the standard Python
rem install does not put python.exe on the PATH...
rem %~dp0 is the directory of this script"%~dp0python" "%~dp0\scripts\hg" %*
endlocal
- You can test this works by typing hg from any directory in dos and you will see the hg instructions
Setup Mercurial HgWeb Host on IIS 7
- Choose an IIS7 plugin technology
- ISAPI is the fastest and considered a superior option
- CGI is an older and equally as capable option
- If you are unsure use the ISAPI option
ISAPI Configuration Instructions for Mercurial HgWeb
- Download the ISAPI WSGI Handler Source Code
- There is a known installer issue where setup fails missing MSVCR71.dll so you have to build the source.
- (work around provided where you download the egg is not as flexible and could cause issue when using the latest version of Python 2.X)
- Unzip the source code .zip file into a temporary downloads directory Eg. C:\ISAPI-204-Source\
- Compile the ISAPI source code from DOS by running the following command from the ISAPI source directory:
- python setup.py install
- isapi_wsgi.py will be installed in the ‘site-packages’ directory of your Python installation. Eg. C:\Python27\Lib\site-packages\
- Download the hgwebdir_wsgi.py file from the Mercural source. This can be found in the contrib\win32 directory
- isapi_wsgi.py is a Python script that is configured to compile into the _hgwebdir_wsgi.dll file
-
HgWeb ISAPI Configuration
- Create the file folder that will be hosting your Mercurial IIS7 repository website. Eg. c:\inetpub\hg\
- Copy hgwebdir_wsgi.py file to the website folder.
- Open hgwebdir_wsgi.py for editing by right clicking it and using the Python editor (notepad will not open the file properly due to the invalid new line characters)
- You should see a pretty version of file with colors from the IDLE or Pythonwin editors
- Set the hgweb config value to the website folder.
hgweb_config = r'c:\inetpub\hg\hgweb.config' - Unless you are using a virtual application/directory set the path prefix to zero then save/close the file.
path_prefix = 0 - Create a new hgweb.config file in the website folder with the following content
[paths]
/ = c:\path-to-desired-repository-folder-location\* - Open a DOS prompt and set the directory to your website folder. Eg. c:\inetpub\hg\
- Execute the following to compile a DLL called _hgwebdir_wsgi.dll
python hgwebdir_wsgi.py - Create a new application pool in IIS Manager called “Mercurial-Repo”
- Set the .net framework version to “No Managed Code”
- *Only if* the installation is 32 bit then change the “Enable 32-bit Applications” to true.
- Create a new website in IIS Manager pointing it to the website folder. Be sure to select the Mercurial-Repo application pool.
- Open up the Handler Mappings for your new web site.
- Add a new “Wildcard Script Map” with the Executable location pointing to the _hgwebdir_wsgi.dll in the website folder. Name it Mercurial-ISAPI
- Click the OK button, and click YES to allow the ISAPI extension.
- Assuming you have followed all directions carefully browsing to the newly created website should serve the empty repositories listing!
This has been a comprehensive approach to configuring Mercurial on IIS 7 and Windows Server 2008 via building the source code directly to ensure the version of Mercurial setup matches the version of Python as well the system architecture. This is the only tutorial I know of that allows the 64 bit version of Mecurial 1.8+ to be setup.
Please post a comment if you have *any* issues with following these directions. I have invested time and care into testing this process and keeping these instructions current. Thank you.

17 responses to Hosting Mercurial Repo IIS7 Windows Server 2008R2 x64 Python ISAPI CGI
I’ve followed your instructions to more or less the letter. The main difference is that I’m running with the latest version of Mercurial (1.9).
When I finally have completed your tutorial, and want to run the hg web page, then I’m getting a 500 error.
OK I am going through the tutorial again myself to check it using Mercurial 1.9. Could you please check your Application Log and find out what the message is related to the 500 error?
*Edit* – After testing Mercurial 1.9 setup on IIS7 with Python 2.7 I did get receive a
as noted below. I must test further to get this figured out. For now perhaps you want to proceed with Mercurial 1.8?
Also what version of Python are you using? 2.6 or 2.7?
Thank you,
Rob
I used Python 2.6.6, should have mentioned it. Also I took the x64 approach. Meaning all of the installed requirements including mercurial were x64.
I’m getting the same issue trying to install Mercurial 1.9.2, but following all other instructions to the letter, via the x64 path:
Failed to import callback module ‘hgwebdir_wsgi’
The specified module could not be found.
I’m not comfortable going with this setup if I can’t upgrade to the latest Mercurial. Do you have any idea what’s going wrong?
Thanks for the guide,
I’ve installed the mercurial once again with x64. This time using:
1.8.4 version of Mercurial
2.7.2 version of Python
Now I do not get an http error response, rather
“Mercurial repository not found. The specified repository “hg” is unknown, sorry. Please go back to the main repository list page.”
Mercurial is running as an application in the IIS under the default website.
At first as I mentioned, I set it up as an application on the default IIS site, where the url was subdomain.domain.no/hg
I reckon it went wrong because the setting implied that the mercurial repositories directory would be on the top level.
Next I tried running mercurial as it’s own site, that went far better, then I got results implying that I could setup repositores. However, then I had an issue with getting the hg command to work. Meaning typing ‘hg init test’ would not do anything other than provide an error implying the hg command wasn’t valid.
Yes this error can be fixed by setting
path_prefix = 1as mentioned in HgWeb ISAPI ConfigurationThis error is due to hg not being in your windows path environment. Instructions have been added above (step 3 of Mercurial setup)
Now your Mercurial server should be up and running
Excellent guide, worked straight away for me after trying a couple other guides/methods which failed with Python import errors.
Only bit that wasn’t on there explicitly was ensuring that ISAPI was enabled on IIS as it’s not by default.
Saved me loads of time, thank you so much.
As the previous person, Idyll, I originally tried walking through the guide using Mercurial 1.9 & Python 2.7 w/ the end result a being a (HTTP Error 500.0 – Internal Server Error).
As you stated it will require more testing and troubleshooting to determine what’s going on there.
In the meantime, if I end up using Mercurial 1.8.x for the repository server, I was wondering if you know if that would be compatible with clients running Mercurial 1.9 (using TortoiseHg GUI)?
Thanks for taking the time to piece together this article and all the additional assistance!
Hi, can you help me with ‘Couldn’t import standard bz2 (incomplete Python install).’ when trying to run ‘python.exe setup.py –pure build_py -c -d . build_ext -i …’
My environment is win server 2008 X64, Python 2.7.2
Thank you
This has to be a Python installation issue. I have done the install on that same configuration without that problem. First of all are you certain you have installed the 64 bit versions? I suggest you start over and be sure you get 64 bit version of Python and Mercurial. Let me know if you need further assistance.
For the ApplicationPool it is necessary to set Pipelinemode to classic.
Otherwise I got server error 500.
Hello,
thank you for the instructions. Found your link on StackOverflow while looking for a solution to the Python error using Jeremy Skinner’s tutorial.
Have you found a way to get it working with Mercurial 1.9 ?
Thank you very much for this guide!
1. did it on windows 2008 R2 Server, 64bit.
first attempt with mercurial 1.9.2 failed, got the 500 as reported and investigated by you above.
2. Taking your advice I switched too Mercurial 1.8.4 direct from the links here and had it running minutes later.
ad 2: One strange thing to a python newbie: uninstalling python packages? I brutally removed the files from the pythons sub folder site-packages folder than removed 2 other files found in the python directory by searching for “mercurial” and installed hg 1.8.4.
ad 1: Actually while testing 1.9.2 I had something wrong: my old Mercurial 1.8 that came with tortoise hg was available via hg, since the path was directing to the old version. *Maybe* this affected the outcome. It might make sense to either remove tortoise before executing this guide or at least to check “hg –version” to be sure that the correct mercurial is under configuration.
I’m also getting a server error 500 when trying this with Mercurial 1.9.2. Tried it again with 1.8.4 and it worked.
I had some problems with security as well though, the easiest way to solve it was to run the application pool as a new user that I made an administrator.
It’s nice, but not working solution (Server 2008 R2 x64). I used HG 1.9.2 & Python 2.7.2 (x64). During building HG I got:
package init file ‘mercurial\pure\__init__.py’ not found (or not a regular file)
Well, by some advice I created that rubbish and anyway stop at error:
running build_py
running build_ext
running build_mo
warning: hgbuildmo: could not find msgfmt executable, no translations will be built
So WTF is that ‘msgfmt’ and why nobody tested building HG?
(
I think the bottom line here is that none of us have been able to get Mecurial HG 1.9.x running. I will do some more testing when I have time. My wife is due with a new baby tomorrow!
I will refresh this article soon. In the mean time I guess we have to suck up and use Mecurial 1.8 cause it works ‘out of box’ when following this post.
Thank you
Hi,
As all of you i have a problem using Mercurial HG 1.9.x
I realised that there is a problem setting up security permissions over this folder (HG source code)…
maybe it can help…
Leave a reply to Hosting Mercurial Repo IIS7 Windows Server 2008R2 x64 Python ISAPI CGI