Tips on ActivePython, PyWin32 and virtualenv
by Sridhar Ratnakumar

Sridhar Ratnakumar, December 14, 2010

ActivePythonWhen you create a pure Python sandbox using virtualenv --no-site-packages the global site-packages directory is rightfully not included ... however, this poses a challenge in accessing non-trivial packages from within the virtualenv. While packages such as PyQt4 can be reinstalled into the virtualenv using pypm -E C:\myvenv install pyqt4, that is not true for PyWin32 which is included with the ActivePython distribution itself.

C:\> virtualenv --no-site-packages C:\myenv
C:\> C:\myenv\Scripts\python.exe
>>> import win32api
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named win32api

Fortunately, there is a simple workaround to get around this problem.

C:\> copy C:\Python27\Lib\site-packages\pywin32.pth C:\myenv\Lib\site-packages\
C:\> more C:\myenv\Lib\site-packages\pywin32.pth
# .pth file for the PyWin32 extensions

Now simply edit this file (in your text editor, eg: IDLE) to contain the absolute paths:

# .pth file for the PyWin32 extensions

That's it -- you can now import pywin32 from the newly created virtualenv and still exclude the packages in your global site-packages directory.

>>> import win32api

All of this can be automated by using Fabric and fablib - which also copies other pywin32 packages, such as win32com.

Get started using ActivePython or visit PyPM Index!

Subscribe to ActiveState Blogs by Email

Share this post:

Category: python
About the Author: RSS

Sridhar Ratnakumar is a senior Python developer at ActiveState where he works tirelessly to ensure that ActivePython is the world's highest quality Python distribution. He's been developing on Python since 2003 and is the author of various Python modules, including pyrtm and applib. He earned his software development stripes at Amazon and Microsoft.


1 comments for Tips on ActivePython, PyWin32 and virtualenv

This method works well for win32api, but doesn't give you win32com.