wiki:VirtualEnv

Version 37 (modified by Darren Weber, 16 years ago) ( diff )

--

Installing Chimera and a Python Virtual Environment for Chimera Development

This page contains an example of a user installation of Chimera and additional configuration options to create a virtual python environment for development with Chimera. The Chimera distribution currently ships with a custom build of python2.7 (as of Aug, 2010). This custom python installation may not be compatible with any other installations of python on the system (including any site-packages in other python installations). The following examples provide a tidy way to access and add new packages in a virtual environment derived from the Chimera python installation. Also, see additional tips on development with eclipse for Chimera.

Installing Chimera

Get a stable release or a daily build of Chimera. In the examples below, it was a daily build for a linux 64-bit system (dated 2010-08-24). To run the installation, without superuser privileges:

cd ~/Downloads/
chmod +x chimera-alpha-linux_x86_64.exe
mkdir -p $HOME/src
./chimera-alpha-linux_x86_64.exe

Here's an example of the interactive prompts and replies:

UnZipSFX 5.41 of 16 April 2000, by Info-ZIP (Zip-Bugs@lists.wku.edu).
Original path: '/home/dweber/Downloads'
  inflating: chimera_install_35bPbx/installer  
  inflating: chimera_install_35bPbx/chimera.exe  

Enter install location: $HOME/src/Chimera64-daily 
Extracting files.  This may take a few minutes.
Executing command: './chimera.exe -q -d /home/dweber/src/Chimera64-daily'
UnZipSFX 5.52 of 28 February 2005, by Info-ZIP (http://www.info-zip.org).

Install desktop menu and icon? yes

To install desktop menu and icon, run:

    $HOME/src/Chimera64-daily/bin/xdg-setup install

If run as root, then it installs for all users,
otherwise it installs just for the current user.

Install symbolic link to chimera for command line use in which directory?
    0 -- no link
    1 -- /usr/local/sbin
    2 -- /usr/local/bin
    3 -- /usr/sbin
    4 -- /usr/bin
    5 -- /sbin
    6 -- /bin
    7 -- /usr/games
[hit Enter for default (0)]: 

This installation did not use sudo privileges, so the install path was changed from /opt/UCSF/Chimera64-2010-08-05 to $HOME/src/Chimera64-daily. To install the desktop menu and icons:

$HOME/src/Chimera64-daily/bin/xdg-setup install

Lastly, a few environment variables are set to simplify executing the new installation programs. If your installation path is different, you will need to set the CHIMERA environment variable to your path. The CHIMERA path must point to a hard-link, not a sym-link, in the directory tree. Warning: do not set these environment variables for daily work with Chimera, they are to be set for a shell session that is only required during installation of additional python packages into the Chimera distribution of python.

# For a daily build, use:
export CHIMERA=$($HOME/src/Chimera64-daily/bin/chimera --root)
# For an svn-build, use: 
#export CHIMERA=$($HOME/src/Chimera64-svn/bin/chimera --root)
export PATH=${CHIMERA}/bin/${PATH} 
export LD_LIBRARY_PATH=${CHIMERA}/lib

Adding python distribution tools to the Chimera installation

Now get the distribute or the setuptools package installed into the Chimera python installation. Note the following has some version specific details that need attention for any current installation. At the time of writing (Aug, 2010), this installation used a specific daily-build of Chimera (2010-08-24) and a specific version of setuptools for the version of python that was built for Chimera (python2.7).

Option A: to use distribute, try the following:

cd ~/Downloads
curl -O http://python-distribute.org/distribute_setup.py
${CHIMERA}/bin/python2.7 distribute_setup.py

Option B: to use setuptools, try the following:

cd ~/Downloads
curl -O http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg
sh setuptools-0.6c11-py2.7.egg --prefix=$CHIMERA

Now it's easy to use these tools for installation of any additional python packages into the Chimera python distribution. For example,

export CHIMERA=$($HOME/src/Chimera64-daily/bin/chimera --root)
export PATH=${CHIMERA}/bin/:${PATH} 
export LD_LIBRARY_PATH=${CHIMERA}/lib
${CHIMERA}/bin/easy_install pylint
${CHIMERA}/bin/easy_install pyflakes

Using virtualenv

Adding additional packages to the Chimera python distribution is now possible and a great way to do this, while preserving the state of the Chimera distribution, is to use virtualenv. First, add the virtualenv package into the Chimera python installation, like so:

$CHIMERA/bin/easy_install virtualenv
$CHIMERA/bin/virtualenv --help

Now that virtualenv is installed in the Chimera python distribution, we can setup any additional custom virtual environments to add python packages into our custom development environment. As a regular user, the following will create a Chimera-specific development environment in ~/src/chimera-virtualenv:

mkdir -p ~/src
cd ~/src
${CHIMERA}/bin/virtualenv --distribute ${HOME}/src/chimera-virtualenv
ls -al ${HOME}/src/chimera-virtualenv/

Once this virtual environment is created, it can be activated and deactivated with the following commands (note how the system prompt changes to indicate that we have entered and left the virtual environment).

source ~/src/chimera-virtualenv/bin/activate
deactivate

VirtualEnv Python Development for Chimera

Now you can start the Chimera build of python and import the chimera module. First activate the virtualenv and confirm that it's using the right python.

source $HOME/src/chimera-virtualenv/bin/activate
cd $HOME/src/chimera-virtualenv
export CHIMERA=$(cd $HOME/src/Chimera64-daily && pwd -P)
#export CHIMERA=$(cd $HOME/src/Chimera64-build && pwd -P)
export LD_LIBRARY_PATH=$CHIMERA/lib
cat > chimeraImportStartup.py <<END
import sys
import os
print os.getenv('CHIMERA')
sys.path.append(os.path.join(os.getenv('CHIMERA'), 'share'))
sys.path.append(os.path.join(os.getenv('CHIMERA'), 'lib'))
import chimera
print chimera.version.version
for s in sys.path:
    print s
END
export PYTHONSTARTUP=./chimeraImportStartup.py
python

An interactive terminal session might look like this:

$ source ~/src/chimera-virtualenv/bin/activate
(chimera-virtualenv)$ which python
/home/dweber/src/chimera-virtualenv/bin/python
(chimera-virtualenv)$ python
Python 2.7 (r27:82500, Aug 23 2010, 21:16:26) 
[GCC 4.2.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> import os
>>> os.getenv('CHIMERA')
'/home/dweber/src/Chimera64-daily'
>>> sys.path.append(os.path.join(os.getenv('CHIMERA'), 'share'))
>>> sys.path.append(os.path.join(os.getenv('CHIMERA'), 'lib'))
>>> import chimera
>>> chimera.version.version
'alpha version 1.5 (build 31253) 2010-08-24 01:28:28 GMT'
>>> for s in sys.path: print s
... 

/home/dweber/src/chimera-virtualenv/lib/python2.7/site-packages/distribute-0.6.10-py2.7.egg
/home/dweber/src/chimera-virtualenv/lib/python2.7/site-packages/pip-0.7.2-py2.7.egg
/home/dweber/src/chimera-virtualenv/lib/python27.zip
/home/dweber/src/chimera-virtualenv/lib/python2.7
/home/dweber/src/chimera-virtualenv/lib/python2.7/plat-linux2
/home/dweber/src/chimera-virtualenv/lib/python2.7/lib-tk
/home/dweber/src/chimera-virtualenv/lib/python2.7/lib-old
/home/dweber/src/chimera-virtualenv/lib/python2.7/lib-dynload
/home/dweber/src/Chimera64-daily/lib/python2.7
/home/dweber/src/Chimera64-daily/lib/python2.7/plat-linux2
/home/dweber/src/Chimera64-daily/lib/python2.7/lib-tk
/home/dweber/src/chimera-virtualenv/lib/python2.7/site-packages
/home/dweber/src/Chimera64-daily/lib/python2.7/site-packages/distribute-0.6.14-py2.7.egg
/home/dweber/src/Chimera64-daily/lib/python2.7/site-packages/virtualenv-1.4.9-py2.7.egg
/home/dweber/src/Chimera64-daily/lib/python2.7/site-packages
/home/dweber/src/Chimera64-daily/lib/python2.7/site-packages/PIL
/home/dweber/src/Chimera64-daily/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info
/home/dweber/src/Chimera64-daily/share
/home/dweber/src/Chimera64-daily/lib
>>> 
(chimera-virtualenv)$ deactivate 
$ 

To add additional packages into the virtual environment, try using pip.

$ source ~/src/chimera-virtualenv/bin/activate
(chimera-virtualenv)$ pip --help
Usage: pip COMMAND [OPTIONS]

Options:
  --version             show program's version number and exit
  -h, --help            Show help
  -E DIR, --environment=DIR
                        virtualenv environment to run pip in (either give the
                        interpreter or the environment base directory)
  -s, --enable-site-packages
                        Include site-packages in virtualenv if one is to be
                        created. Ignored if --environment is not used or the
                        virtualenv already exists.
  -v, --verbose         Give more output
  -q, --quiet           Give less output
  --log=FILENAME        Log file where a complete (maximum verbosity) record
                        will be kept
  --proxy=PROXY         Specify a proxy in the form
                        user:passwd@proxy.server:port. Note that the
                        user:password@ is optional and required only if you
                        are behind an authenticated proxy.  If you provide
                        user@proxy.server:port then you will be prompted for a
                        password.
  --timeout=SECONDS, --default-timeout=SECONDS
                        Set the socket timeout (default 15 seconds)

Commands available:
  bundle: Create pybundles (archives containing multiple packages)
  freeze: Output all currently installed packages (exact versions) to stdout
  help: Show available commands
  install: Install packages
  search: Search PyPI
  uninstall: Uninstall packages
  unzip: Unzip individual packages
  zip: Zip individual packages

Developing Chimera with spyder (a python IDE)

And now for something completely different! How about installing a spyder. First, we need to install some spyder dependencies, including sip and PyQt.

WARNING: This example is an unstable solution that may not work on other platforms. This example is based on an Ubuntu linux system that had packages installed for Qt4, !PyQt4, and QScintilla2, which are spyder dependencies. Your system may need these installed, along with their development headers. Although those packages were installed on the system, they were not directly accessible to the Chimera python build. In my case, a solution was to download and build !PyQt4 within the Chimera virtual environment. The order of the following installs is important (they are successive dependency resolutions).

source ~/src/chimera-virtualenv/bin/activate
## Use debian source packages instead of direct download
#curl -O http://www.riverbankcomputing.co.uk/static/Downloads/sip4/sip-4.10.5.tar.gz
#tar zxvf sip-4.10.5.tar.gz
#cd sip-4.10.5
mkdir -p ~/Downloads/deb-src
cd ~/Downloads/deb-src
apt-get source --download-only python-sip
dpkg-source -x sip4-qt3_4.10.1-0ubuntu1.dsc 
cd sip4-qt3-4.10.1/
python configure.py --help
python configure.py --debug --platform=linux-g++-64
make
make install 
deactivate
source ~/src/chimera-virtualenv/bin/activate
## Use debian source packages instead of direct download
#cd ~/Downloads
#curl -O http://www.riverbankcomputing.co.uk/static/Downloads/PyQt4/PyQt-x11-gpl-4.7.4.tar.gz
#tar zxvf PyQt-x11-gpl-4.7.4.tar.gz
#cd PyQt-x11-gpl-4.7.4
mkdir -p ~/Downloads/deb-src
cd ~/Downloads/deb-src
apt-get source --download-only python-qt4-dev
dpkg-source -x python-qt4_4.7.2-0ubuntu1.dsc 
cd python-qt4-4.7.2/
# configure without installing designer plugin or QScintilla API, as these
# should be installed already by a prior installation of Qt4 or PyQt into the
# system paths
python configure.py --debug --trace --qmake=/usr/bin/qmake-qt4 --no-qsci-api --no-designer-plugin
make
make install
deactivate
source ~/src/chimera-virtualenv/bin/activate
## Use debian source packages instead of direct download
#cd ~/Downloads
#curl -O http://212.219.56.133/sites/www.ibiblio.org/gentoo/distfiles/QScintilla-gpl-2.4.3.tar.gz
#tar zxvf QScintilla-gpl-2.4.3.tar.gz
#cd QScintilla-gpl-2.4.3/Python
mkdir -p ~/Downloads/deb-src
cd ~/Downloads/deb-src
apt-get source --download-only libqscintilla2-dev
dpkg-source -x qscintilla2_2.4.3-0ubuntu1.dsc 
cd qscintilla2-2.4.3/Python
python configure.py --debug --trace
make
make install
# Ignore the failure for: cp -f QScintilla2.api /usr/share/qt4/qsci/api/python/QScintilla2.api
# The QScintilla2.api file was installed by an Ubuntu package (don't overwrite it).
# This was the last step in the install, everything else installed OK.
deactivate

Let's check the additional module dependencies for spyder. The Chimera python installation includes modules for numpy and matplotlib. The install for pylint is easy, but scipy installation is not.

source ~/src/chimera-virtualenv/bin/activate
pip install pylint
pip install pyflakes
pip install rope
deactivate

That should satisfy most of the spyder dependencies. Now try to install spyder using pip.

source ~/src/chimera-virtualenv/bin/activate
pip install spyder
deactivate

OK, if that works, then try to setup the environment and run spyder.

source ~/src/chimera-virtualenv/bin/activate
#export CHIMERA=$(cd $HOME/src/Chimera64-daily && pwd -P)
export CHIMERA=$(cd $HOME/src/Chimera64-build && pwd -P)

# PROBLEM: without LD_LIBRARY_PATH, Chimera python2.7 cannot load _md5 module,
# but with LD_LIBRARY_PATH set the spyder will segfault (probably a
# conflict with the Chimera foreign build of some library).
export LD_LIBRARY_PATH=$CHIMERA/lib
#export LD_PRELOAD=libotf.so

export TCL_LIBRARY="$CHIMERA/lib/tcl8.6"
export TCLLIBPATH="{$CHIMERA/lib}"
unset TK_LIBRARY
unset TIX_LIBRARY
export PATH=$PATH:$CHIMERA/bin
unset PYTHONHOME
unset PYTHONPATH 
spyder

If that fails (as it did for me), try using sudo privileges (this worked for me):

sudo -i
source /home/dweber/src/chimera-virtualenv/bin/activate
export CHIMERA=$(cd /home/dweber/src/Chimera64-build && pwd -P)
#export CHIMERA=/opt/UCSF/Chimera64-2010-08-24
export PATH=$PATH:$CHIMERA/bin/ 
export LD_LIBRARY_PATH=$CHIMERA/lib
export LD_PRELOAD=libotf.so
export TCL_LIBRARY="$CHIMERA/lib/tcl8.6"
export TCLLIBPATH="{$CHIMERA/lib}"
unset TK_LIBRARY
unset TIX_LIBRARY
unset PYTHONHOME
unset PYTHONPATH 
spyder

TODO: launching spyder within Chimera

Eric and I tried a few things, including launching Chimera from within spyder and also launching spyder from within Chimera-IDLE using. For the latter option, we tried:

>>> import sys
>>> sys.path.append('/home/dweber/src/chimera-virtualenv/lib/python2.7/site-packages/')
>>> from spyderlib import spyder
>>> spyder.main()

Then within the spyder we tried:

>>> import sys
>>> import os
>>> sys.path.append(os.path.join(os.getenv('CHIMERA'), 'share'))
>>> sys.path.append(os.path.join(os.getenv('CHIMERA'), 'lib'))
>>> sys.path
>>> import chimera
>>> chimera.runCommand('open 1gcn')

This command appeared to run and provide status in the Chimera main window, but the display did not update to show the molecule. So, we ran into issues with process communication or something.

A possible solution may be to clone the IDLE extension from $CHIMERA/share/Idle, to replace Idle with spyder. Looking at the Chimera-IDLE extension, we may need to hook some kind of spyder-shell into the chimera.replyobj. The spyder does have a startup option for --light, which seems to provide a basic shell-like interface. Of course, that may be no better than IDLE, so that could be pointless. The goal is to get up all the IDE features of spyder.

Note: See TracWiki for help on using the wiki.