Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Install pysvn in a virtualenv

I can install pysvn site-wide using the binary package. For example, in Ubuntu:

$ sudo apt-get install python-svn

Or, on Windows, I can install site-wide using the .exe installer.

Outside of a virtualenv, I can do this

$ python -c "import pysvn; print 'ok'"
ok

Now I make a virtualenv (I use the mkvirtualenv command from the virtualenvwrapper package)

$ mkvirtualenv test1

But since virtualenv defaults to not importing global site packages, I can not use pysvn inside this virtualenv.

(test1)$ python -c "import pysvn; print 'ok'"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named pysvn

How do I access pysvn in a virtualenv without enabling global site packages?

like image 523
Christian Long Avatar asked Sep 22 '14 22:09

Christian Long


1 Answers

There are a number of ways to handle this.

Option 0

Allow access to the global site packages from within the virtualenv. Pass the --system-site-packages option to virtualenv when creating the virtual environment.

Or, use the toggleglobalsitepackages command (from virtualenvwrapper) to allow access to global site packages.

(test1)$ toggleglobalsitepackages
Enabled global site-packages
(test1)$ python -c "import pysvn; print 'ok'"
ok

(test1)$ toggleglobalsitepackages
Disabled global site-packages
(test1)$ python -c "import pysvn; print 'ok'"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named pysvn

Option 1

Use easy_install to install the package in to the virtualenv using a binary installer. For example, on Windows the process might look like this:

  1. Download the binary installer file. In this example, let's call it example_installer.msi (or example_installer.exe)
  2. Activate the virtualenv (I use virtualenvwrapper-win on Windows)
  3. easy_install example_installer.msi

Verify that you can install the installer site-wide, by double-clicking and running the installer in gui mode (then uninstal using the Windows Add/Remove Programs control panel). If you can install it site-wide, then easy_install can probably install it in to a virtualenv.

However, the pysvn binary installer is not structured properly for easy_install. If you try this with the Windows pysvn binary installer you get this error:

error: py27-pysvn-svn185-1.7.9-1572.exe is not a valid distutils Windows .exe

Option 2

Use the add2virtualenv command from virtualenvwrapper. This adds a .pth file to the virtualenv's site-packages directory, which gives the virtualenv access to the specified directories.

Note that you must specify the parent directory, instead of the specific package. That is, instead of

add2virtualenv /usr/lib/python2.7/dist-packages/pysvn

It should be

add2virtualenv /usr/lib/python2.7/dist-packages

See this question: add2virtualenv (virtualenv wrapper) does not work with scipy

To find the directory where a package is installed, do this:

$ python
>>> import pysvn
>>> pysvn.__file__
'/usr/lib/python2.7/dist-packages/pysvn/__init__.pyc'

The problem is, this includes all the packages in the specified directory, not just pysvn. So, it has the same drawback as toggleglobalsitepackages.

Option 3

Symlink the install directory in to the virtualenv's site-packages.

A convenient way to get to the virtualenv's site-packages directory is to use virtualenvwrapper's cdsitepackages command

cdsitepackages
ln -s /usr/lib/python2.7/dist-packages/pysvn pysvn

Summary

On Windows, try Option 1 (easy_install from binary installer). If that fails, install globally and allow the virtualenv to access it by using virtualenvwrapper-win's toggleglobalsitepackages command, or by passing the --system-site-packages option to virtualenv.

On systems that support symlinking, such as Linux and OS X, use Option 3. It allows you to access the specific packages you need without allowing access to the whole global site packages.

like image 112
Christian Long Avatar answered Sep 24 '22 20:09

Christian Long