This is a test version of Biostars. For the public version, visit https://www.biostars.org.
ImportError: No module named pymol (Windows)

Hi. I found this script online:

"""
Pymol python script to add a function to color atoms by residues.
syntaxe in Pymol :
color_by_resname selection
"""

__author__      = "Thibault Tubiana"
__copyright__   = "Copyleft"

color_code = {
  'ALA' : 'lemon', 
  'CYS' : 'yellow', 
  'ASP' : 'red', 
  'GLU' : 'violet2', 
  'PHE' : 'emerald', 
  'GLY' : 'orange', 
  'HIS' : 'peacock', 
  'ILE' : 'lime2', 
  'LYS' : 'indigo', 
  'LEU' : 'grass', 
  'MET' : 'green', 
  'ASN' : 'purple2', 
  'PRO' : 'tangerine', 
  'GLN' : 'magenta2', 
  'ARG' : 'blue', 
  'SER' : 'scarlet', 
  'THR' : 'vermillion', 
  'VAL' : 'lemon-lime', 
  'TRP' : 'cyan', 
  'TYR' : 'turquoise',
}

def create_color():
    """
    Create missing color in pymol
    """
    cmd.set_color("scarlet",[1.0,0.2,0.0])
    cmd.set_color("vermillion",[1.0,0.4,0.0])
    cmd.set_color("tangerine",[1.0,0.8,0.0])
    cmd.set_color("lemon",[0.8,1.0,0.0])
    cmd.set_color("lemon-lime",[0.6,1.0,0.0])
    cmd.set_color("lime2",[0.4,1.0,0.0])
    cmd.set_color("grass",[0.2,1.0,0.0])
    cmd.set_color("emerald",[0.0,1.0,0.4])
    cmd.set_color("turquoise",[0.0,1.0,0.8])
    cmd.set_color("peacock",[0.0,0.4,1.0])
    cmd.set_color("indigo",[0.4,0.0,1.0])
    cmd.set_color("purple2",[0.8,0.0,1.0])
    cmd.set_color("magenta2",[1.0,0.0,0.8])
    cmd.set_color("violet2",[1.0,0.0,0.4])



def color_by_resname(selection="all"):
    """
    this function aims to color residues. The color of each residues is from
    this article : 
    Taylor, W. R. (1997). Residual colours: a proposal for aminochromography.
    Protein Engineering, 10(7), 743-6.
    usage on pymol : 
    color_by_resname selection.
    > eg: color by resname on chain A.
      color_by_resname chain A
    > eg: color by resname on chain A and only on ALA and LYS.
      color_by_resname chain A and resname ALA+LYS
    > eg: color all by resname
      color_by_resname all
    """
    create_color()
    for res in color_code:
        sel = selection + " and resname %s" % res
        cmd.color(color_code[res], sel)

cmd.extend("color_by_resname",color_by_resname)

and I am trying to add to it. I want to make my script fetch the proteins instead of me having to use the fetch command in the PyMOL console. How can I import PyMOL propertly into Python so I can use the fetch command. Every time I try to import it, I get this error : ImportError: No module named pymol. I am a beginner with Python and I do not really know how to change any paths, so a simple explanation to this problem would be very helpful!

Thanks in advance!

software error pymol scripting coding python

1 answer

You first need to install the module, see for example this page: http://www.pymol.org/install

I already downloaded PyMOL from the website and have the PyMOL Console and GUI on my computer. Do I need to reinstall them?

I don't work on Windows, but do you have multiple versions of Python installed perhaps?

Nope. I just tried pymol.cmd.fetch and it works. Thanks though!

Log in to answer this question.