Why Do I Need To Import Entrez Biopython Module Separately?
2
2
Entering edit mode
13.1 years ago
Martin ▴ 30

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 • 3.7k views
ADD COMMENT
5
Entering edit mode
13.1 years ago
dli ▴ 250

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'
>>>
ADD COMMENT
5
Entering edit mode

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.

ADD REPLY
4
Entering edit mode
13.1 years ago
brentp 24k

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.

ADD COMMENT

Login before adding your answer.

Traffic: 1648 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6