Search code examples
pythonpymol

How to enable python loop in pymol command line?


Here said the script should be used within the pymol commandline. I would like to output many distances using a loop after reading this. But I got error message:

File "<string>", line 1
    for i in range(resi_total_n):
                                ^
SyntaxError: unexpected EOF while parsing

My code is:

from pymol import cmd
mol_name='name'
resi=10 # the target residue number
resi_total_n=500 # the total residue number

f=open('dist.txt','w')
resi_n=0
for i in range(resi_total_n):
    resi_n += 1
    dst=cmd.distance('tmp',mol_name+'///'+str(resi)+'/ca',mol_name+'///'+str(resi_n)+'/ca') #the alpha carbon
    f.write("%8.3f\n"%dst)
f.close()

Solution

  • Here I found the answer:

    When trying to do programming, better stick to Python. Save the following as script.py and use run script.py from within Pymol or just issue pymol script.py