4

When I try,

from Bio.Alphabet import IUPAC
from Bio import Seq
my_prot = Seq("AGTACACTGGT", IUPAC.protein)

Why do I encounter the following error:

TypeError: 'module' object is not callable

PS: this is an Example from the BioPython's Cookbook

shellter
  • 36,525
  • 7
  • 83
  • 90
Saif al Harthi
  • 2,948
  • 1
  • 21
  • 26

2 Answers2

8

In the BioPython source code the "Seq" class is located in the file "Seq.py" in the path "/Seq/Seq.py"

Meaning... You need to import Seq (a file) which means its a "Module" and then call the class "Seq" within the 'Module' 'Seq'

So try this:

from Bio.Alphabet import IUPAC
from Bio import Seq
my_prot=Seq.Seq("AGTACACTGGT",IUPAC.protein)

If you are ever confused in Python about what you are importing and what you are calling you can do this:

import Bio.Seq
print type(Bio.Seq)
>>> <type 'module'>
print type(Bio.Seq.Seq)
>>> <type 'classobj'>
Ben DeMott
  • 3,362
  • 1
  • 25
  • 35
  • 2
    In general, when you see this pattern, the library author may be expecting you to write `from Bio.Seq import Seq`. Then use the `Seq` class as you did in your question. – Wesley Apr 01 '11 at 21:56
  • Note that since [this commit](https://github.com/biopython/biopython/commit/6d9d97b6c09c9b01d18d81900ae7d33e0d4694ea) that `Seq` has become a new-style class. So now `print type(Bio.Seq.Seq)` will return ``. – BioGeek Jul 28 '16 at 13:55
1

Ben gave a nice clear answer explaining the problem. I guess you copied the example wrong,

>>> from Bio.Seq import Seq
>>> from Bio.Alphabet import IUPAC
>>> my_prot = Seq("AGTACACTGGT", IUPAC.protein)
>>> my_prot
Seq('AGTACACTGGT', IUPACProtein())
>>> my_prot.alphabet
IUPACProtein()

At least, that's what it currently says http://www.biopython.org/DIST/docs/tutorial/Tutorial.html

Note that the cause of the confusion would be avoided had Biopython used seq (lower case) for the module, and Seq (title case) for the class - which is now recommended practice for Python, see http://www.python.org/dev/peps/pep-0008/

Peter Cock
  • 1,585
  • 1
  • 9
  • 14