How can I use PyMOL API from Python?

1.9k Views Asked by At

I have PyMOL already installed on my Linux machine. I know it is installed because when I write pymol -cp pymol_api_test.py the script executes.

I want to run the following python script using python3:

import pymol
from pymol import cmd

print(cmd.align("/home/bbq_spatial/bbq_input_pdb/pdb1a6j.pdb",  
                "/home/bbq_spatial/bbq_output_pdb/pdb1a6j.pdb", 
                cycles=0, transform=0))

However, it doesn't run when I call it using python3:

user_name@server_name:~$ nano pymol_api_test.py
user_name@server_name:~$ python3 pymol_api_test.py
Traceback (most recent call last):
  File "pymol_api_test.py", line 1, in <module>
    import pymol
ModuleNotFoundError: No module named 'pymol'
user_name@server_name:~$

How can I resolve this?

2

There are 2 best solutions below

6
Pouya Esmaeili On

Install conda (or pip) as a package manger for Python. And then install pymol with the following commands:

conda install -c conda-forge -c schrodinger pymol-bundle

You can check your installed packages in your env with the following command:

conda list

Use this link to install conda on your Linux machine.

Pay attention,PyMol may require other dependencies on you system, check the official guides for installation.

UPDATE:

Check a specific pkg:

with pip: pip show <pkg>

with conda: conda show <pkg>

List all installed pkgs:

with pip: pip list

with conda: conda list

0
FlaFlam On

Make sure that your python interpreter has access to the pymol package. This can be done adding it to the PYTHONPATH environment variable.

$ export PYTHONPATH=/opt/pymol/lib/python3.10/site-packages/:$PYTHONPATH

In linux, the pymol command is not a binary but just a shell script to run the corresponding python package. So you can check it to get the correct path to be added.

$ cat $(which pymol)

#!/bin/sh
exec "/usr/bin/python3" "/opt/pymol/lib/python3.10/site-packages/pymol/__init__.py" "$@"