8

I've successfully installed Python 2.7 and Anaconda but when i try to import a library i get always this error:

>>> import scipy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named scipy

I've set up the PYTHONHOME to C:\Python27 and PYTHONPATH to C:\Python27\Lib.

EDIT : content of PATH

In my $PATH variable i have C:\Users\Mattia\Anaconda2, C:\Users\Mattia\Anaconda2\Scripts and C:\Users\Mattia\Anaconda2\Library\bin.

Do i have to set any other env veriables?

tia_0
  • 412
  • 1
  • 3
  • 11

5 Answers5

7

The problem is that you should not have either PYTHONPATH or PYTHONHOME set. They are both pointing to a non-Continuum version of Anaconda, I believe. Anaconda will install (by default) into a directory called Anaconda, either at C:\Anaconda or at C:\Users\USERNAME\Anaconda (IIRC). It is generally recommended that you should not ever set PYTHONPATH or PYTHONHOME, except as a last resort, exactly because of these kinds of problems.

You can see which Python interpreter you're running by doing:

>>> import sys
>>> sys.executable

And then you can see what directories are ending up in your Python library path (where import statements will look for packages, such as scipy and numpy) by doing one of the following:

>>> import sys
>>> sys.path

or the more readable version:

>>> import sys
>>> for p in sys.path:
...    print p
IanSR
  • 9,898
  • 4
  • 14
  • 15
  • Can you please elaborate? I printed the path, and I see it includes the directory the module is installed in yet it says Module not found! – Atheer Mar 30 '21 at 15:03
  • This helped, I needed to do `unset PYTHONPATH` then everything worked fine for me using the conda base environment. I tried to run a custom installed python module. – phi Apr 04 '22 at 09:06
2

As pointed out by @Mr.F the error was given by the presence of the PYTHONPATH and PYTHONHOME. Deleting them i was able to use the Anaconda version of python.

tia_0
  • 412
  • 1
  • 3
  • 11
  • Ahh! This took me FOREVER. I had to delete `export PYTHONPATH=/usr/lib/python2.7:$PYTHONPATH` from my `~/.bash_profile` (for osx) – Jacksonkr Jun 16 '17 at 13:42
2

If you have module not found error, you may need to launch python from anaconda terminal using "python" instead of the shortened "py". I had my module installed properly, but spent forever trying to fix it because of this. Apparently py does not launch the anaconda activated or anaconda base environment, but launches another version of python.

Dharman
  • 30,962
  • 25
  • 85
  • 135
mberna
  • 313
  • 5
  • 18
0

Use $ conda install package_name instead

0

When using anaconda environment other than base, this issue is mostly due to kernel. See this Youtube video that best explains it

The solution is in the video. You can also check the Ipython documentation here for more details.

Quick solution:

  • Make user that you identify the various python installations on your machine and that you are the intended one in anaconda. You can use which -a python. You can also check your path to ensure there aren't various python bin folders in the path.
  • Use jupyter kernelspec list to identify the various kernels in anaconda.
  • Preferably use different kernels for different environment.
  • In my case, I activated the new enviroment using conda active myenv, then install a kernel for myenv using python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
  • Note that the python executable should be the right one - preferable from <ANACONDA_HOME>/bin/

This solved the issue for me. Credit to the links attached to this post.

okmich
  • 700
  • 5
  • 11