Failure to import numpy in Jupyter notebook - numpy

I am new to iPython/Jupyter. Python skills limited, but learning. I am trying to import numpy as np and get the following:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-4ee716103900> in <module>()
----> 1 import numpy as np
/Users/jmmiii/Library/Enthought/Canopy_32bit/User/lib/python2.7/site-packages/numpy/__init__.py in <module>()
166 return loader(*packages, **options)
167
--> 168 from . import add_newdocs
169 __all__ = ['add_newdocs', 'ModuleDeprecationWarning']
170
/Users/jmmiii/Library/Enthought/Canopy_32bit/User/lib/python2.7/site-packages/numpy/add_newdocs.py in <module>()
11 from __future__ import division, absolute_import, print_function
12
---> 13 from numpy.lib import add_newdoc
14
15 ###############################################################################
/Users/jmmiii/Library/Enthought/Canopy_32bit/User/lib/python2.7/site-packages/numpy/lib/__init__.py in <module>()
6 from numpy.version import version as __version__
7
----> 8 from .type_check import *
9 from .index_tricks import *
10 from .function_base import *
/Users/jmmiii/Library/Enthought/Canopy_32bit/User/lib/python2.7/site-packages/numpy/lib/type_check.py in <module>()
9 'common_type']
10
---> 11 import numpy.core.numeric as _nx
12 from numpy.core.numeric import asarray, asanyarray, array, isnan, \
13 obj2sctype, zeros
/Users/jmmiii/Library/Enthought/Canopy_32bit/User/lib/python2.7/site-packages/numpy/core/__init__.py in <module>()
4 from numpy.version import version as __version__
5
----> 6 from . import multiarray
7 from . import umath
8 from . import _internal # for freeze programs
ImportError: dlopen(/Users/jmmiii/Library/Enthought/Canopy_32bit/User/lib/python2.7/site-packages/numpy/core/multiarray.so, 2): no suitable image found. Did find:
/Users/jmmiii/Library/Enthought/Canopy_32bit/User/lib/python2.7/site-packages/numpy/core/multiarray.so: mach-o, but wrong architecture
I have several python installs on my Mac, which has Yosemite, including Canopy and Anaconda. I want my Jupyter notebook to use the Anaconda install including all the modules, libraries, etc. associated with it. It seems however that jupyter is targeting Canopy instead. Thus, I think my problem might stem from the wrong linkage.
QUESTION 1: Does my conclusion hold water? If not, what might I be missing?
QUESTION 2: How can I direct/link jupyter with Anaconda and not with Canopy so that I import everything from anaconda only?
Thanks for everyone's help!

You can either set the PATH to execute python commands from the ~/anaconda/bin directory by prepending it to your .bah_profile by running the following command.
export PATH="/Users/jmmiii/anaconda/bin:$PATH"
OR, you can create an alias for the command by editing your ~/.bash_profile and adding:
alias jupyter-notebook="/Users/jmmiii/anaconda/bin/jupyter-notebook"

Related

ImportError: cannot import name '_backports' from 'matplotlib.cbook'

---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_22516\254480426.py in <module>
2 import matplotlib.pyplot as plt
3 from mpl_toolkits.mplot3d import Axes3D, art3d # NOQA
----> 4 from matplotlib.cbook import _backports
5 from collections import defaultdict
6 import types
ImportError: cannot import name '_backports' from 'matplotlib.cbook' (C:\Users\saidt\anaconda3\envs\tensorflow\lib\site-packages\matplotlib\cbook\__init__.py)
I cannot solve this problem. I tried several ways advised on other posts (uninstalled, reinstalled, changed the version of matplotlib) but still have this issue. I even checked matplotlib.cbook (https://matplotlib.org/stable/api/cbook_api.html) but there is no _backports. I wonder where to find the solution. If someone could help, I would really appreciate it.

ModuleNotFoundError: No module named 'numpy.random.bit_generator' while importing sklearn

I installed opencv, tensorflow and other tools on my Macbook M1 air by watching this tutorial:
After installing, opencv and tensorflow works fine but when I try to import sklearn the mentioned error occurs. Here is the Error:
File ~/miniforge3/envs/ml/lib/python3.8/site-packages/scipy/stats/distributions.py:11, in <module>
8 from ._distn_infrastructure import (rv_discrete, rv_continuous, rv_frozen)
10 from . import _continuous_distns
---> 11 from . import _discrete_distns
13 from ._continuous_distns import *
14 from ._discrete_distns import *
File ~/miniforge3/envs/ml/lib/python3.8/site-packages/scipy/stats/_discrete_distns.py:21, in <module>
17 from ._distn_infrastructure import (
18 rv_discrete, _ncx2_pdf, _ncx2_cdf, get_distribution_names,
19 _check_shape)
20 import scipy.stats._boost as _boost
---> 21 from ._biasedurn import (_PyFishersNCHypergeometric,
22 _PyWalleniusNCHypergeometric,
23 _PyStochasticLib3)
25 class binom_gen(rv_discrete):
26 r"""A binomial discrete random variable.
27
28 %(before_notes)s
(...)
51
52 """
File _biasedurn.pyx:1, in init scipy.stats._biasedurn()
ModuleNotFoundError: No module named 'numpy.random.bit_generator'
The tutorial I was following suggested the exact version of numpy and python. I looked some places for help and they suggested updating the numpy. I'm not sure whether I should do that because that may break other libraries like OpenCV.
versions:
python 3.8.6
numpy 1.18.5
scikit-learn 1.1.1
scipy 1.8.1

ImportError: cannot import name 'get_versions' from 'matplotlib._version'

When doing my imports:
import cv2
from matplotlib import pyplot as plt
import numpy as np
import opencv_wrapper as cvw
I get:
ImportError Traceback (most recent call last)
/var/folders/bw/fb7g3vhj2ln41zrg3v5ynnn40000gn/T/ipykernel_96862/883562467.py in <module>
1 # import the necessary packages
2 import cv2
----> 3 from matplotlib import pyplot as plt
4 import numpy as np
5 import opencv_wrapper as cvw
/opt/anaconda3/envs/py37/lib/python3.7/site-packages/matplotlib/__init__.py in <module>
114 # Get the version from the _version.py versioneer file. For a git checkout,
115 # this is computed based on the number of commits since the last tag.
--> 116 from ._version import get_versions
117 __version__ = str(get_versions()['version'])
118 del get_versions
ImportError: cannot import name 'get_versions' from 'matplotlib._version' (/opt/anaconda3/envs/py37/lib/python3.7/site-packages/matplotlib/_version.py)
I'm using:
matplotlib 3.4.3
numpy 1.16.2
opencv-python 4.0.0.21
opencv-wrapper 0.2.3
python 3.7.13
Due to the opencv wrapper these are the versions I need to use.
I've tried to uninstall and reinstall but the error persists. Any help will be appreciated.

ImportError: cannot import name 'hf_bucket_url' in HuggingFace Transformers

So I installed the latest version of transformers on Google Colab
!pip install transformers
When trying to invoke the conversion file using
!python /usr/local/lib/python3.6/dist-packages/transformers/convert_pytorch_checkpoint_to_tf2.py .py --help
Or trying to use
from transformers.file_utils import hf_bucket_url. // works
from transformers.convert_pytorch_checkpoint_to_tf2 import *. // fails
convert_pytorch_checkpoint_to_tf("gpt2", pytorch_file, config_file, tf_file).
I get this error
ImportError Traceback (most recent call last)
<ipython-input-3-dadaf83ecea0> in <module>()
1 from transformers.file_utils import hf_bucket_url
----> 2 from transformers.convert_pytorch_checkpoint_to_tf2 import *
3
4 convert_pytorch_checkpoint_to_tf("gpt2", pytorch_file, config_file, tf_file)
/usr/local/lib/python3.6/dist-packages/transformers/convert_pytorch_checkpoint_to_tf2.py in <module>()
20 import os
21
---> 22 from transformers import (
23 ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP,
24 BERT_PRETRAINED_CONFIG_ARCHIVE_MAP,
ImportError: cannot import name 'hf_bucket_url'
What's going on?
It turns out to be a bug. This PR solves the issue by importing the function hf_bucket_url properly.

I am not able to import statsmodel.api into kaggle kernel

I updated the statsmodels package in the kaggle kernel successfully using the code:
!pip install statsmodels --upgrade
This gave me the version of (0.10.0). However when I try to import statsmodels.api it giving me an error.
import statsmodels.api as sm
The error I am getting is:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-7-3b8b7e2c2e57> in <module>
8 import matplotlib.pyplot as plt
9 from sklearn.preprocessing import Normalizer
---> 10 import statsmodels.api as sm
11
12 # Input data files are available in the "../input/" directory.
/opt/conda/lib/python3.6/site-packages/statsmodels/api.py in <module>
16 from . import robust
17 from .robust.robust_linear_model import RLM
---> 18 from .discrete.discrete_model import (Poisson, Logit, Probit,
19 MNLogit, NegativeBinomial,
20 GeneralizedPoisson,
/opt/conda/lib/python3.6/site-packages/statsmodels/discrete/discrete_model.py in <module>
43
44 from statsmodels.base.l1_slsqp import fit_l1_slsqp
---> 45 from statsmodels.distributions import genpoisson_p
46
47 try:
/opt/conda/lib/python3.6/site-packages/statsmodels/distributions/__init__.py in <module>
1 from statsmodels.tools._testing import PytestTester
2 from .empirical_distribution import ECDF, monotone_fn_inverter, StepFunction
----> 3 from .edgeworth import ExpandedNormal
4 from .discrete import genpoisson_p, zipoisson, zigenpoisson, zinegbin
5
/opt/conda/lib/python3.6/site-packages/statsmodels/distributions/edgeworth.py in <module>
5 import numpy as np
6 from numpy.polynomial.hermite_e import HermiteE
----> 7 from statsmodels.compat.scipy import factorial
8 from scipy.stats import rv_continuous
9 import scipy.special as special
ImportError: cannot import name 'factorial'
I've upgraded the scipy package, and I am still getting the same error. I am new to Kaggle and Python and I need the OLS function to do regression analysis. How can I fix this problem? If not, is there any other function I can use to get a regression summary?