ImportError for skbio module - python

I am running Python 3 and have skbio v0.5.5 installed. Following the examples in this tutorial, I am trying to run the import statements for some skbio classes, but am getting errors. For example,
from skbio.alignment import Alignment
results in
ImportError: cannot import name 'Alignment' from 'skbio.alignment'
Also,
from skbio import BiologicalSequence
results in
ImportError: cannot import name 'Alignment' from 'BiologicalSequence'
How do I resolve this?

The Alignment class is from an older version of the skbio library, specifically from before version 0.3.
If you would like to use those classes, you would need to install scikit-bio from before that version, by doing something like:
pip install scikit-bio==0.2
If you're just going through the Introduction to Applied Bioinformatics book, like you mentioned in your comment, it's better to use the latest version of the book instead, which will use the latest version of the scikit-bio library.

Related

ImportError - arch module python

I am trying to use the arch module in python. After installing it, I succesfully imported arch_model by executing from arch import arch_model.
However, I also need to use other functions, such as ConstantMean, as documented on the maintainers github here.
Yet, when I try to import it, it gives me the following error:
ImportError: cannot import name 'ConstantMean' from 'arch'
(C:\Users\frede\anaconda3\envs\earnings_risk\lib\site-packages\arch_init_.py)
When I check the functions available in the module via dir(arch) it also does not list ConstantMean, nor most of the other functions that should theoretically be available according to the documentation. What can be the reason for this? Any help is appreciated.
Try This:
from arch.univariate import ConstantMean

ImportError: cannot import name 'array2d'

Code:
from sklearn.utils.validation import array2d
Output:
ImportError: cannot import name 'array2d'
I have installed sklearn v0.23.1 with pycharm.
I tried to look in site-packages\sklearn\utils\validation.py and find "array2d" with ctrl+F, but it is not there. Can I import something when it is not in the file?
I am not sure, if the problem is in installation or import.
According to the documentation of the latest stable version (currently 0.23.1), there is no module array2d in sklearn.utils.validation.
The situation is the same for the previous versions 0.22.2, 0.21.3, 0.20.4, 0.19.2, and 0.18.2.
Not sure where you got this from and what you are trying to do - googling the error leads to this closed Github thread, where the usage is not like the one you try here (and the error there comes from a broken installation, which is certainly not the case here).
In short, the command from sklearn.utils.validation import array2d is invalid, and the error thus justified.

Import threshold_yen and threshold_isodata from skimage.filter

I am working with different threshold algorithms from SKimage, and when I go to import certain packages I get an error, but have no problem with others. For example:
from skimage.filter import threshold_adaptive, threshold_isodata
returns the traceback:
ImportError: cannot import name threshold_isodata. I am using python 2.7, and following the documentation found here: http://scikit-image.org/docs/dev/api/skimage.filter.html#skimage.filter.threshold_isodata
Specifically, I'm hoping to use threshold_isodata and threshold_yen. Does anybody have suggestions for how to solve this error? Alternatively, are there other packages that use the same algorithm?
As mentioned in a comment, threshold_isodata is only available in the master repo (i.e. not officially released in v0.9), hence the import error.
It turns out that threshold_yen wasn't properly imported into the filter subpackage in version 0.9. (This has been fixed in master.) Until v0.10 is released, you should import threshold_yen as follows:
from skimage.filter.thresholding import threshold_yen
EDIT: Note that this question and answer are specific to very old versions of scikit-image. The skimage.filter module was renamed skimage.filters in v0.11

How to import a module from a directory?

On my system I have two versions of Python (to call them I type python and python2 in the command line). When I use the first version of Python, I cannot import sklearn module but I can do it in the second version of Python.
I would like to use the first version of python (because other modules are available there) and, at the same time, I would like to be able to import sklearn from this version of Python.
My solution was to use:
import sys
sys.path.append('location_of_the_sklearn_module')
To find the location of the sklearn module I started a python session (using the second version of python, in which sklearn works). The I type:
import sklearn
sklearn.__file__
As a result I got:
/home/name/my_name/numpy/local/lib/python2.7/site-packages/sklearn/__init__.pyc
In the session of the first version of Python I tried:
import sys
sys.path.append('/home/name/my_name/numpy/local/lib/python2.7/site-packages/sklearn')
import sklearn
Unfortunately it did not work. As a result I got: ImportError: No module named sklearn
Does anybody know what I am doing wrong and if it is possible to reach the goal in the way I try?
When importing packages, you need to add the parent directory of the package to PYTHONPATH, not the package directory itself, so just change...
sys.path.append('/home/name/my_name/numpy/local/lib/python2.7/site-packages/sklearn')
...to...
sys.path.append('/home/name/my_name/numpy/local/lib/python2.7/site-packages')
...although it may not necessarily import correctly in Python 3.x.

Checking a Python module version at runtime

Many third-party Python modules have an attribute which holds the version information for the module (usually something like module.VERSION or module.__version__), however some do not.
Particular examples of such modules are libxslt and libxml2.
I need to check that the correct version of these modules are being used at runtime. Is there a way to do this?
A potential solution wold be to read in the source at runtime, hash it, and then compare it to the hash of the known version, but that's nasty.
Is there a better solutions?
Use pkg_resources. Anything installed from PyPI at least should have a version number.
>>> import pkg_resources
>>> pkg_resources.get_distribution("blogofile").version
'0.7.1'
If you're on python >=3.8 you can use a module from the built-in library for that. To check a package's version (in this example lxml) run:
>>> from importlib.metadata import version
>>> version('lxml')
'4.3.1'
This functionality has been ported to older versions of python (<3.8) as well, but you need to install a separate library first:
pip install importlib_metadata
and then to check a package's version (in this example lxml) run:
>>> from importlib_metadata import version
>>> version('lxml')
'4.3.1'
Keep in mind that this works only for packages installed from PyPI. Also, you must pass a package name as an argument to the version method, rather than a module name that this package provides (although they're usually the same).
I'd stay away from hashing. The version of libxslt being used might contain some type of patch that doesn't effect your use of it.
As an alternative, I'd like to suggest that you don't check at run time (don't know if that's a hard requirement or not). For the python stuff I write that has external dependencies (3rd party libraries), I write a script that users can run to check their python install to see if the appropriate versions of modules are installed.
For the modules that don't have a defined 'version' attribute, you can inspect the interfaces it contains (classes and methods) and see if they match the interface they expect. Then in the actual code that you're working on, assume that the 3rd party modules have the interface you expect.
Some ideas:
Try checking for functions that exist or don't exist in your needed versions.
If there are no function differences, inspect function arguments and signatures.
If you can't figure it out from function signatures, set up some stub calls at import time and check their behavior.
I found it quite unreliable to use the various tools available (including the best one pkg_resources mentioned by this other answer), as most of them do not cover all cases. For example
built-in modules
modules not installed but just added to the python path (by your IDE for example)
two versions of the same module available (one in python path superseding the one installed)
Since we needed a reliable way to get the version of any package, module or submodule, I ended up writing getversion. It is quite simple to use:
from getversion import get_module_version
import foo
version, details = get_module_version(foo)
See the documentation for details.
You can use
pip freeze
to see the installed packages in requirements format.
For modules which do not provide __version__ the following is ugly but works:
#!/usr/bin/env python3.6
import sys
import os
import subprocess
import re
sp = subprocess.run(["pip3", "show", "numpy"], stdout=subprocess.PIPE)
ver = sp.stdout.decode('utf-8').strip().split('\n')[1]
res = re.search('^Version:\ (.*)$', ver)
print(res.group(1))
or
#!/usr/bin/env python3.7
import sys
import os
import subprocess
import re
sp = subprocess.run(["pip3", "show", "numpy"], capture_output=True)
ver = sp.stdout.decode('utf-8').strip().split('\n')[1]
res = re.search('^Version:\ (.*)$', ver)
print(res.group(1))

Categories

Resources