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
{{ message }}
Repository navigation
editable/develop install warning: You may be importing Biopython from inside the source tree. #2318
I am reporting a problem with Biopython version, Python version, and operating
system as follows:
$ python
Python 3.7.4 (default, Aug 13 2019, 15:17:50)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys; print(sys.version)
3.7.4 (default, Aug 13 2019, 15:17:50)
[Clang 4.0.1 (tags/RELEASE_401/final)]
>>> import platform; print(platform.python_implementation()); print(platform.platform())
CPython
Darwin-18.7.0-x86_64-i386-64bit
>>> import Bio; print(Bio.__version__)
/Users/xxx/repositories/biopython/Bio/__init__.py:128: BiopythonWarning: You may be importing Biopython from inside the source tree. This is bad practice and might lead to downstream issues. In particular, you might encounter ImportErrors due to missing compiled C extensions. We recommend that you try running your code from outside the source tree. If you are outside the source tree then you have a setup.py file in an unexpected directory: /Users/xxx/repositories/biopython.
format(_parent_dir), BiopythonWarning)
1.75.dev0
(Please copy and run the above in your Python, and copy-and-paste the output)
Expected behaviour
No warning BiopythonWarning: You may be importing Biopython from inside the source tree. ...
Actual behaviour
Noisy warning as above.
Steps to reproduce
Using pip to install in editable (develop) mode:
$ pip install -h
...
-e, --editable <path/url> Install a project in editable mode (i.e. setuptools "develop mode") from a local
project path or a VCS url.
...
I've not looked into how exactly editable mode works - it seems to cope with C extensions with them too in the source tree - this is on macOS:
$ cd
$ python
...
>>> import Bio
/Users/xxx/repositories/biopython/Bio/__init__.py:128: BiopythonWarning: You may be importing Biopython from inside the source tree. This is bad practice and might lead to downstream issues. In particular, you might encounter ImportErrors due to missing compiled C extensions. We recommend that you try running your code from outside the source tree. If you are outside the source tree then you have a setup.py file in an unexpected directory: /Users/xxx/repositories/biopython.
format(_parent_dir), BiopythonWarning)
>>> Bio.__file__
'/Users/xxx/repositories/biopython/Bio/__init__.py'
>>> from Bio.Nexus import cnexus
>>> cnexus.__file__
'/Users/xxx/repositories/biopython/Bio/Nexus/cnexus.cpython-37m-darwin.so'
Perhaps the original try/excepts on all the C code imports was better, despite being more work?
On reflection, I'm in favour of removing the warning message. I now test custom biopython branches usingeditable via conda with an environment.yml that looks like this:
I'm not sure if this is a good setup for testing branches(?), but anyway, this means I always get the warning traceback, which is inappropriate. I do not have a better solution than the original custom ImportError messages for the C extensions or leaving the user to debug any installation issues.
Yeah, I think so too - as implemented our BiopythonWarning: You may be importing Biopython from inside the source tree. ... is breaking a very real use case. e.g. It deters me from using developer mode (which is otherwise especially useful if you tend only to modify the Python code).
I've not thought of any way to fix the warning; reverting back to the old try/except on each major C module may be best.
So we are agreeing towards (re)instating a custom traceback via try-except clauses (like in Bio/Align/__init__.py when _aligners is imported). Which imports exactly would need this form of custom traceback?
We need @mdehoon's input before making any change here.
There are also parts of the C code with a Python fall back (Nexus and pairwise2?) which used to be important for Jython (deprecated) and PyPy (C code should generally work now).
These are the extensions that are build when doing python setup.py build:
Bio.Align._aligners
Bio.cpairwise2
Bio.Nexus.cnexus
Bio.PDB.QCPSuperimposer.qcprotmodule
Bio.motifs._pwm
Bio.Cluster._cluster
Bio.PDB.kdtrees
As @peterjc said, pairwise2 has a pure Python fallback, it prints a warning and continues. The same seems to be true for Nexus.Nexus (but without giving a warning).
I thought we got the idea for this type of warning mechanism from another project? Was it numpy? Do they have similar reported issues?
Another option is just to remove the warning, without taking care about the imports. And we could move the text of the warning message to our Readme.rst. Possibly in a FAQ paragraph?
Removing the warning and adding an FAQ might be enough given the dominance of wheels nowadays.
Nowadays most people will install from wheels, which will have the compiled files. The only people who will hit the C import errors are self-builders (who are likely to be more knowledgable?) and people running in the source folder (likely to be novices not using a wheel?). It was the later group our warning aimed to help.
I thought we got the idea for this type of warning mechanism from another project? Was it numpy? Do they have similar reported issues?
We discussed what numpy do #2007 (comment), but never implemented it. I think we dismissed it as too complex, but I guess it has the advantage that what they do is more robust and will not give these false positive warnings for editable/develop installations
+1 on following NumPy with their __config__ import check approach. It is more complicated, but I accept now that the simple option we took has severe downsides.
numpy's solution is quite involved and difficult to emulate in full- it generates a lot of config info inside the file. Instead, i'm trying a light-weight version where an empty Bio.__config__.py is added during installation but remains absent from the source tree. In my case I copy the file from Scripts/__config__.py. Then in Bio/__init__.py a try-except clause wraps import Bio.__config__.
However, I'm struggling, I tried adding to setup.py either data_files=[("Bio", ["Scripts/__config__.py"]),] or package_data={"Bio": ["Scripts/__config__.py"]} but without success. I'm not sure if the MANIFEST.in can be edited to copy files in this way where the structure of the source tree and install dir is not identical. Perhaps someone with more experience in packaging can help?
You can probably do touch Bio/__config__.py during the build rather than a copy... if in Python land, maybe something like:
with open("Bio/__config__.py", "w") as handle:
handle.write('"""This is a dummy file created during installation to control a local import warning."""\n')
I would guess adding Bio/__config__.py explicitly to MANIFEST.in will work - it would be there during build time?
Thanks @peterjc that would work too, but the part I'm not sure on is how to add __config__.py to the /path/lib/python3.7/site-packages/biopython-*.egg/Bio/ dir and not the source Bio/ dir (unless editable mode is used)?
(Possibly relevant question: Does NumPy's solution actually work for develop/editable mode?)
Yes it does, I tested running pip install -e . and it adds the __config__.py correctly. The issue is that numpy's install setup is really involved so it's hard to understand/emulate.
I was thinking we could maybe use numpy's own config class for this since numpy is a biopython dependancy. This would also have the advantage of logging all the system config information at the time of building the package. However, this would mean that numpy would need installing before we call setup.py
I'm definitely not a packaging expert but let me see if I get this straight. Is the idea to have a __config__.py file that only gets created/copied when you do setup.py install/develop? And that way, we could control in Bio/__init__.py if we are running from source or not?
One extremely naive idea is to check if we can find any egg file for Biopython. Both setup.py install and setup.py develop create egg files, egg-link and .egg respectively, in a site-packages folder, which can be global or on the user's home. We can use the site module to find the paths to these folders and just check for any file with 'egg' in its extension and 'biopython' in the name? We can add checking for dist-info to check if Biopython was installed via pip install biopython or conda install biopython (wheels).
I wrote this silly function to do this:
defused_setuptools():
"""Returns True if Biopython was setup using setuptools."""importpathlibimportsitepkgdirs=site.getsitepackages()
pkgdirs.append(site.getusersitepackages()) # userforpinpkgdirs:
p=pathlib.Path(p)
ifnotp.exists():
continueforchildinp.iterdir():
if'egg'inchild.suffix:
if'biopython'inchild.name:
returnTrue# install from sourceelifchild.suffix=='dist-info':
ifbiopythoninchild.name:
returnTrue# install from wheelreturnFalseis_imported=used_setuptools()
print(is_imported)
I tested on Windows/Linux with:
python setup.py install = True
python setup.py develop = True
pip install -e . = True
pip install --user -e . = True
pip install biopython = True
conda install biopython = True
manually add source folder to PYTHONPATH (only tested on Linux) = False
It's a very simple approach with maybe several caveats that I'm not seeing/ignoring because I don't know enough about the problem. But it would be easy to add to Bio/__init__.py, run it on import and if False, issue the warning.
Thanks @JoaoRodrigues ! That looks like a neat approach although I'm unfamiliar with it, so maybe someone else can comment. How much overhead does it add? We want import Bio to be fast of course so I wonder if your logic can ran at the end of the build to generate the dummy flag/file (e.g. __config__.py) that we then simply need to check for in Bio/__init__.py?
I wouldn't say it adds much, only if the user has thousands of packages installed. Also, since biopython starts with a 'b' it should find it pretty quickly. The worst case scenario here is that return False where you have to go through all the files in those pkgdirs if biopython is not installed.
Adding my function to Bio/__init__.py and using it instead of the os.path.exists test gives this:
python setup.py develop
(biopy-test) [11:42] joaor@Tests $ time python -X importtime test_imports.py
import time: self [us] | cumulative | imported package
...
import time: 467 | 1792 | os
...
import time: 853 | 4721 | site
...
import time: 850 | 10974 | pathlib
import time: 5945 | 17279 | Bio
real 0m0.033s
user 0m0.020s
sys 0m0.000s
Added source folder to PYTHONPATH (worse case scenario)
(biopy-test) [11:46] joaor@Tests $ time python -X importtime test_imports.py
import time: self [us] | cumulative | imported package
...
import time: 12297 | 29430 | Bio
real 0m0.050s
user 0m0.008s
sys 0m0.016s
Left out a bunch of output from other modules being imported, kept the two I added (site and pathlib). Seems like peanuts, a few milliseconds on my machine.
Based on @JoaoRodrigues work exploring site.getsitepackages() I have turned that into PR #3653 which looks for the special .egg-link file in the deployment directory which setuptools creates as a pointer for a development installation.
Activity
chris-rands commentedon Oct 31, 2019
We discussed quite a few alternatives in #2007, none of which were perfect. Do you have I suggested fix?
peterjc commentedon Oct 31, 2019
I've not looked into how exactly editable mode works - it seems to cope with C extensions with them too in the source tree - this is on macOS:
Perhaps the original try/excepts on all the C code imports was better, despite being more work?
CC @mdehoon
chris-rands commentedon Sep 3, 2020
On reflection, I'm in favour of removing the warning message. I now test custom biopython branches using
editablevia conda with anenvironment.ymlthat looks like this:I'm not sure if this is a good setup for testing branches(?), but anyway, this means I always get the warning traceback, which is inappropriate. I do not have a better solution than the original custom
ImportErrormessages for the C extensions or leaving the user to debug any installation issues.peterjc commentedon Sep 3, 2020
Yeah, I think so too - as implemented our
BiopythonWarning: You may be importing Biopython from inside the source tree. ...is breaking a very real use case. e.g. It deters me from using developer mode (which is otherwise especially useful if you tend only to modify the Python code).I've not thought of any way to fix the warning; reverting back to the old try/except on each major C module may be best.
JoaoRodrigues commentedon Sep 3, 2020
So you'd only issue the warning if you imported a C extension or a module that might call it? That seems very reasonable!
chris-rands commentedon Sep 4, 2020
So we are agreeing towards (re)instating a custom traceback via try-except clauses (like in
Bio/Align/__init__.pywhen_alignersis imported). Which imports exactly would need this form of custom traceback?peterjc commentedon Sep 4, 2020
We need @mdehoon's input before making any change here.
There are also parts of the C code with a Python fall back (Nexus and pairwise2?) which used to be important for Jython (deprecated) and PyPy (C code should generally work now).
MarkusPiotrowski commentedon Sep 4, 2020
These are the extensions that are build when doing
python setup.py build:As @peterjc said,
pairwise2has a pure Python fallback, it prints a warning and continues. The same seems to be true forNexus.Nexus(but without giving a warning).I thought we got the idea for this type of warning mechanism from another project? Was it
numpy? Do they have similar reported issues?Another option is just to remove the warning, without taking care about the imports. And we could move the text of the warning message to our
Readme.rst. Possibly in a FAQ paragraph?peterjc commentedon Sep 4, 2020
Removing the warning and adding an FAQ might be enough given the dominance of wheels nowadays.
Nowadays most people will install from wheels, which will have the compiled files. The only people who will hit the C import errors are self-builders (who are likely to be more knowledgable?) and people running in the source folder (likely to be novices not using a wheel?). It was the later group our warning aimed to help.
chris-rands commentedon Sep 4, 2020
We discussed what
numpydo #2007 (comment), but never implemented it. I think we dismissed it as too complex, but I guess it has the advantage that what they do is more robust and will not give these false positive warnings for editable/develop installationspeterjc commentedon Sep 4, 2020
+1 on following NumPy with their
__config__import check approach. It is more complicated, but I accept now that the simple option we took has severe downsides.mdehoon commentedon Sep 4, 2020
Numpy's solution sounds good to me.
chris-rands commentedon Sep 5, 2020
numpy's solution is quite involved and difficult to emulate in full- it generates a lot of config info inside the file. Instead, i'm trying a light-weight version where an emptyBio.__config__.pyis added during installation but remains absent from the source tree. In my case I copy the file fromScripts/__config__.py. Then inBio/__init__.pya try-except clause wrapsimport Bio.__config__.However, I'm struggling, I tried adding to
setup.pyeitherdata_files=[("Bio", ["Scripts/__config__.py"]),]orpackage_data={"Bio": ["Scripts/__config__.py"]}but without success. I'm not sure if theMANIFEST.incan be edited to copy files in this way where the structure of the source tree and install dir is not identical. Perhaps someone with more experience in packaging can help?peterjc commentedon Sep 5, 2020
You can probably do
touch Bio/__config__.pyduring the build rather than a copy... if in Python land, maybe something like:I would guess adding
Bio/__config__.pyexplicitly toMANIFEST.inwill work - it would be there during build time?chris-rands commentedon Sep 5, 2020
Thanks @peterjc that would work too, but the part I'm not sure on is how to add
__config__.pyto the/path/lib/python3.7/site-packages/biopython-*.egg/Bio/dir and not the sourceBio/dir (unless editable mode is used)?peterjc commentedon Sep 5, 2020
Ah - I begin to see the conundrum. (Possibly relevant question: Does NumPy's solution actually work for develop/editable mode?)
Maybe we remove the clever warning, and go back to catching a few more likely
ImportErrorexceptions with a little more guidance?chris-rands commentedon Sep 5, 2020
Yes it does, I tested running
pip install -e .and it adds the__config__.pycorrectly. The issue is that numpy's install setup is really involved so it's hard to understand/emulate.I was thinking we could maybe use numpy's own config class for this since numpy is a biopython dependancy. This would also have the advantage of logging all the system config information at the time of building the package. However, this would mean that numpy would need installing before we call
setup.pyJoaoRodrigues commentedon Sep 5, 2020
Thanks for all the detective work @chris-rands !
I'm definitely not a packaging expert but let me see if I get this straight. Is the idea to have a
__config__.pyfile that only gets created/copied when you dosetup.py install/develop? And that way, we could control inBio/__init__.pyif we are running from source or not?chris-rands commentedon Sep 5, 2020
Yes @JoaoRodrigues that's a good summary thanks
I guess another idea would be to create a dummy C extension for this after all the regular ones are built
JoaoRodrigues commentedon Sep 5, 2020
One extremely naive idea is to check if we can find any egg file for Biopython. Both
setup.py installandsetup.py developcreate egg files,egg-linkand.eggrespectively, in asite-packagesfolder, which can be global or on the user's home. We can use thesitemodule to find the paths to these folders and just check for any file with 'egg' in its extension and 'biopython' in the name? We can add checking fordist-infoto check if Biopython was installed viapip install biopythonorconda install biopython(wheels).I wrote this silly function to do this:
I tested on Windows/Linux with:
python setup.py install= Truepython setup.py develop= Truepip install -e .= Truepip install --user -e .= Truepip install biopython= Trueconda install biopython= TrueIt's a very simple approach with maybe several caveats that I'm not seeing/ignoring because I don't know enough about the problem. But it would be easy to add to
Bio/__init__.py, run it on import and if False, issue the warning.chris-rands commentedon Sep 7, 2020
Thanks @JoaoRodrigues ! That looks like a neat approach although I'm unfamiliar with it, so maybe someone else can comment. How much overhead does it add? We want
import Bioto be fast of course so I wonder if your logic can ran at the end of the build to generate the dummy flag/file (e.g.__config__.py) that we then simply need to check for inBio/__init__.py?JoaoRodrigues commentedon Sep 7, 2020
I wouldn't say it adds much, only if the user has thousands of packages installed. Also, since
biopythonstarts with a 'b' it should find it pretty quickly. The worst case scenario here is thatreturn Falsewhere you have to go through all the files in thosepkgdirsif biopython is not installed.Adding my function to
Bio/__init__.pyand using it instead of theos.path.existstest gives this:python setup.py developAdded source folder to PYTHONPATH (worse case scenario)
Left out a bunch of output from other modules being imported, kept the two I added (
siteandpathlib). Seems like peanuts, a few milliseconds on my machine.Silence source import warning in dev mode
peterjc commentedon Jul 5, 2021
Based on @JoaoRodrigues work exploring
site.getsitepackages()I have turned that into PR #3653 which looks for the special.egg-linkfile in the deployment directory which setuptools creates as a pointer for a development installation.Silence source import warning in dev mode