I cant import sklearn in Anaconda 2.3.0 - python

I have the newest Anaconda 2.3 on a non-network-Linux-machine. Everything works fine besides
import sklearn
I then get back
----> 1 import sklearn
sklearn.py in <module>()
ValueError: Attempted relative import in non-package
I have to go into the /pkgs folder in anaconda and into scikit-learn to import from there. This works and whats even weirder, when I type
import
and the tabulator to show possible packages, sklearn is shown.

Might be that you're using another Python installation on your computer.
For instance, I have Python in my Anaconda environement and the other one by default. If execute: ~/anaconda/bin/python I can import sklearn without problems.
On the contrary if I execute /usr/bin/python and then try to export it I get the error message since I installed sklearn from Anaconda.
Check your PATH variable with typing $PATH on your terminal and check the order, if Anaconda is not the first one (or it's not there) type: export PATH="/path/to/anaconda/bin:$PATH"
I hope this works.

Related

VSCode - import errors and interpreter errors

I have several imports in my current code:
from flask import Flask
import datetime as dt
import numpy as np
import pandas as pd
When I run this in VSCODE I am getting this error:
However, running this in jupyter notebook has no problems. When I looked online it said to use python interpreter but when I go to do that I get this error:
And another error:
Anaconda prompt says modules/packages are installed but when I run pip install in default windows terminal it says pip has no module:
Delete and reinstall Python extensions according to the same problem on github.
The second problem is related to the location of your Python interpreter. You need to choose the correct interpreter. I still recommend using one Python version in one environment. You can use other Python versions in virtual environment, so it won't lead to confusion.

Separate versions of Python on same computer won't import same modules

I've been coding in Python 3.7.2 as my usual, but an API that I really want for my code only supports up to 3.6 (and does not support 2.7). I downloaded Python 3.6.4 to my computer, which also downloads a separate instance of the IDLE (not a problem). If I try to import something like numpy to my code in 3.7 (ex. import numpy as np) then it works as expected. However, if I do the same in the 3.6 IDLE I get:
Traceback (most recent call last):
File "", line 1, in
import numpy as np
ModuleNotFoundError: No module named 'numpy'
I think that it's a path problem but I'm unsure on how to fix it, and I can't find a solution to this problem elsewhere. Any help is appreciated, thanks.
Step 1: Get the location of the python executable from IDLE
import sys
print(sys.executable) # e.g. /Users/jk/.../bin/python
Step 2: Run the pip in the same folder as the one returned above.
/Users/jk/.../bin/pip install numpy
P.S. It's better to maintain libraries independently for each distribution, or even better use virtualenv or conda to create environments.
Try to install numpy specifically for python3.6:
python3.6 -m pip install numpy

ImportError: cannot import name 'moduleTNC' - python

I'm having a problem in python when I try to import linear_model from sklearn library: from sklearn import linear_model. I have just installed it via pip simply in this way: pip install sklearn. I know that to avoid this error suffices uninstall and reinstall sklearn, but it didn't work. I also installed it via conda but opening the idle (is that correct?) it gives the same error.
How to avoid it?
NB: If I use jupyter from conda it works well as it should.
I had this same problem and resolved it with:
conda remove scipy scikit-learn -y
conda install scipy scikit-learn -y
I saw it here, where many other people said it solved their problems too.
With regards to the following error:
ImportError: cannot import name 'moduleTNC'
It can be solved by renaming moduletnc.cp36-win_amd64.pyd with moduleTNC.cp36-win_amd64.pyd in:
AppData\Roaming\Python\Python36\site-packages\scipy\optimize
I can't mark as possible duplicate, so I'm just pasting here. If that's a wrong behaviour, I'm sorry:
Import module works in Jupyter notebook but not in IDLE
The reason is that your pip/conda installed library paths are not accessible by python IDLE. You have to add those library paths to your environment variable(PATH). To do this open my computer > properties > advanced system settings > system.
Under environment variables look for PATH and at the end add the location of installed libraries. Refer this for more details on how to add locations in path variable. Once you do these you will be able to import the libraries. In order to know what locations python searches for libraries you can use
import sys
print sys.path
This will give you a list of locations where python searches for libraries. Once you edit the PATH variable those locations will be reflected here.
Refer this also in order to know how to add python library path.
Note: The tutorial is a reference on how to edit PATH variable. I encourage you to find the location of installed libraries and follow the steps to edit the same.

Packages not working, using Anaconda

I have installed Anaconda for Windows. It's on my work PC, so I chose the option "Just for Me" as I don't have admin rights.
Anaconda is installed on the following directory:
c:\Users\huf069\AppData\Local\Continuum\Anaconda
The Windows installer has added this directory (+ the Anaconda\Scripts directory) to the System path.
I can launch Python but trying to run
x = randn(100,100)
gives me a Name Error: name 'randn' is not defined,
whereas, as I understood, this command should work when using Anaconda, as the numpy package is included.
it works fine if I do:
import numpy
numpy.random.randn(100,100)
Anyone understand what could be happening ?
I can launch Python, but trying to run x = randn(100,100) gives me a Name Error: name 'randn' is not defined, whereas, as I understood, this command should work when using Anaconda, as the numpy package is included
The Anaconda distribution comes with the numpy package included, but still you'll need to import the package. If you want to use the randn() function without having to call the complete name, you can import it to your local namespace:
from numpy.random import randn
x = randn(100,100)
Otherwise, the call numpy.random.randn is your way to go.
You might want tot take a look at the Modules section of the Python Tutorial.

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