This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Why Do I Need To Import Entrez Biopython Module Separately?

If I try to run this script:

#imports
import Bio

#Give ncbi your e-mail
Bio.Entrez.email='no.name@acompany.com'

I get an surprising error:

Traceback (most recent call last):
    File "bio_error.py", line 9, in <module>
        Bio.Entrez.email='no.name@acompany.com'
AttributeError: 'module' object has no attribute 'Entrez'

Bu if I use:

#imports
import Bio
from Bio import Entrez

#Give ncbi your e-mail    
Bio.Entrez.email='no.name@acompany.com'

It works fine, why?

biopython entrez

2 answers

i think it is because Entrez module in under the subdirectory of Bio in the packages directory, import Bio didnot import the module in its subdirectory.

in my server it is located at /usr/lib64/python2.6/site-packages/Bio/Entrez

how about this:

Python 2.6.5 (r265:79063, Jun 25 2011, 08:36:25) 
[GCC 4.4.4 20100726 (Red Hat 4.4.4-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Bio.Entrez as Entrez
>>> Entrez.email = 'xxx@xxx.com'
>>>

That's exactly right. This isn't Biopython specific; that is how imports work in Python. Doing 'import Bio' does not recursively import individual modules. This improves speed by only importing necessary modules, at the small cost of needing to be explicit about the imports.

This is a python question. Say you have a directory structure that looks like this.

mymod/
     __init__.py
     auto/
          __init__.py
     noauto/
            __init__.py

mymod.auto.__init__.py has the contents `print "auto"`
mymod.noauto.__init__.py has the contents `print "noauto"`

and to import the auto only.

mymod.__init__.py has the contents `from . import auto`

So you get:

>>> import mymod
auto

>>> mymod.noauto
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'noauto'

>>> import mymod.noauto # or from mymod import noauto
noauto

Because the mymod does not automatically import noauto.

Log in to answer this question.