ipython recognizes python module, but jupyter notebook does not - python

On linux I am managing Python via anaconda. However, as there was no good support for amplpy, I installed that via pip.
Now, When I am in ipython, I can do
In [1]: import amplpy
In [3]: amplpy
Out[3]: <module 'amplpy' from '/home/x/anaconda3/envs/myenv3/lib/python3.6/site-packages/amplpy/__init__.py'>
and similarly if I run from bare python. However, when I start jupyter notebook
(myenv3) x:yy$ jupyter notebook
and open a fresh python 3 sheet, I get
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-2-93ba964328c9> in <module>()
----> 1 import amplpy
ModuleNotFoundError: No module named 'amplpy'
sys.path gives me
['',
'/home/x/anaconda3/lib/python36.zip',
'/home/x/anaconda3/lib/python3.6',
'/home/x/anaconda3/lib/python3.6/lib-dynload',
'/home/x/anaconda3/lib/python3.6/site-packages',
'/home/x/anaconda3/lib/python3.6/site-packages/Sphinx-1.5.6-py3.6.egg',
'/home/x/anaconda3/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg',
'/home/x/anaconda3/lib/python3.6/site-packages/IPython/extensions',
'/home/x/.ipython']
Which is the wrong folder! ipython has /home/x/anaconda3/envs/myenv3/lib/... as path!
What can cause such inconsistency, and how do I fix it?

Most probably, you've installed the module to a different environment than the one your jupyter command is using (e.g. if there' no jupyter in your "myenv3", whatever else is found on the PATH will be used). See e.g. Keras import error Nadam for troubleshooting tips.

Related

"ImportError: DLL load failed while importing _base: The specified module could not be found." error when trying to import package rasterio [python]

I am trying to import the package rasterio into python and jupyter notebook, link to package: https://rasterio.readthedocs.io/en/latest/installation.html
Just to start off clean, I created a virtual environment, I will call projectenv
I access projectenv in my terminal via: projectenv\Scripts\activate
I then download all of the necessary dependencies for rasterio from this suggested website hosting the needed binary wheel files: https://www.lfd.uci.edu/~gohlke/pythonlibs/
I specifically install these packages/wheel files via pip:
GDAL-3.4.2-cp39-cp39-win_amd64.whl,
Fiona-1.8.21-cp39-cp39-win_amd64.whl,
Shapely-1.8.1.post1-cp39-cp39-win_amd64.whl,
pyproj-3.3.0-cp39-cp39-win_amd64.whl,
Rtree-1.0.0-cp39-cp39-win_amd64.whl
(Though I am not sure about Rtree being needed).
Everything installs fine. And to test I simply access python in my terminal from projectenv and trying importing rasterio via simply import rasterio, and it is successfully imported. However, this is just in my terminal window, and I want to import rasterio into a jupyter notebook.
And so I run ipython kernel install --user --name=projectenv to get a kernel for this virtual environment running in jupyter notebook. In my terminal, within projectenv, I simply run jupyter notebook to open jupyter notebook and then I open a new notebook in "projectenv". Now in the new jupyter notebook, I go to Kernel > Change kernel and select projectenv and the kernel is then ready. I then simply run import rasterio and I see this error message (full traceback included):
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_13964/1380003273.py in <module>
----> 1 import rasterio
~\AppData\Roaming\Python\Python39\site-packages\rasterio\__init__.py in <module>
15 from pathlib import Path
16
---> 17 from rasterio._base import gdal_version
18 from rasterio.drivers import driver_from_extension, is_blacklisted
19 from rasterio.dtypes import (
ImportError: DLL load failed while importing _base: The specified module could not be found.
How come this is happening if I was previously able to import rasterio in my terminal? I am trying to find a way to fix this, but it seems like rasterio was installed fine, but just cannot be imported into jupyter notebook for some reason. I am thinking perhaps I am not properly changing the kernel correctly in jupyter notebook.

Jupyter Notebook does not import any module

ModuleNotFoundError Traceback (most recent call last)
<ipython-input-2-c1d07d468637> in <module>
----> 1 import requests
2
ModuleNotFoundError: No module named 'requests'
I first installed a module and was wondering why it was not working.
Then I tried with modules that ought to be installed, like pandas and requests.
On all modules, I get the same issue.
Then I checked if the modules really are not installed, or if they are not in the proper folder
After that, I uninstalled and reinstalled anaconda
Nothing worked so far. I appreciate any help
Jupyter error message:
Pip installed modules:
I think you are installing your modules on a vitualenv and the Jupyter notebook is running outside the virtualenv.
This happened to me once.
Maybe you forgot to append the path to your modules to sys.path.
Example from within your notebook, if u want to import some selfwritten module from some relative location:
import sys
sys.path.append("../../../")
sys.path.append("../../")
# Rest of your code goes here, for example import $MODULE_NAME
Then you can import $MODULE_NAME (so, use the proper modulename of your desired module) iff. that module is in ../../ or in ../../../.
HTH. :-)

python newbie - how to import library

I installed the 'pandas' module and now I need to run it at my command line, but it is not available. How to make it available?
In [1]: import pandas as pd
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-af55e7023913> in <module>()
----> 1 import pandas as pd
ImportError: No module named 'pandas'
In [2]:
Your module can't be found in the current directory, in pythons site-packages folder or in any path defined in the $PYTHON_PATH environment variable.
If you installed pandas via pip install pandas and encounter an ImportError, you most likely have a problem due to multiple python distributions installed on your system.
Look carefully at the output of pip install pandas and examine the folders in its output. Find out which executable you are running when calling python via which python and look at the version numbers.
This is a very common problem btw. so prepare for a close of this question.

Import of matlab.engine works in IPython but not in Jupyter

I just installed the MATLAB Engine API for Python to my home directory using
$ python setup.py build --build-base=$HOME install --prefix=$HOME
However, when I try to import matlab.engine from a Jupyter notebook, I get the following error:
---------------------------------------------------------------------------
EnvironmentError Traceback (most recent call last)
<ipython-input-2-833daa01cd46> in <module>()
8 import numpy as np
9 import matplotlib.pyplot as plt
---> 10 import matlab.engine
11
12 from dopa_net.behavioral_experiments.analysis_code import behavior_analysis as ba
/home/despo/dbliss/lib/matlab/engine/__init__.py in <module>()
57 pythonengine = importlib.import_module("matlabengineforpython"+_PYTHONVERSION)
58 except:
---> 59 raise EnvironmentError('The installation of MATLAB Engine for Python is '
60 'corrupted. Please reinstall it or contact '
61 'MathWorks Technical Support for assistance.')
EnvironmentError: The installation of MATLAB Engine for Python is corrupted. Please reinstall it or contact MathWorks Technical Support for assistance.
Oddly, I don't get this error when I import matlab.engine from within an IPython shell in the same directory as the Jupyter notebook. Not only can I import the engine from the shell -- it works in the shell, which demonstrates to me that nothing is corrupted:
In [1]: import matlab.engine
In [2]: eng = matlab.engine.start_matlab()
In [3]: tf = eng.isprime(37)
In [4]: print tf
True
Any ideas what's going on?
N.B. I did install a version of the engine to my home directory earlier today, but I tried to (and thought I did) uninstall it. My guess is some part of it has been left behind and is causing trouble. But why is it causing trouble only for Jupyter, and not for the IPython shell? (And how would you guess I should get rid of it?)
Update
I tracked the error to the following line in the __init__.py file for the matlab.engine module:
importlib.import_module("matlabengineforpython2_7")
which does not raise an error in the IPython shell, but does raise the following error in Jupyter:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-4-831d184262c4> in <module>()
----> 1 importlib.import_module("matlabengineforpython2_7")
/usr/local/anaconda-2.4.0/lib/python2.7/importlib/__init__.pyc in import_module(name, package)
35 level += 1
36 name = _resolve_name(name[level:], package, level)
---> 37 __import__(name)
38 return sys.modules[name]
ImportError: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.15' not found (required by /srv/local/matlab-2015a/extern/engines/python/dist/matlab/engine/glnxa64/../../../../../../../bin/glnxa64/libmwfl.so)
I have no idea what's going on here.
The hypothesis that the IPython Shell and Jupyter Notebook don't have access to the same C libraries seems to have been correct. Why this is the case, I have no idea. The two were installed as part of the same package, use the same version of Python, etc.
Adding the following line to my .bashrc shell configuration file fixed the issue:
export LD_LIBRARY_PATH=XXX
where XXX is the directory containing the version of libstdc++.so.6 I want the Jupyter Notebook to use (and that the IPython Shell already uses).

IPython import error - pattern

I'm a newbie to IPython/Anaconda (for Windows 7), and for some reason, I'm getting an import error:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-6-484073d472a5> in <module>()
6 import matplotlib.pyplot as plt
7 import requests
----> 8 import pattern
ImportError: No module named pattern
Here's what I did to set up Anaconda/IPython:
Installed Anaconda. I already have a version of python on this computer. The Anaconda distribution was installed to C:\Users\MyName, while the regular python distribution (2.7) was installed to C:\Python27.
When I tried to import some packages (sklearn, pandas, requests, pattern), for some reason those packages just wouldn't import so I installed them manually (downloading them to a local folder on my desktop and running $ python setup.py install, and after that it all seemed to work).
I can run IPython notebooks fine. However, when I try to import pattern specifically, I get that error (which I don't get for sklearn or any of the other packages I installed manually). Printing my sys path from the terminal (import sys print sys.path) and IPython notebook gives:
Terminal:
['C:\\Users\\MyName\\Desktop\\Data Science\\cs109\\Labs', 'C:\\python27\\lib\\site-
packages\\mrjob-0.4.2_dev-py2.7.egg', 'C:\\python27\\lib\\site-
packages\\simplejson-3.3.1-
py2.7.egg', 'C:\\python27\\lib\\site-packages\\pyyaml-3.10-py2.7-win32.egg',
'C:\\python27\\lib\\site-packages\\boto-2.18.0-py2.7.egg',
'C:\\python27\\lib\\site-packages\\requests-2.0.1-py2.7.egg',
'C:\\Windows\\system32\\python27.zip',
'C:\\Python27\\lib\\site-packages\\Orange\\orng', 'C:\\python27\\DLLs',
'C:\\python27\\lib', 'C:\\python27\\lib\\plat-win', 'C:\\python27\\lib\\lib-tk',
'C:\\python27', 'C:\\python27\\lib\\site-packages', 'C:\\python27\\lib\\site-
packages\\PIL', 'C:\\python27\\lib\\site-packages\\win32',
'C:\\python27\\lib\\site-
packages\\win32\\lib', 'C:\\python27\\lib\\site-packages\\Pythonwin']
IPython notebook:
['', 'C:\\Users\\MyName\\Anaconda\\python27.zip', 'C:\\Python27\\lib\\site-
packages\\Orange\\orng', 'C:\\Users\\MyName\\Anaconda\\DLLs',
'C:\\Users\\MyName\\Anaconda\\lib', 'C:\\Users\\MyName\\Anaconda\\lib\\plat-win',
'C:\\Users\\MyName\\Anaconda\\lib\\lib-tk', 'C:\\Users\\MyName\\Anaconda',
'C:\\Users\\MyName\\Anaconda\\lib\\site-packages',
'C:\\Users\\MyName\\Anaconda\\lib\\site-packages\\PIL',
'C:\\Users\\MyName\\Anaconda\\lib\\site-packages\\win32',
'C:\\Users\\MyName\\Anaconda\\lib\\site-packages\\win32\\lib',
'C:\\Users\\MyName\\Anaconda\\lib\\site-packages\\Pythonwin',
'C:\\Users\\MyName\\Anaconda\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg-
info', 'C:\\Users\\MyName\\Anaconda\\lib\\site-packages\\IPython\\extensions']
If I do
import pattern
print pattern
It shows that pattern is located at:
C:\python27\lib\site-packages\pattern\__init.pyc
Any thoughts as to why IPython can't import pattern?
I struggled with this - I kept getting an error saying package couldn't be found.
Running below in command prompt worked for me.
conda install -c asmeurer pattern=2.5
On windows, open cmd.exe and type:
conda install pattern
This should do it ;)
Sometimes this happens when you have multiple versions of Python/Anaconda installed on your machine. As the Pattern package does not run on Python 3.4, you need to launch IPython Notebook from the Anaconda server that runs Python 2.7.
So the first step is to make sure that you install the Pattern package using pip in the version of Anaconda that runs Python 2.7.
For example, C:\Users\MyName\Anaconda\Scripts\pip install pattern
The second step is to make sure that you run ipython notebook from the correct path.
For example, C:\Users\MyName\Anaconda\Scripts\ipython notebook
That should do it.

Categories

Resources