Skip to content

confusing error message in Bio.Align #1991

Closed

Description

@mdehoon

Bio.Align.__init__.py imports Bio.Align._aligners, which may fail if a user attempts to import Bio.Align from the biopython source tree. The error message warns against this:

try:
    from Bio.Align import _aligners
except ImportError as e:
    new_exc = ImportError("{}: you should not import directly from the "
                          "biopython source directory; please exit the source "
                          "tree and re-launch your code from there".format(e))

However, there may be other reasons why the import fails (for example, _aligners may be missing due to a failed compilation, or may have been compiled incorrectly). The warning message therefore should be rephrased to be more broad.

Activity

  1. chris-rands commented on Apr 5, 2019

    @chris-rands
    Contributor

    Agreed, my fault sorry, I didn't consider this in #1736. If you would like to suggest an appropriately worded message, I can easily write a PR (or write it yourself if you prefer of course), thanks

  2. MarkusPiotrowski commented on Apr 9, 2019

    @MarkusPiotrowski
    Contributor

    How about:
    "{}: you are possibly missing a compiled '_aligners.c' file (_aligners.pyd or _aligners.so); have you installed from source? Are you trying to import from within the Biopython source directory? Try running your code from outside the source tree".format(e)

  3. mdehoon commented on Apr 12, 2019

    @mdehoon
    ContributorAuthor

    It should be possible to detect whether we are in the Biopython source directory or not. Ideally in Bio/__init__.py, so that any attempt to import from the source directory raises an exception.

  4. mdehoon commented on Apr 13, 2019

    @mdehoon
    ContributorAuthor

    One way to do this is to check __file__ at the beginning of Bio/__init__.py.
    If in the source tree, it will report Bio/__init__.py or Bio/__init__.pyc.
    Outside of the source tree, it will report the full path, e.g. /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Bio/__init__.pyc.
    We could issue a warning or raise an Exception if __file__ is a relative path.
    If we want to be more precise, we could look in the parent directory to see if there's a setup.py file.

  5. MarkusPiotrowski commented on Apr 13, 2019

    @MarkusPiotrowski
    Contributor

    Do you think it's worth the hassle? I guess such an import error is mostly seen by developers, who are either working in a local git respository with no compiled C modules or who are directly working on the file, here __init__.py.

    I wonder if just working in the biopython directory will cause an import error at all:

    >>> import os
    >>> os.getcwd()
    'G:\\git_32bit\\biopython'
    >>> from Bio.Align import *
    >>>

    or

    >>> import os
    >>> os.getcwd()
    'G:\\git_32bit\\biopython\\Bio\\Align'
    >>> from Bio.Align import *
    >>>

    No error messages at all! So maybe the error message about importing from Biopython source tree is not only confusing but possibly wrong?

    Can someone show me how to invoke an ImportError here by being in the Biopython source directory?

  6. mdehoon commented on Apr 13, 2019

    @mdehoon
    ContributorAuthor

    In the Biopython source directory, I am getting

    >>> import os
    >>> os.getcwd()
    '/Users/mdehoon/Software/biopython-1.72'
    >>> from Bio import Align
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "Bio/Align/__init__.py", line 22, in <module>
        from Bio.Align import _aligners
    ImportError: cannot import name _aligners
    

    Perhaps you have PYTHONPATH set, causing an import from someplace outside of the source directory?

  7. MarkusPiotrowski commented on Apr 13, 2019

    @MarkusPiotrowski
    Contributor
    >>> import os
    >>> import Bio
    >>> os.getcwd()
    'G:\\git_32bit\\biopython'
    >>> Bio.__version__
    '1.74.dev0'
    >>> from Bio.Align import *
    >>>

    Also, I renamed _aligners.pyd in my standard Biopython installation (under pythonxxx/lib/site-packages/Bio) and, when importing from my developing Biopython branch, don't get an import error (so it is not imported from my standard Biopython). When I renamed _aligners.pyd in my developing Biopython branch, I get the import error. The same vice versa.
    So I only see the import error when I'm missing the compiled _aligners file, but not when I import from within the Biopython source tree.
    Are you sure you have a working compiled _aligners file in your /biopython-1.72 folder?

  8. mdehoon commented on Apr 14, 2019

    @mdehoon
    ContributorAuthor

    Are you sure you have a working compiled _aligners file in your /biopython-1.72 folder?

    Yes it is there.
    If you run

    >>> from Bio import Align
    >>> print Align.__file__
    

    and

    >>> from Bio.Align import _aligners
    >>> print _aligners.__file__
    

    it will tell you which Bio.Align is imported.

  9. chris-rands commented on Apr 14, 2019

    @chris-rands
    Contributor

    Importing from the biopython/ directory is a known cause of ImportError here as noted previously by myself and other users. As mdehoon said in a mailing list, numpy has a similar issue and this inspired my original traceback message. This Q&A has an explanation: https://stackoverflow.com/questions/14570011/explain-why-numpy-should-not-be-imported-from-source-directory

  10. MarkusPiotrowski commented on Apr 14, 2019

    @MarkusPiotrowski
    Contributor

    @chris-rands This is what I doubt. In the SO that you cited the answer says that you should leave the source directory directyl after install (and I think they mean the folder in which you have downloaded the package, and not the folder, in which the package was finally installed), because it will not contain the compiled C extension.

    However, once you have installed it, you should change your directory out of the source tree. Python starts looking for packages in your current directory, so the presence of the incomplete numpy package (without the necessary built C extension modules) will be picked up first and lead to the error that message that you quote.

    So there's no general magic that somehow prevents imports of C extensions when you are in in the source directory, it's just that it is missing the compiled C extensions. The message that I should leave the source tree does not help, because when I do this, I'm using my standard Biopython installation but not my developing branch. The only things that helps is to compile the C extensions.

    Maybe it's just a misunderstanding, but for me it is/was not clear, that "source directory" or "source tree" should only be the folder, in which the installation package was downloaded. When I read "leave the Biopython source tree" I think I have to leave my developing Biopython branch, but how should I run run_tests when doing this?

    @mdehoon You were right regarding one of my examples from above, e.g. when I'm in folder Bio in my developing branch and try to import Bio, it was using my system.path and imported from my standard Biopython installation.

    However, it's OK if I install on top of Bio in my developing biopython folder:

    (base) G:\git_32bit\biopython>python
    Python 2.7.15 |Anaconda, Inc.| (default, Feb 21 2019, 17:23:47) [MSC v.1500 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import Bio
    >>> Bio.__version__
    '1.74.dev0'
    >>> Bio.__file__
    'Bio\\__init__.pyc'
    >>> from Bio import Align
    >>> Align.__file__
    'Bio\\Align\\__init__.pyc'
    >>> from Bio.Align import _aligners
    >>> _aligners.__file__
    'Bio\\Align\\_aligners.pyd'
    >>>
    

    Your import error message 'Cannot import name _aligners' (in contrast to 'no module named _aligners') makes me thinking that something is wrong with your compiled aligner file.

    Whatever, I think the error message as suggested in PR #2007 is now much clearer, and I wonder, as said earlier, if we really should hassle to find out in which directory the user is actually in.

  11. mdehoon commented on Apr 15, 2019

    @mdehoon
    ContributorAuthor

    I see three problems with the error message in #2007 :

    • We would have to put this error message in each C extension;
    • It doesn't warn you if it can import _aligners from a location other than the install directory (as in your example);
    • It doesn't warn you in case of pure-Python extensions.

    In general, I think it is not a good idea to import modules from the source or development folder, as it can cause modules to be imported from unexpected places.

  12. mdehoon commented on Apr 15, 2019

    @mdehoon
    ContributorAuthor

    @MarkusPiotrowski My suggestion would be to print a warning (not an Exception) if Bio is imported from within the source tree (and no Exception or warning in Bio.Align).

  13. MarkusPiotrowski commented on Apr 15, 2019

    @MarkusPiotrowski
    Contributor

    @mdehoon Coming back to your original suggestion to detect already in Bio/__init__.py if the user is in the source tree I would agree if you make it a warning instead of an exception.

  14. MarkusPiotrowski commented on Apr 15, 2019

    @MarkusPiotrowski
    Contributor

    Ups, seems that our comments crossed each other. I deleted my other comment, when I realized that it was missing your original point and that you already had made a suggestion.

  15. mdehoon commented on Apr 15, 2019

    @mdehoon
    ContributorAuthor

    @peterjc , @chris-rands Would you agree with adding a warning to Bio/__init__.py if it is imported from the source tree? (and removing the ImportError from Bio/Align)?

  16. peterjc commented on Apr 15, 2019

    @peterjc
    Member

    I like the idea of a single warning in Bio/__init__.py if it is imported from the source tree.

    It does make sense to me to combine that with removing existing try/except to turn compiled code ImportError into a hand crafted error message.

    However, what about try/except to turn compiled code ImportError into a warning that slow Python code is being used instead? e.g. https://github.com/biopython/biopython/blob/biopython-173/Bio/pairwise2.py#L1105 - we can probably remove this warning if the top level warning is suitably worded?

    Some existing code silently drops back to slower Python code, so no change needed. e.g. https://github.com/biopython/biopython/blob/biopython-173/Bio/Nexus/Nexus.py#L1900

  17. mdehoon commented on Apr 16, 2019

    @mdehoon
    ContributorAuthor

    @peterjc

    However, what about try/except to turn compiled code ImportError into a warning that slow Python code is being used instead? e.g. https://github.com/biopython/biopython/blob/biopython-173/Bio/pairwise2.py#L1105 - we can probably remove this warning if the top level warning is suitably worded?

    Not all C code has a pure-python fallback. For example, the pairwise aligner in Bio.Align._aligners has only been implemented in C.

  18. peterjc commented on Apr 16, 2019

    @peterjc
    Member

    Poorly worded on my part. Those are the easy cases - they would get the new warning from Bio/__init__.py and then a (default) ImportError exception.

  19. MarkusPiotrowski commented on Apr 16, 2019

    @MarkusPiotrowski
    Contributor

    I'm not sure about the implications, would this mean that you can use nothing from Biopython, if you are missing a single compiled C extension?
    This would make testing on Windows quite hard.

  20. MarkusPiotrowski commented on Apr 16, 2019

    @MarkusPiotrowski
    Contributor

    Oh no, I understand now, a warning from Bio.__init__ and the ImportError from the respective module.

  21. peterjc commented on Apr 17, 2019

    @peterjc
    Member

    Or, if there is a pure Python fallback, a warning from Bio/__init__.py and perhaps a second warning.

  22. chris-rands commented on Apr 25, 2019

    @chris-rands
    Contributor

    Sorry for the delay, I just updated #2007 with a warning now raised from Bio/__init__.py when there is a setup.py file present. Is the way to check for the source tree appropriate? Is the wording of the warning precise enough?

    Was there a consensus on whether or not to remove the custom ImportError message in Bio/Align/__init__.py?

  23. peterjc commented on Apr 25, 2019

    @peterjc
    Member

    @chris-rands I think we have agreement: @mdehoon suggested the Bio/__init__.py warning and also removing the Bio/Align/__init.py error, #1991 (comment) - and I agree that seems sensible.

    @MarkusPiotrowski: ?

  24. MarkusPiotrowski commented on Apr 25, 2019

    @MarkusPiotrowski
    Contributor

    I'm in two minds about this. I can agree, but I also think that it cannot harm to have a more explicit ImportError message in those cases where a non-compiled C file may be the reason for the failed import. Of course we needn't to repeat the 'Biopython source-tree' stuff. Or we could mention 'missing compiled C extensions' in the general warning.

  25. peterjc commented on Apr 26, 2019

    @peterjc
    Member

    I would like the top level warning to specifically mention C extensions not working if importing from the source tree.

  26. peterjc commented on May 9, 2019

    @peterjc
    Member

    Closed via #2007

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions