Question: Why Do I Need To Import Entrez Biopython Module Separately?
2
gravatar for Martin
7.4 years ago by
Martin30
Martin30 wrote:

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 • 1.9k views
ADD COMMENTlink modified 7.4 years ago by dli220 • written 7.4 years ago by Martin30
5
gravatar for dli
7.4 years ago by
dli220
WUSTL
dli220 wrote:

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 COMMENTlink modified 6.9 years ago • written 7.4 years ago by dli220
5

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 REPLYlink written 7.4 years ago by Brad Chapman9.3k
4
gravatar for brentp
7.4 years ago by
brentp22k
Salt Lake City, UT
brentp22k wrote:

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 COMMENTlink modified 7.4 years ago • written 7.4 years ago by brentp22k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 836 users visited in the last hour