How can I use pywin32 with a virtualenv without having to include the host environment's site-packages folder?

Question:

I’m working with PyInstaller under Python 2.6, which is only partially supported due to the mess MS have created with their manifest nonense which now affects Python since it is now MSVC8 compiled.

The problem is that the manifest embedding support relies on the pywin32 extensions in order to build which is a pain because without including the host’s site-packages folder when I create the virtualenv (kinda defeats the point in a build environment) I cannot find a way to install the required extensions so they are accessible to PyInstaller.

Has anyone found a solution to this issue?

Asked By: jkp

||

Answers:

OK, well since I had to find a way forward I improvised. I’ve internally created a git repository with a hacked-together version of pywin32 that will install within a virtualenv using the standard setup.py script. It took a lot of fiddling to make it work right but I managed to get it to load and the dependent code now works as I need it to. If people feel this would be of benefit to the community please post a comment: if I get enough I’ll try and put something up on my github account.

Answered By: jkp

I found http://old.nabble.com/Windows:-virtualenv-and-pywin32–td27658201.html (now a dead link) which offered the following solution:

  1. Browse http://sourceforge.net/projects/pywin32/files/ for the URL of the exe you want
  2. Activate your virtualenv
  3. Run easy_install http://PATH.TO/EXE/DOWNLOAD

This works with modern versions of setuptools (circa February 2014, reported by tovmeod in the comments).


If you are using an old version of setuptools (or distribute it merged back into setuptools), you may get this error message:

error: c:usersblahappdatalocaltempeasy_install-ibkzv7pywin32-214.win32-py2.6.exe is not a valid distutils Windows .exe

In which case:

  1. Download the exe yourself
  2. Activate your virtualenv
  3. Run easy_install DOWNLOADED_FILE.exe

I rather hopefully tried “pip install” rather than “easy_install”, but this didn’t work, and likely never will (citation needed).


Finally, I found but haven’t tested a solution at http://www.mail-archive.com/[email protected]/msg272040.html which is:

Solved this by copying the pywin32.pth file into my virtualenv site-packages
and editing the file to point to the path.

If the other options don’t work for you, maybe this will?

Answered By: lofidevops

This may have been improved since previous answer, since I’ve successfully installed pywin32 on sandbox on several machines without any specific “hacks” :

$ virtualenv sandbox
$ sandboxscriptsactivate
(sandbox) $ git clone https://github.com/Travis-Sun/pywin32.git
(sandbox) $ cd pywin32
(sandbox) $ python setup.py install

Tested with following environment :

Answered By: cthepenier

Edit: Scratch this for now, appears to be some problems with the installation still…

I got rather tired of the whole situation, and just created a set of converted wheels (“wheel convert <.exe>”). I’ll try and keep them maintained for the most recent build, but do shout if there are any issues.

https://tr00st.co.uk/python/wheel/pywin32/

Installation can be done easily using pip and pointing to the package matching your version and architecture. For example, for Python 3.5/amd64:

pip install https://tr00st.co.uk/python/wheel/pywin32/pywin32-219-cp35-none-win_amd64.whl

Caveat: The –upgrade process currently fails, as the uninstall procedure is unable to clean up after itself (Access Denied when cleaning up win32api.pyd) – this is only when removing the temporary directory, which can be manually deleted. Easiest way around this is to uninstall and reinstall instead of upgrading, then manually delete the temporary folder.

Answered By: tr00st

For Python 2.7 or 3.x use pypiwin32.

pip install pypiwin32