text: user15529: I am looking for a Python package that performs pairwise structural alignment of protein structures (i.e., PDB files) and returns a sequence alignment. PyMOL is able to do this through the GUI, for example:
  
  For two protein PDBs, one can be aligned to the other with the "align" menu or align command:
  
  With it, the sequences appear aligned in the sequence viewer according to the structural alignment, and an "alignment" object is created, which can be saved with the command:
  save A_aligned_with_B.aln, aln_structure_1_to_structure_0
  which produces the A_aligned_with_B.aln alignment file in Clustal format (nevermind the ?s, which correspond to special HIS resnames):
  CLUSTAL
  
  structure_0  YIYQDSISLPWKVLLVMLLALITLATTLSNAFVIATVYRTRKL?TPANYLIASLAVTDLLVSI
  structure_1  EEQGNKL?WAALLILMVIIPTIG-----GNTLVILAVSLEKKLQYATNYFLMSLAVADLLVGL
                                           .*..**..*....**....**...****.****..
  
  structure_0  LVMPISTMYTVT-GRWTLGQVVCDFWLSSDITCCTASIL?LCVIALDRYWAITDAVEYSAKRT
  structure_1  FVMPIALLTIMFEAMWPLPLVLCPAWLFLDVLFSTASIM?LCAISVDRYIAIKKPIQANQYNS
               .****......  ..*.*..*.*..**..*....****.***.*..***.**........ ..
  
  structure_0  PKRAAVMIALVWVFSISISLPP-FFWRQAKAEEEVSE-CVVNTD?IL-YTVYSTVGAFYFPTL
  structure_1  RATAFIKITVVWLISIGIAIPVPIKGIETDVDNPNNITCVLTKERFGDFMLFGSLAAFFTPLA
               ...*...*..**..**.*..*  .              **....    ........**..*..
  
  structure_0  LLIALYGRIYVEARSRILKQTPNRTGKLLEKKKLMAAR--ERKATKTLGIILGAFIVCWLPFF
  structure_1  IMIVTYFLTI?ALQKKAYLVKNKPPQRLTIGKKSVQTISNEQRASKVLGIVFFLFLLMWCPFF
               ..*..*....                              * .*.*.***....*...*.***
  
  structure_0  IISLVMPICKDACW--F?LAIFDFFTWLGYLNSLINPIIYTMSNEDFKQAF?KLIRFK--   
  structure_1  ITNITLVLCDS-CNQTTLQMLLEIFVWIGYVSSGVNPLVYTLFNKTFRDAFGRYITCNYR   
               *.......*   *   .  .....*.*.**..*..**..**..*..*..**         
  
  I need a Python package that is easily installable/distributable that accepts two PDBs or protein structures and returns the structural alignment of the sequences somehow. All within a Python script, and not creating intermediate files, e.g. for the alignment.
  There are plenty of Python tools that allow structural superimposition and RMSD calculation (e.g., Biopython), but I haven't found any that are able to return a sequence alignment from the structural alignment.
  Edit:
  Possible answer, thanks to the answer and comments from Matteo Ferla.
  import pymol2
  with pymol2.PyMOL() as pymol:
      pymol.cmd.load('foo.pdb', 'foo')
      pymol.cmd.load('bar.pdb', 'bar')
      pymol.cmd.align('foo', 'bar', object='aln')
      aln = pymol2.cmd2.pymol.exporting.get_alnstr('aln', _self=pymol.cmd) # I don't know if this line could be cleaner
      # aln is itself a string
  
  
  user6322: PyMOL in itself can be used as python package.
  The GUI app has its own Python, but in a conda environment you can install it as a package.
  Any PyMOL app command can be used as a cmd command. In PyMOL Wiki you see both. They follow a similar pattern and actually have more functionality.
  import pymol2
  with pymol2.PYMOL() as pymol:
      pymol.cmd.load('foo.pdb', 'foo')
      pymol.cmd.load('bar.pdb', 'bar')
      pymol.cmd.align('foo', 'bar')
      pymol.cmd.save('foobar.aln')
  
  For more discussion of how the alignment output object is used see Assessing PyMol sequence alignment object
  
original_text: 15529: <p>I am looking for a Python package that performs pairwise structural alignment of protein structures (i.e., PDB files) and returns a sequence alignment. PyMOL is able to do this through the GUI, for example:</p>
  <p><a href="https://i.stack.imgur.com/mtLMF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mtLMF.png" alt="PyMOL GUI showing two protein structures and their sequences" /></a></p>
  <p>For two protein PDBs, one can be aligned to the other with the &quot;align&quot; menu or <code>align</code> command:</p>
  <p><a href="https://i.stack.imgur.com/weICw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/weICw.png" alt="PyMOL GUI showing the two protein structures superimposed and their sequences aligned position by position (and with gaps)" /></a></p>
  <p>With it, the sequences appear aligned in the sequence viewer according to the structural alignment, and an &quot;alignment&quot; object is created, which can be saved with the command:</p>
  <p><code>save A_aligned_with_B.aln, aln_structure_1_to_structure_0</code></p>
  <p>which produces the <code>A_aligned_with_B.aln</code> alignment file in Clustal format (nevermind the ?s, which correspond to special HIS resnames):</p>
  <pre><code>CLUSTAL
  
  structure_0  YIYQDSISLPWKVLLVMLLALITLATTLSNAFVIATVYRTRKL?TPANYLIASLAVTDLLVSI
  structure_1  EEQGNKL?WAALLILMVIIPTIG-----GNTLVILAVSLEKKLQYATNYFLMSLAVADLLVGL
                                           .*..**..*....**....**...****.****..
  
  structure_0  LVMPISTMYTVT-GRWTLGQVVCDFWLSSDITCCTASIL?LCVIALDRYWAITDAVEYSAKRT
  structure_1  FVMPIALLTIMFEAMWPLPLVLCPAWLFLDVLFSTASIM?LCAISVDRYIAIKKPIQANQYNS
               .****......  ..*.*..*.*..**..*....****.***.*..***.**........ ..
  
  structure_0  PKRAAVMIALVWVFSISISLPP-FFWRQAKAEEEVSE-CVVNTD?IL-YTVYSTVGAFYFPTL
  structure_1  RATAFIKITVVWLISIGIAIPVPIKGIETDVDNPNNITCVLTKERFGDFMLFGSLAAFFTPLA
               ...*...*..**..**.*..*  .              **....    ........**..*..
  
  structure_0  LLIALYGRIYVEARSRILKQTPNRTGKLLEKKKLMAAR--ERKATKTLGIILGAFIVCWLPFF
  structure_1  IMIVTYFLTI?ALQKKAYLVKNKPPQRLTIGKKSVQTISNEQRASKVLGIVFFLFLLMWCPFF
               ..*..*....                              * .*.*.***....*...*.***
  
  structure_0  IISLVMPICKDACW--F?LAIFDFFTWLGYLNSLINPIIYTMSNEDFKQAF?KLIRFK--   
  structure_1  ITNITLVLCDS-CNQTTLQMLLEIFVWIGYVSSGVNPLVYTLFNKTFRDAFGRYITCNYR   
               *.......*   *   .  .....*.*.**..*..**..**..*..*..**         
  </code></pre>
  <p>I need a Python package that is easily installable/distributable that accepts two PDBs or protein structures and returns the structural alignment of the sequences somehow. All within a Python script, and not creating intermediate files, e.g. for the alignment.</p>
  <p>There are plenty of Python tools that allow structural superimposition and RMSD calculation (e.g., Biopython), but I haven't found any that are able to return a sequence alignment from the structural alignment.</p>
  <p><strong>Edit:</strong></p>
  <p>Possible answer, thanks to the answer and comments from <a href="https://bioinformatics.stackexchange.com/a/19109/15529">Matteo Ferla</a>.</p>
  <pre class="lang-py prettyprint-override"><code>import pymol2
  with pymol2.PyMOL() as pymol:
      pymol.cmd.load('foo.pdb', 'foo')
      pymol.cmd.load('bar.pdb', 'bar')
      pymol.cmd.align('foo', 'bar', object='aln')
      aln = pymol2.cmd2.pymol.exporting.get_alnstr('aln', _self=pymol.cmd) # I don't know if this line could be cleaner
      # aln is itself a string
  </code></pre>
   6322: <p>PyMOL in itself can be used as python package.
  The GUI app has its own Python, but in a conda environment you can install it as a package.
  Any PyMOL app command can be used as a cmd command. In PyMOL Wiki you see both. They follow a similar pattern and actually have more functionality.</p>
  <pre><code>import pymol2
  with pymol2.PYMOL() as pymol:
      pymol.cmd.load('foo.pdb', 'foo')
      pymol.cmd.load('bar.pdb', 'bar')
      pymol.cmd.align('foo', 'bar')
      pymol.cmd.save('foobar.aln')
  </code></pre>
  <p>For more discussion of how the alignment output object is used see <a href="https://bioinformatics.stackexchange.com/questions/14919/assessing-pymol-sequence-alignment-object/14936#14936">Assessing PyMol sequence alignment object</a></p>
  