Tag: windows

Fix “import mercurial” problem in windows

Hi guys, it’s been a while since my last post, this time I want to share a tiny tip that saved me from a huge headache.

At work we’re planning on using Trac to manage our project (mainly as an issue tracker) along with Mercurial. Mercurial is great, all distributed version control systems rock, but when we chose mercurial it just seemed like the right decision due to it’s excellent performance and relative ease to integrate with Apache.

In any case, today I installed trac on our server computer, which unfortunately is a Windows machine. Unfortunate because setting things up on Windows is a bit more troublesome than on Linux or even Mac.

Once Trac was up and running along with our Mercurial server, both under Apache thanks to mod_wsgi, I installed the TracMercurial plugin so that I could setup our Mercurial repository in Trac. But even after adding the repo, I could not even browse the repo.

After reading a couple of posts, I found out that I had to check if the mercurial module could be imported in a normal python prompt. And so

>> import mercurial

… returned a module not found error.

Quick fix

Because I installed Selenic’s official windows binaries, the mercurial module is kept safe in the program files folder, where only the hg.exe binary can access it.

To fix this, I just copied the mercurial folder that was in c:\program files\mercurial\lib\mercurial into Python’s site-packages folder (C:\python26\lib\site-packages when using python 2.6).

And after restarting apache, I could see the “browse source” tab in Trac and all was good!

Note: I know, this is kind of BARBARIC, I could have added mercurial’s path into the PYTHON_PATH, but I just didn’t want to mix all those modules…

How to get XP Styles on wxPython apps made with py2exe

Let me begin with this: wxPython is fantastic! I could write a moderately simple app in ~17 hours which automates a task that is normally done manually loading data into a web form. The app consists on custom wizard classes, access to web resources (requesting and parsing) and session management with cookies. I’m impressed that I could finish the application, with no previous knowledge on any of the aforementioned subjects, in such a short time span.

Publicity aside, I faced a little problem. I packed the python application into an executable that didn’t require a full python install using the py2exe extension, after reading the guide on how to get py2exe to work with wxPython. My first problem was some silly issue with some MSCV*.dll files:

*** finding dlls needed ***
error: MSVCP90.dll: No such file or directory

After a quick read on several places, I found a simple solution for this problem, ignoring the dll file when building  the executables.

With that solved, I could successfully build and launch my application. The less obvious problem was, that even though the application launched it looked ugly! Here’s a screenshot on what I was getting when running the generated exe vs running the .py file directly:

normal_theme-vs-xp_theme

Hideous indeed. The problem was that for some reason the application was not using XP Themes when drawing the application’s widgets, and after some reading I discovered that there are some serious issues regarding Python2.6 (the version I’m using), wxPython and py2exe when it comes to manifests. Long story short, I had to associate a correct manifest to my exe file, and after lurking around my Python installs, I found a manifest that wxPython creates for Python that served my needs:

<?xml version=’1.0′ encoding=’UTF-8′ standalone=’yes’?>
<assembly xmlns=’urn:schemas-microsoft-com:asm.v1′ manifestVersion=’1.0′>
<trustInfo xmlns=”urn:schemas-microsoft-com:asm.v3″>
<security>
<requestedPrivileges>
<requestedExecutionLevel level=’asInvoker’ uiAccess=’false’ />
</requestedPrivileges>
</security>
</trustInfo>
<dependency>
<dependentAssembly>
<assemblyIdentity
type=’win32′
name=’Microsoft.VC90.CRT’
version=’9.0.21022.8′
processorArchitecture=’*’
publicKeyToken=’1fc8b3b9a1e18e3b’ />
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly>
<assemblyIdentity
type=”win32″
name=”Microsoft.Windows.Common-Controls”
version=”6.0.0.0″
processorArchitecture=”*”
publicKeyToken=”6595b64144ccf1df”
language=”*” />
</dependentAssembly>
</dependency>
</assembly>
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel level='asInvoker' uiAccess='false' />
      </requestedPrivileges>
    </security>
  </trustInfo>
  <dependency>
    <dependentAssembly>
      <assemblyIdentity
     type='win32'
     name='Microsoft.VC90.CRT'
     version='9.0.21022.8'
     processorArchitecture='*'
     publicKeyToken='1fc8b3b9a1e18e3b' />
    </dependentAssembly>
  </dependency>
  <dependency>
    <dependentAssembly>
      <assemblyIdentity
         type="win32"
         name="Microsoft.Windows.Common-Controls"
         version="6.0.0.0"
         processorArchitecture="*"
         publicKeyToken="6595b64144ccf1df"
         language="*" />
    </dependentAssembly>
  </dependency>
</assembly>

Both the “Microsoft.VC90.CRT” and “Microsoft.Windows.Common-Controls” assemblies are important. The first one is mandatory for Python >= 2.6 is built with Visual Studio 2008, and the second one is the one that links our app to the dlls that provide the nice XP Theme widgets.

Just loaded that text into a string (called manifest in my case) in my setup.py file and passed it as an  argument to the setup function like this:

setup(windows=[{
               'script':"app.py",
               'other_resources' : [(24, 1, manifest)]
               }],
        name = "My App",
        version="0.1"
    )

Easy as pie :D.

Hope you find this useful!

Configuring Apache + mod_wsgi + django… on Virtual Box serving files from Windows 7!

If you enjoy reading about weird software combinations let me tell you about my system configuration. I am currently using Windows 7 RC as my primary development OS (I quickly replaced my XP SP3) and I am finding it quite good to use (waiting for that new Fedora with the lovely Kde 4.2 to come out). In any case. I wanted to test how to set up Apache on a linux machine, but I didn’t want to go through the hassle of partitioning my HDD (not yet) so I decided to try Sun’s Virtual Box virtualization solution.

I got my Fedora 10 distro up and running in a jiffy, including the “Guest additions” that worked right out of the box (something that never really worked for me with VMWare). So, soon I started installing all the necessary packages for our project:

yum install Django

yum install python-psycopg2

yum install python-markdown

yum install  python-dateutil

yum install mod_wsgi

I did have to download and install the PIL library by hand because I didn’t find the appropiate package for fedora and I already knew I could downlaod the tar.gz file from their site and use the infamous setup.py install command to install it.

The thing is, I want to keep my development files in sync with the files that were going to be served by Apache, that is, find a way to mount some sort of virtual drive connecting the virtualzied Fedora with the Windows 7 host. Thanks to Virtual Box’s shared folders options this was a breeze!

So I set up a shared folder on vbox’s shared folders menu, I pointed that to the trunk dir of our project and named it “mgtrunk”.

I refuse to mount this folder by hand each time, so I modified my fstab file and added a line like this at the end:

mgtrunk /mnt/mg vboxsf defaults 0 0
mgtrunk /mnt/mg vboxsf defaults 0 0

Obviously I created the “mg” folder at the “mnt” directory previosly :).

A quick reboot and everything was good to go.

The next step was configuring mod_wsgi (which was automatically generated by the yum installed), so I went on and edited the wsgi.conf file created under /etc/httpd/conf.d/

$ vim /etc/httpd/conf.d/wsgi.conf

I edited it adding lines like the ones in mod_wsgi’s integration with django wiki page relating to the Apache config. And also created a django.wsgi file in my django project under the apache/ dir, just like the aformentioned page mentions.

With a lot of expectations I started the httpd service, and rushed to http://localhost/ on my browser… BOOM… Cannot load the page, followed by a pesky SELinux ballon hovering on the screen. What to do? Disable SELinux through it’s administrative interface, I have no time tonight to mess with yer configs (I did try… chcon’ing some files… but errors just kept on appearing from nowhere! So I just disabled SELinux).

And after another reboot I got better results, the page was loading, but unfortunately no images or css where loaded. Apache was not service correctly images no css files or other files, the only thing that worked was the python scripts. Quickly I changed my config to check if an unmodified apache config worked (I overwrote the / directory with my mod_wsgi after all) and images were displayed correctly, how odd! I even uninstalled the mod_python extension to check if it wasn’t causing any conflicts! No luck…

Man this did take me time!  As it turns out, Apache uses some sort of wicked kernel file transfer functionality to directly transfer files. This is how it reads on the httpd.conf file:

# EnableSendfile: Control whether the sendfile kernel support is

# used to deliver files (assuming that the OS supports it).

# The default is on; turn this off if you serve from NFS-mounted

# filesystems.  Please see

# http://httpd.apache.org/docs/2.2/mod/core.html#enablesendfile

Hmm… NFS… Network File System…. I even wonder if the vboxsf supports this sendfile functionality, maybe it doesn’t. So all it took me for the files to display correctly was DISABLING the darn sendfile directive. I added a line on my wsgi.conf file, just before the Directory tag for the media folder, that reads like this:

EnableSendfile off

A final service httpd restart…. and… yay! The site’s working correctly, at last!

Quite an adventure… Now on I can go to try to develop a super scalable WSGI application to interface with my Django site like Eric Florenzano’s Writing Blazing Fast, Infinitely Scalable, Pure-WSGI Utilities blog article.