iPython (python 2) - ImportError: No module named model_selection - python

iPython Notebook
Python 2
Complaining about this line:
from sklearn.model_selection import train_test_split
Why isn't model selection working?

In order to remedy this issue, you need to first find out if you are importing the actual sklearn package, and not just some script with the name sklearn.py saved somewhere in your working directory. The way Python imports modules is somewhat similar to the way it finds variables in its namespace (Local, Enclosed, Global, Built-in). In this case, Python will start importing module by first looking in the current directory and then the site-packages. If it looks in the current working directory and finds a python script with the same name as the module you are trying to import, then it will import that script instead of the actual module.
You can usually find out whether or not the actual module is imported by checking its __file__ or __path__ attribute:
import sklearn
print(sklearn.__file__)
print(sklearn.__path__)
Reviewing the output of those print statements will tell you whether the imported package is the module you are after, or just some script lying somewhere in your working directory. If, in case, the output does not point to the site-packages of your Python version then you have imported some script somewhere that's not the module itself. Your quick fix would be to exit the console first, rename the .py script and its compiled version (the .pyc file) and go back to your console and try again.
However, if the output points to your python version's site-packages, then there is something wrong with how the package was installed in the first place. In which case, you will probably need to update or reinstall it.
Particular to this, it turns out that the issue is with the version of sklearn you are using; because the model_selection module in sklearn is available in versions 0.18+. If you're using a version number (sklearn.__version__) lower than 0.18, then you will have to use the old cross_validation module instead of the model_selection module:
from sklearn.cross_validation import train_test_split
You can also just upgrade to the latest version of the package with your preferred package management system.
I hope this is helpful.

Related

Module imports, but doesn't have any attributes

I am trying to import a Python module (fiasco). I cloned it from GitHub and everything appeared to be working fine. Importing it works, but when I try to type, for example iron = fiasco.Element('iron', [1e4, 1e6, 1e8]*u.K), I get the error module 'fiasco' has no attribute 'Element'. I am using Spyder's iPython console. This also fails if I start iPython from the terminal, but works if I start python3 from the terminal.
I had done this on two different computers - on one, it worked at first, but started giving me the same error after I restarted the kernel. On the other, it never worked at all.
If it helps: after importing, I tried typing fiasco. When I did this on the computer where it originally worked, the output was <module 'fiasco' from '/Users/shirman/fiasco/fiasco/__init__.py'>. Now, and on the computer it never worked on, it just says <module 'fiasco' (namespace)>. So maybe this has something to do with paths?
Addition: sys.path points to /Users/shirman, and several paths within /Users/shirman/anaconda3. The fiasco folder is in /Users/shirman.
You have inadvertently created a namespace package due to your sys.path setting. Namespace packages are directories without an __init__.py in the Python search path and allow loading of submodules or -packages from different paths (e.g. path1/foo/a.py and path2/foo/b.py can be imported as foo.a and foo.b).
The problem is that import fiasco finds /Users/shirman/fiasco first and imports it as a namespace package. If you set sys.path such that /Users/shirman/fiasco comes before /Users/shirman, the importer finds the actual package /Users/shirman/fiasco/fiasco first.
Namespace packages are a Python 3.3 feature, so either the other machine had a different sys.path setting, a really old Python 3 installation, or you were using Python 2.

Why is python unable to detect notification module in plyer?

I can't get this simple statement to work:
from plyer import notification
getting:
ImportError: cannot import name 'notification'
the import statement is correct and is used the same way in examples.
I couldn't find any special instructions to use this library so I'm assuming there aren't any.
I installed plyer using pip and it installed successfully. verified the files are in place. I tried using python 3.5 and 3.6, same result.
It seems the package is recognized but just the module isnt found?
Would appreciate some insight :)
A common cause for this kind of problem is having a script or module by the same name in a location that comes before the expected module or package's location in sys.path so it gets imported instead of the expected module or package.
The simple way to sort this out is to add this simple line before:
import plyer; print(plyer);
and check the result which will diplay the path of whatever named plyer was first found. Chances are it's a script in your current working directory...

importing whole python module doesn't allow using submodules

My question is specific to scikit-learn python module, but I had similar issues with matplotlib as well.
When I want to use sklearn, if I just do 'import sklearn' and then call whatever submodule I need, like ' sklearn.preprocessing.scale()', I get an error
"AttributeError: 'module' object has no attribute 'preprocessing'"
On the other hand, when I do 'from sklearn import preprocessing'
and then use 'preprocessing.scale()' it works normally.
When I use other modules like Numpy, it is sufficient to just 'import numpy' and it works well.
Therefore, I would like to ask if anyone can tell me why is this happening and if I am doing something wrong?
Thanks.
A python package is defined in the __init__.py file inside the directory.
This file determines whether or not submodules are include.
When you do import sklearn python finds the file sklearn/__init__.py and executes it to create the sklearn module. This object is the bound to the name sklearn. Submodules are not implicitly imported by the interpreter.
However when doing from sklearn import preprocessing python will first load the sklearn module as before. Then it will check if preprocessing is an attribute of that module (e.g. a function), and if not it will look for the file sklearn/preprocessing.py and improt that module too.
It happens that numpy does something like the following in its __init__.py file:
import .random
Thus when importing numpy as import numpy the execution of that module triggers the importing of numpy.random which is then added as an attribute.
This is useful because sometimes you want to use only part of a package and loading all of it could take a significant amount of time. For example importing numpy does take something like half a second. This is time wasted if you only need a very small subset of its functionality.
You may be interested in reading the documentation for packages.
Numpy conveniently imports its submodules in its __init__.py file and adds them to __all__. There's not much you can do about it when using a library - it either does it or not. sklearn apparently doesn't.

how python import packages with multiple copies

I was believing that when importing a package, it would search from sys.path and use the first hit for import. However, it seems not true:
import mpl_toolkits
print(mpl_toolkits.__path__)
And it outputs:
['/Library/Python/2.7/site-packages/matplotlib-1.5.0-py2.7-macosx-10.11-intel.egg/mpl_toolkits', '/usr/local/lib/python2.7/site-packages/mpl_toolkits']
Can someone please explain to me how exactly python looks for packages if it is installed multiple times in the machine (in different location searchable by sys.path)? Or a pointer to relevant reference would be good.
when you import a module, python uses PYTHON PATH (system variable that contains a list of folders) and loops to search for importable module.
Python will test if it is a package (folder containing init.py) or a module (*.py). it will stop on first module found if no module is found python raises an import error

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.

Categories

Resources