This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Running Pymol From Python Script

Hi all, I would like to know is it possible to run Pymol from Pythoin script ? I tried this script (to produce png file from pdb file)

#!/usr/bin/python
import __main__
__main__.pymol_argv = [ 'pymol', '-qc']
import pymol
pymol.finish_launching()
pdb_file ="my.pdb"
pdb_name ="my_pdb"
pymol.cmd.load(pdb_file, pdb_name)
pymol.cmd.disable("all")
pymol.cmd.enable(pdb_name)
pymol.cmd.png("my_pdb.png")

And run it as "python script.py" but no png file is produced and no error !!! Did I got something wrong about Pymol?? any suggestion !!!

python pymol structure

3 answers

The script below worked perfectly in my machine. I installed pymol via MacPorts, so it's a completely brand new installation without any modifications or tweaks. The script outputted one line about the ray tracing process and the image, with the protein, is sitting in the directory I ran the script in.

Do you have pymol in our PYTHONPATH? Can you go to a Python shell and import pymol? Also, which version of Pymol do you have and which OS are you using?

import __main__
__main__.pymol_argv = [ 'pymol', '-qc'] # Quiet and no GUI

import sys, time, os
import pymol

pymol.finish_launching()

##
# Read User Input
spath = os.path.abspath(sys.argv[1])
sname = spath.split('/')[-1].split('.')[0]

# Load Structures

pymol.cmd.load(spath, sname)
pymol.cmd.disable("all")
pymol.cmd.enable(sname)
pymol.cmd.png("my_image.png")

# Get out!
pymol.cmd.quit()

If he didn't have pymol in his PYTHONPATH, wouldn't he get an error?

Yes, but sometimes package managers install different versions of the software in 'random' places.. it's good to know what exactly is being called.

I guess pymol provided by Linux package manager have some bug or feature missing. Removing old and installed new Pymol from http://sourceforge.net/projects/pymol/ and Now it works. Thanks @João Rodrigues and @Niek De Klein

Read this post:

In the end, don’t forget to close with pymol.cmd.quit()

Adding

pymol.cmd.quit()

to the end of your script should probably work.

Besides, the -q option means that the output of Pymol is silenced, thus the reason you might not see any error.

Try Joao's suggestion, remove the q. Maybe it does give errors.

This might be a stupid question, but have you checked if the file is not being saved somewhere else? Also, remove the "disable"/"enable" part and see if it works.

I did both but still no PNG :( if someone have a working script which makes PNG from a PDB please post

I encountered this problem on Ubuntu 12.04 running pymol verson 1.4.1 (the version installed with apt-get) . I removed that version and reinstalled pymol 1.6.0.0 and was able to get it to work.

Log in to answer this question.