Using pvpython and virtualenv

January 24, 2018

If you have ever naively tried to build ParaView with Python wrapping enabled against a virtual environment you have created, and you have been disappointed with the results, then this post is for you.  For example, maybe your custom application depends on ParaView Python wrapping as well as a specific version of Numpy.  You may have thought it would be nice to create a virtual environment into which you could install Numpy, along with whatever other Python modules you like, then build ParaView against that environment, and like magic, the pvpython you built would allow you to import those modules.

So maybe it is fairly obvious at this point, but that approach does not work.  However, there is at least one way to use ParaView and it’s Python interpreter, pvpython, together with a custom virtual environment.  As long as you build ParaView against the same Python used to create that virtual environment, there’s a simple trick to be able to use all the modules from both worlds.

Let’s start by creating a virtual environment (read more about Virtualenv here) and installing some modules:

cd /home/projects/CoolProject
virtualenv coolVenv
source coolVenv/bin/activate
pip install numpy requests
deactivate

At this point, you can build ParaView with Python wrapping enabled, just be sure that the Python you build against is the same one where the virtualenv module was installed.

Now, let’s write a small Python script to be run by the pvpython interpreter, where you also want to use those modules from your virtual environment:

from paraview.simple import *
import numpy as np

mySource = Cone()
...

That obviously isn’t going to work when you run it with pvpython because the numpy module will not be found. But the Virtualenv documentation has a solution for us. Just edit that script as follows:

activate_this = '/home/projects/CoolProject/coolVenv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

from paraview.simple import *
import numpy as np

mySource = Cone()
...

And that’s it! Any modules you have installed in your virtual environment will now be available. And by the way, this same trick works for vtkpython as well. Enjoy!

 

 

5 comments to Using pvpython and virtualenv

  1. now i am getting error that paraview.simple is not found. Can i add this to my virtual environment as well?

    1. Hi Hugo,

      I haven’t tried doing this with a python from conda. One of the requirements of the trick with virtual environments, however, was that you had to build ParaView against the installation of Python used to create the virtual environment. Furthermore, I don’t think this trick will work as posted anymore, as last time I checked, Python3 virtual environments do not contain “bin/activate_this.py”.

      1. Hi Scott, thanks for the reply.
        Feels like building a proper module that I can import in the PV interpreter will be harder than I thought…
        Also, would you know how to install the paraview librairy in another python interpreter ?
        Hugo

  2. Hi,
    Thanks for this tip, very useful !

    For Python3 (3.8.0), I had to change a little bit :


    activate_this="$HOME/.local/env-python/pvpython-3.8.0/bin/activate_this.py"
    with open(activate_this) as f:
    code = compile(f.read(), activate_this, 'exec')
    exec(code, dict(file=activate_this))
    #EndWith

Leave a Reply