You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
fromBio.Alignimport_alignersexceptImportErrorase:
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.
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
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)
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.
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.
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?
>>> 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?
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?
@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.
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.
@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).
@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.
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.
@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)?
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.
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?
@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.
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.
Activity
chris-rands commentedon Apr 5, 2019
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
MarkusPiotrowski commentedon Apr 9, 2019
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)
mdehoon commentedon Apr 12, 2019
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.mdehoon commentedon Apr 13, 2019
One way to do this is to check
__file__at the beginning ofBio/__init__.py.If in the source tree, it will report
Bio/__init__.pyorBio/__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.pyfile.MarkusPiotrowski commentedon Apr 13, 2019
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
biopythondirectory will cause an import error at all:or
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
ImportErrorhere by being in the Biopython source directory?mdehoon commentedon Apr 13, 2019
In the Biopython source directory, I am getting
Perhaps you have PYTHONPATH set, causing an import from someplace outside of the source directory?
MarkusPiotrowski commentedon Apr 13, 2019
Also, I renamed
_aligners.pydin 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.pydin 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
_alignersfile, but not when I import from within the Biopython source tree.Are you sure you have a working compiled
_alignersfile in your /biopython-1.72 folder?mdehoon commentedon Apr 14, 2019
Yes it is there.
If you run
and
it will tell you which
Bio.Alignis imported.chris-rands commentedon Apr 14, 2019
Importing from the
biopython/directory is a known cause ofImportErrorhere as noted previously by myself and other users. As mdehoon said in a mailing list,numpyhas 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-directoryMarkusPiotrowski commentedon Apr 14, 2019
@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.
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_testswhen doing this?@mdehoon You were right regarding one of my examples from above, e.g. when I'm in folder
Bioin my developing branch and try to import Bio, it was using mysystem.pathand imported from my standard Biopython installation.However, it's OK if I install on top of
Bioin my developing biopython folder: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
alignerfile.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.
mdehoon commentedon Apr 15, 2019
I see three problems with the error message in #2007 :
_alignersfrom a location other than the install directory (as in your example);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.
mdehoon commentedon Apr 15, 2019
@MarkusPiotrowski My suggestion would be to print a warning (not an Exception) if
Biois imported from within the source tree (and no Exception or warning inBio.Align).MarkusPiotrowski commentedon Apr 15, 2019
@mdehoon Coming back to your original suggestion to detect already in
Bio/__init__.pyif the user is in the source tree I would agree if you make it a warning instead of an exception.MarkusPiotrowski commentedon Apr 15, 2019
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.
mdehoon commentedon Apr 15, 2019
@peterjc , @chris-rands Would you agree with adding a warning to
Bio/__init__.pyif it is imported from the source tree? (and removing the ImportError fromBio/Align)?peterjc commentedon Apr 15, 2019
I like the idea of a single warning in
Bio/__init__.pyif 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
ImportErrorinto a hand crafted error message.However, what about try/except to turn compiled code
ImportErrorinto 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
mdehoon commentedon Apr 16, 2019
@peterjc
Not all C code has a pure-python fallback. For example, the pairwise aligner in Bio.Align._aligners has only been implemented in C.
peterjc commentedon Apr 16, 2019
Poorly worded on my part. Those are the easy cases - they would get the new warning from
Bio/__init__.pyand then a (default)ImportErrorexception.MarkusPiotrowski commentedon Apr 16, 2019
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.
MarkusPiotrowski commentedon Apr 16, 2019
Oh no, I understand now, a warning from
Bio.__init__and theImportErrorfrom the respective module.peterjc commentedon Apr 17, 2019
Or, if there is a pure Python fallback, a warning from
Bio/__init__.pyand perhaps a second warning.chris-rands commentedon Apr 25, 2019
Sorry for the delay, I just updated #2007 with a warning now raised from
Bio/__init__.pywhen there is asetup.pyfile 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
ImportErrormessage inBio/Align/__init__.py?peterjc commentedon Apr 25, 2019
@chris-rands I think we have agreement: @mdehoon suggested the
Bio/__init__.pywarning and also removing theBio/Align/__init.pyerror, #1991 (comment) - and I agree that seems sensible.@MarkusPiotrowski: ?
MarkusPiotrowski commentedon Apr 25, 2019
I'm in two minds about this. I can agree, but I also think that it cannot harm to have a more explicit
ImportErrormessage 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.peterjc commentedon Apr 26, 2019
I would like the top level warning to specifically mention C extensions not working if importing from the source tree.
peterjc commentedon May 9, 2019
Closed via #2007
Silence source import warning in dev mode
Silence source import warning in dev mode