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!