Scipy cannot be imported in Jupyter Notebook - python-3.x

I'm trying to use scipy in a jupyter notebook and it says I have it installed, but when I try to import it, it gives me the following error.
Any help would be great. thank you.

TLDR: try this
import sys
!{sys.executable} -m pip install scipy
A bit more info:
Jupyter notebooks are able to work with multiple kernels, which are essentially pointers to the Python (or other language) executable that the notebook uses. In a Python kernel, you can figure out which one is being used by typing
import sys
print(sys.executable)
When you run a bash command in the notebook, like !pip install scipy, that uses the bash environment that was active when you launched the notebook which is not necessarily associated with the Python kernel you are using. That means that it may be installing scipy in a different Python location. You can figure out which Python your shell points to by running !which python. If this doesn't match, then !pip install will not be installing in the right place.
You can fix this by explicitly telling the bash prompt which Python/pip you want to use. For example, this should do the trick:
import sys
!{sys.executable} -m pip install scipy
This runs the pip version associated with your executable, and installs scipy with that. For some more details on what's happening behind the scenes, check out this answer.

The pip that you execute is using a shell that may (and that is probably the case here) have a different python interpreter than the one of the jupyter notebook!
Jake VanderPlas to the rescue https://twitter.com/jakevdp/status/841791667472543745
Do the following in the notebook
import sys
sys.executable
Depending on the output of "sys.executable", adjust your command-line call, still in the notebook
!/usr/bin/python3 -m pip install scipy
But as you have scipy installed, the issue is to understand why your anaconda scipy is not found. Do you have a default virtualenv for all your python use? How do you start the notebook?

Related

I get "syntax error" while installing numpy [duplicate]

I'm trying to use pip to install a package. I try to run pip install from the Python shell, but I get a SyntaxError. Why do I get this error? How do I use pip to install the package?
>>> pip install selenium
^
SyntaxError: invalid syntax
pip is run from the command line, not the Python interpreter. It is a program that installs modules, so you can use them from Python. Once you have installed the module, then you can open the Python shell and do import selenium.
The Python shell is not a command line, it is an interactive interpreter. You type Python code into it, not commands.
Use the command line, not the Python shell (DOS, PowerShell in Windows).
C:\Program Files\Python2.7\Scripts> pip install XYZ
If you installed Python into your PATH using the latest installers, you don't need to be in that folder to run pip
Terminal in Mac or Linux
$ pip install XYZ
As #sinoroc suggested correct way of installing a package via pip is using separate process since pip may cause closing a thread or may require a restart of interpreter to load new installed package so this is the right way of using the API: subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'SomeProject']) but since Python allows to access internal API and you know what you're using the API for you may want to use internal API anyway eg. if you're building own GUI package manager with alternative resourcess like https://www.lfd.uci.edu/~gohlke/pythonlibs/
Following soulution is OUT OF DATE, instead of downvoting suggest updates. see https://github.com/pypa/pip/issues/7498 for reference.
UPDATE: Since pip version 10.x there is no more get_installed_distributions() or main method under import pip instead use import pip._internal as pip.
UPDATE ca. v.18 get_installed_distributions() has been removed. Instead you may use generator freeze like this:
from pip._internal.operations.freeze import freeze
print([package for package in freeze()])
# eg output ['pip==19.0.3']
If you want to use pip inside the Python interpreter, try this:
import pip
package_names=['selenium', 'requests'] #packages to install
pip.main(['install'] + package_names + ['--upgrade'])
# --upgrade to install or update existing packages
If you need to update every installed package, use following:
import pip
for i in pip.get_installed_distributions():
pip.main(['install', i.key, '--upgrade'])
If you want to stop installing other packages if any installation fails, use it in one single pip.main([]) call:
import pip
package_names = [i.key for i in pip.get_installed_distributions()]
pip.main(['install'] + package_names + ['--upgrade'])
Note: When you install from list in file with -r / --requirement parameter you do NOT need open() function.
pip.main(['install', '-r', 'filename'])
Warning: Some parameters as simple --help may cause python interpreter to stop.
Curiosity: By using pip.exe you actually use python interpreter and pip module anyway. If you unpack pip.exe or pip3.exe regardless it's python 2.x or 3.x, inside is the SAME single file __main__.py:
# -*- coding: utf-8 -*-
import re
import sys
from pip import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(main())
To run pip in Python 3.x, just follow the instructions on Python's page: Installing Python Modules.
python -m pip install SomePackage
Note that this is run from the command line and not the python shell (the reason for syntax error in the original question).
I installed python and when I run pip command it used to throw me an error like shown in pic below.
Make Sure pip path is added in environmental variables. For me, the python and pip installation path is::
Python: C:\Users\fhhz\AppData\Local\Programs\Python\Python38\
pip: C:\Users\fhhz\AppData\Local\Programs\Python\Python38\Scripts
Both these paths were added to path in environmental variables.
Now Open a new cmd window and type pip, you should be seeing a screen as below.
Now type pip install <<package-name>>. Here I'm installing package spyder so my command line statement will be as pip install spyder and here goes my running screen..
and I hope we are done with this!!
you need to type it in cmd not in the IDLE. becuse IDLE is not an command prompt if you want to install something from IDLE type this
>>>from pip.__main__ import _main as main
>>>main(#args splitted by space in list example:['install', 'requests'])
this is calling pip like pip <commands> in terminal. The commands will be seperated by spaces that you are doing there to.
If you are doing it from command line,
try -
python -m pip install selenium
or (for Python3 and above)
python3 -m pip install selenium

Module not found when I tried to import pyPDF2

My python version is 3.6. I am able to install the pyPDF2.
Ran pip install pyPDF2 successfully.
Ran pip list, it shows up as 1.26.0
My environment is not base, but I set up an environment as pytorch. pyPDF2 is installed successfully in this environment.
It pops error when I tried to import it. (typo fixed from the original post)
Your import pyPDF is incorrect. Instead try import PyPDF2.
Is also possible your pip does not match your python (this can be the case when multiple python versions are installed at the same time). Try running python -m pip list to confirm what exactly is installed.

import ortools in mac

I am trying to run some code using ortools on a python environment. I did not have troubles on a windows machine but I am having problems on mac (10.12.6). if in my virtual environment I run
pip freeze
or
conda list
ortools appears in my list of installed packages. But if I try to use it
ipython
from ortools.linear_solver import pywrapplp
I get an error saying that there's no module named ortools. If I go to
mac/anaconda3/envs/nameenv/lib/python3.6/site-packages I do have a folder called ortools with some python files including pywrapplp. Do you know what I am doing wrong ?
EDIT
following request from coments:
import os
os.getcwd()
returns '/Users/imac'
which ipython
/anaconda3/bin/ipython
Installing ortools is a bit of a headache. It was some days ago, I think I finally made it with
easy_install ortools
I think it is a problem with the path. I guess because I did not install it with conda it does not find the package. I got around writting:
sys.path.append('/anaconda3/envs/env_name/lib/python3.6/site-packages/')
at the begining of my ipynb. That way I can run ortools.
You could have several python interpreter installed (python2 and python3)
So if you want to use it with ipython, which seems to be bind on python3 in your case.
First check if you have pypi package ortools installed
ipython -m pip show ortools
If you got an error it means the package is not installed.
so you can easily install it using:
ipython -m pip install --user ortools
note: We provide Pypi ortools package (64 bits) for Manylinux, Windows and MacOS.
You can also rebuild it from source https://developers.google.com/optimization/introduction/installing/source

Unable to import pandas on Jupyter Notebook

I've installed both pandas and jupyter notebook on my virtualenvs(python3).
When I check which jupyter, it's correctly refer to my envs. However, I can't import pandas when I run it in the browser.
When I run the ipython, I can import the pandas module though...
ImportError: No module named 'pandas'
Any help?
Jupyter is built with the Anaconda Python distribution (and the conda environment manager) in mind. Although some work has been done to remediate this issue, using jupyter with virtualenv is discouraged.
In this case it sounds like jupyter is using your default (root) Python environment, because it's not aware of your virtualenv one. See here for details on how to fix that.
If you land here looking for the answer, this is it (taken from the question #Aleksey Bilogur refers):
source activate ENVNAME
pip install ipykernel
python -m ipykernel install --user --name ENVNAME --display-name "Python (whatever you want to call it)"
It is really important that you install ipykernel in your virtualenv and that you launch jupyter notebook also from you ENVNAME.
Once you've done that, check if the path to your ENVNAME appears when executing from your notebook:
import sys
sys.path
If it does not, something went wrong and it won't work.

I installed opencv3 with conda, yet I can only import cv2... is this correct?

I am anaconda on a mac os x Yosemite, running python 3.5. I used the following command from anaconda's website
conda install -c menpo opencv3=3.1.0
The terminal read out nothing to do with opencv2.
However, in the ipython console I am unable to import cv3. I get,
conda install -c menpo opencv3=3.1.0
Yet.... interestingly enough I able import cv2. I have read a bit that opencv can be a tricky and wily beast to get working...
Also anaconda specifically says that I have only the opencv3 package.
So, I am wondering... even though I am using opencv3 maybe for some reason it still uses cv2 as the package name to import?
Thanks for any insight!
Apparently, it is just that simple... import cv2 even if you have opencv3.x.
Thank you Miki.

Resources