Trouble importing modules into ipython - python-3.x

On a freshly installed Windows 10 machine, I installed the newest 64bit version of anaconda. Now when I try importing modules into ipython, I get errors. Some sample errors are reproduced below.
I am new to Python and Programming, so would appreciate a bit of help. This is a brand new Lenovo laptop, so there's no scope for any Python version conflict etc. (since nothing was installed on it earlier).
In [1]: import sklearn
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-b7c74cbf5af0> in <module>
----> 1 import sklearn
C:\Software\anaconda3\lib\site-packages\sklearn\__init__.py in <module>
74 else:
75 from . import __check_build
---> 76 from .base import clone
77 from .utils._show_versions import show_versions
78
C:\Software\anaconda3\lib\site-packages\sklearn\base.py in <module>
11 import re
12
---> 13 import numpy as np
14
15 from . import __version__
C:\Software\anaconda3\lib\site-packages\numpy\__init__.py in <module>
138
139 # Allow distributors to run custom init code
--> 140 from . import _distributor_init
141
142 from . import core
C:\Software\anaconda3\lib\site-packages\numpy\_distributor_init.py in <module>
32
33 with RTLD_for_MKL():
---> 34 from . import _mklinit
35
36 del RTLD_for_MKL
ImportError: DLL load failed: The specified module could not be found.
In [2]: import matplotlib.pyplot as plt
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-2-a0d2faabd9e9> in <module>
----> 1 import matplotlib.pyplot as plt
C:\Software\anaconda3\lib\site-packages\matplotlib\__init__.py in <module>
136 # cbook must import matplotlib only within function
137 # definitions, so it is safe to import from it here.
--> 138 from . import cbook, rcsetup
139 from matplotlib.cbook import (
140 MatplotlibDeprecationWarning, dedent, get_label, sanitize_sequence)
C:\Software\anaconda3\lib\site-packages\matplotlib\cbook\__init__.py in <module>
29 from weakref import WeakMethod
30
---> 31 import numpy as np
32
33 import matplotlib
C:\Software\anaconda3\lib\site-packages\numpy\__init__.py in <module>
138
139 # Allow distributors to run custom init code
--> 140 from . import _distributor_init
141
142 from . import core
C:\Software\anaconda3\lib\site-packages\numpy\_distributor_init.py in <module>
32
33 with RTLD_for_MKL():
---> 34 from . import _mklinit
35
36 del RTLD_for_MKL
ImportError: DLL load failed: The specified module could not be found.
In [3]: import pystan
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-3-ca8618bb8e4a> in <module>
----> 1 import pystan
ModuleNotFoundError: No module named 'pystan'
In [4]:

After a lot of google searching, I found a few sites that touch upon some closely related issues (ex. https://github.com/stan-dev/pystan/issues/520). It so happens that one needs to add some anaconda directroties to the Windows path. I added anaconda/Library/bin, anaconda/Scripts to the path. Now everything is working as expected.

Related

"Module 'tensorflow.python.distribute.values' has no attribute 'AutoPolicy'" from importing Tensorflow Hub

I've been trying to run this TensorFlow tutorial on my computer, but while running the following code I've been getting the error from the title:
import os
import shutil
import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_text as text
from official.nlp import optimization # to create AdamW optmizer
import matplotlib.pyplot as plt
tf.get_logger().setLevel('ERROR')
os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'
This is the entire error traceback. While running it in the tutorial's Collab notebook there doesn't seem any problem whatsoever.
AttributeErrorTraceback (most recent call last)
<ipython-input-7-3afaa91eeb1f> in <module>
3
4 import tensorflow as tf
----> 5 import tensorflow_hub as hub
6 import tensorflow_text as text
7 from official.nlp import optimization # to create AdamW optmizer
/usr/local/lib/python3.6/dist-packages/tensorflow_hub/__init__.py in <module>
86
87
---> 88 from tensorflow_hub.estimator import LatestModuleExporter
89 from tensorflow_hub.estimator import register_module_for_export
90 from tensorflow_hub.feature_column import image_embedding_column
/usr/local/lib/python3.6/dist-packages/tensorflow_hub/estimator.py in <module>
60
61
---> 62 class LatestModuleExporter(tf.compat.v1.estimator.Exporter):
63 """Regularly exports registered modules into timestamped directories.
64
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/lazy_loader.py in __getattr__(self, item)
60
61 def __getattr__(self, item):
---> 62 module = self._load()
63 return getattr(module, item)
64
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/lazy_loader.py in _load(self)
43 """Load the module and insert it into the parent's globals."""
44 # Import the target module and insert it into the parent's namespace
---> 45 module = importlib.import_module(self.__name__)
46 self._parent_module_globals[self._local_name] = module
47
/usr/lib/python3.6/importlib/__init__.py in import_module(name, package)
124 break
125 level += 1
--> 126 return _bootstrap._gcd_import(name[level:], package, level)
127
128
/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/api/_v1/estimator/__init__.py in <module>
8 import sys as _sys
9
---> 10 from tensorflow_estimator.python.estimator.api._v1.estimator import experimental
11 from tensorflow_estimator.python.estimator.api._v1.estimator import export
12 from tensorflow_estimator.python.estimator.api._v1.estimator import inputs
/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/api/_v1/estimator/experimental/__init__.py in <module>
8 import sys as _sys
9
---> 10 from tensorflow_estimator.python.estimator.canned.dnn import dnn_logit_fn_builder
11 from tensorflow_estimator.python.estimator.canned.kmeans import KMeansClustering as KMeans
12 from tensorflow_estimator.python.estimator.canned.linear import LinearSDCA
/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/canned/dnn.py in <module>
29 from tensorflow.python.keras.utils import losses_utils
30 from tensorflow.python.util.tf_export import estimator_export
---> 31 from tensorflow_estimator.python.estimator import estimator
32 from tensorflow_estimator.python.estimator.canned import head as head_lib
33 from tensorflow_estimator.python.estimator.canned import optimizers
/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/estimator.py in <module>
50 from tensorflow.python.util.tf_export import estimator_export
51 from tensorflow_estimator.python.estimator import model_fn as model_fn_lib
---> 52 from tensorflow_estimator.python.estimator import run_config
53 from tensorflow_estimator.python.estimator import util as estimator_util
54 from tensorflow_estimator.python.estimator.export import export_lib
/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/run_config.py in <module>
28 from tensorflow.core.protobuf import rewriter_config_pb2
29 from tensorflow.python.distribute import estimator_training as distribute_coordinator_training
---> 30 from tensorflow.python.distribute import parameter_server_strategy_v2
31 from tensorflow.python.util import compat_internal
32 from tensorflow.python.util import function_utils
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/parameter_server_strategy_v2.py in <module>
26
27 from tensorflow.python.distribute import distribute_lib
---> 28 from tensorflow.python.distribute import distribute_utils
29 from tensorflow.python.distribute import parameter_server_strategy
30 from tensorflow.python.distribute import sharded_variable
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_utils.py in <module>
372 # (synchronization=ON_READ, aggregation=NONE,SUM,MEAN,ONLY_FIRST_REPLICA)
373 VARIABLE_POLICY_MAPPING = {
--> 374 vs.VariableSynchronization.AUTO: values_lib.AutoPolicy,
375 vs.VariableSynchronization.ON_WRITE: values_lib.OnWritePolicy,
376 vs.VariableSynchronization.ON_READ: values_lib.OnReadPolicy,
AttributeError: module 'tensorflow.python.distribute.values' has no attribute 'AutoPolicy'
Is there any particular reason as to why is this happening?
Thank you in advance.
My guess is this could be related to a version mismatch between the versions you're using for tensorflow and tensorflow_hub.
It looks like AutoPolicy was removed from TF in a recent commit, so if you built tensorflow from source and included this commit, then that could be the issue. If you want to build TF from source, checkout the latest official release before building (2.4.1).

Why do I have problems with TensorFlow in Jupyter?

I have been fine with my Jupyter notebook for running my python codes for some time. However, I recently picked interests in Style Transfer with deep learning. The codes I got for practice required that I downgrade TensorFlow to a lower version. Fortunately, this worked but unfortunately, all other codes that I have developed for months (requiring TensorFlow backend) stopped working. I tried using JupyterLab and sometimes, they worked while most times, I get the same error just as in Jupyter notebook. Now, the only get around is to move my codes to Spyder (which is quite frustrating).
Every time I run my codes, I get the following error:
AttributeError Traceback (most recent call last)
<ipython-input-2-6d5587dfcc0c> in <module>
1 # import all libraries
----> 2 import keras
3 from keras.models import Sequential
4 from keras.layers import Dense, Activation, Dropout
5 from keras.layers.convolutional import Conv1D
~\Miniconda3\envs\tensorflow\lib\site-packages\keras\__init__.py in <module>
1 from __future__ import absolute_import
2
----> 3 from . import utils
4 from . import activations
5 from . import applications
~\Miniconda3\envs\tensorflow\lib\site-packages\keras\utils\__init__.py in <module>
4 from . import data_utils
5 from . import io_utils
----> 6 from . import conv_utils
7
8 # Globally-importable utils.
~\Miniconda3\envs\tensorflow\lib\site-packages\keras\utils\conv_utils.py in <module>
7 from six.moves import range
8 import numpy as np
----> 9 from .. import backend as K
10
11
~\Miniconda3\envs\tensorflow\lib\site-packages\keras\backend\__init__.py in <module>
87 elif _BACKEND == 'tensorflow':
88 sys.stderr.write('Using TensorFlow backend.\n')
---> 89 from .tensorflow_backend import *
90 else:
91 # Try and load external backend.
~\Miniconda3\envs\tensorflow\lib\site-packages\keras\backend\tensorflow_backend.py in <module>
3 from __future__ import print_function
4
----> 5 import tensorflow as tf
6 from tensorflow.python.framework import ops as tf_ops
7 from tensorflow.python.training import moving_averages
~\Miniconda3\envs\tensorflow\lib\site-packages\tensorflow\__init__.py in <module>
22
23 # pylint: disable=g-bad-import-order
---> 24 from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import
25
26 try:
~\Miniconda3\envs\tensorflow\lib\site-packages\tensorflow\python\__init__.py in <module>
86 # Bring in subpackages.
87 from tensorflow.python import data
---> 88 from tensorflow.python import keras
89 from tensorflow.python.feature_column import feature_column_lib as feature_column
90 from tensorflow.python.layers import layers
~\Miniconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\__init__.py in <module>
23
24 from tensorflow.python.keras import activations
---> 25 from tensorflow.python.keras import applications
26 from tensorflow.python.keras import backend
27 from tensorflow.python.keras import callbacks
~\Miniconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\applications\__init__.py in
<module>
23
24 from tensorflow.python.keras import backend
---> 25 from tensorflow.python.keras import engine
26 from tensorflow.python.keras import layers
27 from tensorflow.python.keras import models
~\Miniconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\engine\__init__.py in
<module>
21 # TODO(fchollet): Remove hourglass imports once external code is done importing
22 # non-public APIs.
---> 23 from tensorflow.python.keras.engine.base_layer import InputSpec
24 from tensorflow.python.keras.engine.base_layer import Layer
25 from tensorflow.python.keras.engine.input_layer import Input
~\Miniconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in
<module>
37 from tensorflow.python.keras import initializers
38 from tensorflow.python.keras import regularizers
---> 39 from tensorflow.python.keras.utils import generic_utils
40 from tensorflow.python.keras.utils import tf_utils
41 # A module that only depends on `keras.layers` import these from here.
~\Miniconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\utils\__init__.py in <module>
30 from tensorflow.python.keras.utils.generic_utils import Progbar
31 from tensorflow.python.keras.utils.generic_utils import serialize_keras_object
---> 32 from tensorflow.python.keras.utils.io_utils import HDF5Matrix
33 from tensorflow.python.keras.utils.layer_utils import convert_all_kernels_in_model
34 from tensorflow.python.keras.utils.layer_utils import get_source_inputs
~\Miniconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\utils\io_utils.py in <module>
27
28 try:
---> 29 import h5py
30 except ImportError:
31 h5py = None
~\Miniconda3\envs\tensorflow\lib\site-packages\h5py\__init__.py in <module>
32 raise
33
---> 34 from . import version
35
36 if version.hdf5_version_tuple != version.hdf5_built_version_tuple:
~\Miniconda3\envs\tensorflow\lib\site-packages\h5py\version.py in <module>
15
16 from collections import namedtuple
---> 17 from . import h5 as _h5
18 import sys
19 import numpy
h5py\h5.pyx in init h5py.h5()
AttributeError: type object 'h5py.h5.H5PYConfig' has no attribute '__reduce_cython__'
How do I rectify this situation?
Many thanks in anticipation of your suggestions.

why import sklearn is not working in Jupyter?

I installed the recent version of Anaconda but "Import sklearn" gives an error
ImportError Traceback (most recent call last)
<ipython-input-5-b7c74cbf5af0> in <module>
----> 1 import sklearn
~\anaconda3\lib\site-packages\sklearn\__init__.py in <module>
62 else:
63 from . import __check_build
---> 64 from .base import clone
65 from .utils._show_versions import show_versions
66
~\anaconda3\lib\site-packages\sklearn\base.py in <module>
11 from scipy import sparse
12 from .externals import six
---> 13 from .utils.fixes import signature
14 from . import __version__
15
~\anaconda3\lib\site-packages\sklearn\utils\__init__.py in <module>
14 from . import _joblib
15 from ..exceptions import DataConversionWarning
---> 16 from .fixes import _Sequence as Sequence
17 from .deprecation import deprecated
18 from .validation import (as_float_array,
~\anaconda3\lib\site-packages\sklearn\utils\fixes.py in <module>
90 from ._scipy_sparse_lsqr_backport import lsqr as sparse_lsqr
91 else:
---> 92 from scipy.sparse.linalg import lsqr as sparse_lsqr # noqa
93
94
~\anaconda3\lib\site-packages\scipy\sparse\linalg\__init__.py in <module>
115 from .dsolve import *
116 from .interface import *
--> 117 from .eigen import *
118 from .matfuncs import *
119 from ._onenormest import *
~\anaconda3\lib\site-packages\scipy\sparse\linalg\eigen\__init__.py in <module>
9 from __future__ import division, print_function, absolute_import
10
---> 11 from .arpack import *
12 from .lobpcg import *
13
~\anaconda3\lib\site-packages\scipy\sparse\linalg\eigen\arpack\__init__.py in <module>
20 from __future__ import division, print_function, absolute_import
21
---> 22 from .arpack import *
~\anaconda3\lib\site-packages\scipy\sparse\linalg\eigen\arpack\arpack.py in <module>
43 __all__ = ['eigs', 'eigsh', 'svds', 'ArpackError', 'ArpackNoConvergence']
44
---> 45 from . import _arpack
46 import numpy as np
47 import warnings
ImportError: DLL load failed: The specified procedure could not be found.
I found out what the problem was. I had created an env file for Jupyter and edited it to open jupyter in Chrome. After updating python, import sklearn did not work. So I had to go back and delete that env file and everything worked fine after that. Hope this helps future readers of this post.

ImportError: DLL load failed: The specified procedure could not be found. (import sklearn)

When I import sklearn, it throws this ImportError(DLL load failed: The specified procedure could not be found.)
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-b7c74cbf5af0> in <module>()
----> 1 import sklearn
~\Anaconda3\lib\site-packages\sklearn\__init__.py in <module>()
62 else:
63 from . import __check_build
---> 64 from .base import clone
65 from .utils._show_versions import show_versions
66
~\Anaconda3\lib\site-packages\sklearn\base.py in <module>()
11 from scipy import sparse
12 from .externals import six
---> 13 from .utils.fixes import signature
14 from . import __version__
15
~\Anaconda3\lib\site-packages\sklearn\utils\__init__.py in <module>()
11
12 from .murmurhash import murmurhash3_32
---> 13 from .validation import (as_float_array,
14 assert_all_finite,
15 check_random_state, column_or_1d, check_array,
~\Anaconda3\lib\site-packages\sklearn\utils\validation.py in <module>()
20
21 from ..externals import six
---> 22 from ..utils.fixes import signature
23 from .. import get_config as _get_config
24 from ..exceptions import NonBLASDotWarning
~\Anaconda3\lib\site-packages\sklearn\utils\fixes.py in <module>()
81 return stats.boxcox(x, lmbda)
82 else:
---> 83 from scipy.special import boxcox # noqa
84
85
~\Anaconda3\lib\site-packages\scipy\special\__init__.py in <module>()
638 from .sf_error import SpecialFunctionWarning, SpecialFunctionError
639
--> 640 from ._ufuncs import *
641
642 from .basic import *
ImportError: DLL load failed: The specified procedure could not be found.
If you already have a working installation of numpy and scipy, then do this:
pip install -U scikit-learn
otherwise:
conda install scikit-learn
finally check for updates:
conda update pip

How do you run tensorflow on colaboratory?

I'm trying to use Google's Colaboratory. The first line of output was hopeful.
Using TensorFlow backend.
But then I got this error message.
ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory
That error apparently triggered another error. (Sorry for the long stack trace, but the error message itself suggested including it when asking for help.)
During handling of the above exception, another exception occurred:
ImportError Traceback (most recent call last)
<ipython-input-6-0b846698ae4a> in <module>()
2 import numpy as np
3 import matplotlib.pylab as plt
----> 4 from keras.models import Sequential
5 from keras.layers import Dense, InputLayer
6
/usr/local/lib/python3.6/dist-packages/keras/__init__.py in <module>()
1 from __future__ import absolute_import
2
----> 3 from . import utils
4 from . import activations
5 from . import applications
/usr/local/lib/python3.6/dist-packages/keras/utils/__init__.py in <module>()
4 from . import data_utils
5 from . import io_utils
----> 6 from . import conv_utils
7
8 # Globally-importable utils.
/usr/local/lib/python3.6/dist-packages/keras/utils/conv_utils.py in <module>()
7 from six.moves import range
8 import numpy as np
----> 9 from .. import backend as K
10
11
/usr/local/lib/python3.6/dist-packages/keras/backend/__init__.py in <module>()
82 elif _BACKEND == 'tensorflow':
83 sys.stderr.write('Using TensorFlow backend.\n')
---> 84 from .tensorflow_backend import *
85 else:
86 raise ValueError('Unknown backend: ' + str(_BACKEND))
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in <module>()
3 from __future__ import print_function
4
----> 5 import tensorflow as tf
6 from tensorflow.python.training import moving_averages
7 from tensorflow.python.ops import tensor_array_ops
/usr/local/lib/python3.6/dist-packages/tensorflow/__init__.py in <module>()
22
23 # pylint: disable=g-bad-import-order
---> 24 from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import
25 # pylint: disable=wildcard-import
26 from tensorflow.tools.api.generator.api import * # pylint: disable=redefined-builtin
/usr/local/lib/python3.6/dist-packages/tensorflow/python/__init__.py in <module>()
47 import numpy as np
48
---> 49 from tensorflow.python import pywrap_tensorflow
50
51 # Protocol buffers
/usr/local/lib/python3.6/dist-packages/tensorflow/python/pywrap_tensorflow.py in <module>()
72 for some common reasons and solutions. Include the entire stack trace
73 above this error message when asking for help.""" % traceback.format_exc()
---> 74 raise ImportError(msg)
75
76 # pylint: enable=wildcard-import,g-import-not-at-top,unused-import,line-too-long
ImportError: Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 58, in <module>
from tensorflow.python.pywrap_tensorflow_internal import *
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
_pywrap_tensorflow_internal = swig_import_helper()
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
File "/usr/lib/python3.6/imp.py", line 243, in load_module
return load_dynamic(name, filename, file)
File "/usr/lib/python3.6/imp.py", line 343, in load_dynamic
return _load(spec)
ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory
Failed to load the native TensorFlow runtime.
See https://www.tensorflow.org/install/install_sources#common_installation_problems
for some common reasons and solutions. Include the entire stack trace
above this error message when asking for help.
Below that was a suggestion to consult https://www.tensorflow.org/install/install_sources#common_installation_problems.
But that page is about installing TensorFlow from source. I'm not trying to install from source. I just want a runnable version that I can call.
Any suggestions would be appreciated.
Thanks.

Resources