jupyter notebook / Failed to start the kernel due the KeyError - python-3.x

My jupyter notebook doesn't start due the dead kernel with following Kernel error:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tornado/web.py", line 1512, in _execute
result = yield result
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tornado/gen.py", line 1055, in run
value = future.result()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tornado/concurrent.py", line 238, in result
raise_exc_info(self._exc_info)
File "<string>", line 4, in raise_exc_info
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tornado/gen.py", line 1069, in run
yielded = self.gen.send(value)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/notebook/services/sessions/handlers.py", line 67, in post
model = yield gen.maybe_future(sm.get_session(path=path))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/notebook/services/sessions/sessionmanager.py", line 170, in get_session
return self.row_to_model(row)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/notebook/services/sessions/sessionmanager.py", line 209, in row_to_model
raise KeyError
KeyError

In my case, the issue originated from having prompt-toolkit requirement conflict in jupyter-console and ipython.
You can use pip check to make sure whether you have the same problem. If the output is something similar to my output below, you have to fix the broken packages issue.
>>> pip check ipython
ipython 5.0.0 has requirement prompt-toolkit<2.0.0,>=1.0.3, but you'll have prompt-toolkit 2.0.9 which is incompatible.
>>> pip check jupyter-console
jupyter-console 6.0.0 has requirement prompt-toolkit<2.1.0,>=2.0.0, but you'll have prompt-toolkit 1.0.15 which is incompatible.
The quick fix is to try the solution originally mentioned here.
pip uninstall prompt-toolkit
pip install prompt-toolkit==1.0.15
pip uninstall jupyter-console
pip install jupyter-console==5.2.0

Related

How to install sklearn2pmml correctly in python?

I have been trying to install sklearn2pmml using pip
pip install --upgrade sklearn2pmml==0.83.0
pip install --upgrade sklearn2pmml
pip install sklearn2pmml
python3 -m pip install sklearn2pmml
I have tried all variations to pip install command. But I am still getting the same error (shown below)
pip install --upgrade sklearn2pmml==0.83.0
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
Defaulting to user installation because normal site-packages is not writeable
Collecting sklearn2pmml==0.83.0
Using cached sklearn2pmml-0.83.0.tar.gz (6.3 MB)
Preparing metadata (setup.py) ... error
error: subprocess-exited-with-error
× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [26 lines of output]
Traceback (most recent call last):
File "<string>", line 36, in <module>
File "<pip-setuptools-caller>", line 34, in <module>
File "/tmp/pip-install-4rirpdmx/sklearn2pmml_8f0847b696dd4b058e058b7e1e18cdda/setup.py", line 44, in <module>
"sklearn-pandas>=0.0.10"
File "/home/notebook/.local/lib/python3.7/site-packages/setuptools/_distutils/core.py", line 147, in setup
_setup_distribution = dist = klass(attrs)
File "/home/notebook/.local/lib/python3.7/site-packages/setuptools/dist.py", line 474, in __init__
for ep in metadata.entry_points(group='distutils.setup_keywords'):
File "/home/notebook/.local/lib/python3.7/site-packages/setuptools/_vendor/importlib_metadata/__init__.py", line 999, in entry_points
return SelectableGroups.load(eps).select(**params)
File "/home/notebook/.local/lib/python3.7/site-packages/setuptools/_vendor/importlib_metadata/__init__.py", line 449, in load
ordered = sorted(eps, key=by_group)
File "/home/notebook/.local/lib/python3.7/site-packages/setuptools/_vendor/importlib_metadata/__init__.py", line 997, in <genexpr>
dist.entry_points for dist in unique(distributions())
File "/home/notebook/.local/lib/python3.7/site-packages/setuptools/_vendor/importlib_metadata/_itertools.py", line 16, in unique_everseen
k = key(element)
File "/home/notebook/.local/lib/python3.7/site-packages/setuptools/_vendor/importlib_metadata/__init__.py", line 931, in _normalized_name
return self._name_from_stem(stem) or super()._normalized_name
File "/home/notebook/.local/lib/python3.7/site-packages/setuptools/_vendor/importlib_metadata/__init__.py", line 600, in _normalized_name
return Prepared.normalize(self.name)
File "/home/notebook/.local/lib/python3.7/site-packages/setuptools/_vendor/importlib_metadata/__init__.py", line 855, in normalize
return re.sub(r"[-_.]+", "-", name).lower().replace('-', '_')
File "/opt/pyenv/versions/3.7.4/lib/python3.7/re.py", line 192, in sub
return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or bytes-like object
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed
× Encountered error while generating package metadata.
╰─> See above for output.
note: This is an issue with the package mentioned above, not pip.
hint: See above for details.
I have tried installing several versions. I have tried uninstalling py4j and then running it. Installing py4j's different version and the installing it.
Not sure why I am getting this error.
Has someone faced this issue before?
I have tried installing different versions of the package. I got the same error.
I was able to fix this issue by updating
/home/notebook/.local/lib/python3.7/site-packages/setuptools/_vendor/importlib_metadata/init.py
file. I replaced
return re.sub(r"[-_.]+", "-", name).lower().replace('-', '_')
with
return re.sub(r"[-_.]+", "-", name).lower().replace('-', '_') if name else ''
I am not sure why but None was getting passed to normalize function.

when running virtualenv command on terminal

Thank you for your time.
I installed virtualenv on my mac using pip, but when I ran virtualenv env, it showed an error:
Traceback (most recent call last):
File "/usr/local/bin/virtualenv", line 6, in
from pkg_resources import load_entry_point
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/init.py", line 3241, in
#_call_aside
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/init.py", line 3225, in _call_aside
f(*args, **kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/init.py", line 3254, in _initialize_master_working_set
working_set = WorkingSet._build_master()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/init.py", line 583, in _build_master
ws.require(requires)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/init.py", line 900, in require
needed = self.resolve(parse_requirements(requirements))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/init.py", line 786, in resolve
raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'importlib-resources>=1.0' distribution was not found and is required by virtualenv
Please help. I installed the latest packaghes of importlib but it still doesn't work.
Your stack shows 2.7 python.
your tag question as python-3.x
You indicated that you already have importlib.
But error is about importlib-resources.
Try to import importlib_resources:
importlib_resources is a backport of Python 3.9’s standard library importlib.resources module for Python 2.7, and 3.5 through 3.8. Users of Python 3.9 and beyond are encouraged to use the standard library module. Developers looking for detailed API descriptions should refer to the Python 3.9 standard library documentation.

transport_encoding error during installing with pip

I'm getting unexpected arg: keyword encoding in parse() while trying to install any python package through pip.
I'm getting this problem since i installed tensorflow for python 3.6, which probably led to some issue with html5lib and setuptools. Have reinstalled html5lib1.0b10 using tar.gz file (admin install) but the issue still remains.
Please help!!
pip install spacy
Collecting spacy
Exception:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\pip\basecommand.py", line 215, in main
status = self.run(options, args)
File "C:\ProgramData\Anaconda3\lib\site-packages\pip\commands\install.py", line 335, in run
wb.build(autobuilding=True)
File "C:\ProgramData\Anaconda3\lib\site-packages\pip\wheel.py", line 749, in build
self.requirement_set.prepare_files(self.finder)
File "C:\ProgramData\Anaconda3\lib\site-packages\pip\req\req_set.py", line 380, in prepare_files
ignore_dependencies=self.ignore_dependencies))
File "C:\ProgramData\Anaconda3\lib\site-packages\pip\req\req_set.py", line 554, in _prepare_file
require_hashes
File "C:\ProgramData\Anaconda3\lib\site-packages\pip\req\req_install.py", line 278, in populate_link
self.link = finder.find_requirement(self, upgrade)
File "C:\ProgramData\Anaconda3\lib\site-packages\pip\index.py", line 465, in find_requirement
all_candidates = self.find_all_candidates(req.name)
File "C:\ProgramData\Anaconda3\lib\site-packages\pip\index.py", line 423, in find_all_candidates
for page in self._get_pages(url_locations, project_name):
File "C:\ProgramData\Anaconda3\lib\site-packages\pip\index.py", line 568, in _get_pages
page = self._get_page(location)
File "C:\ProgramData\Anaconda3\lib\site-packages\pip\index.py", line 683, in _get_page
return HTMLPage.get_page(link, session=self.session)
File "C:\ProgramData\Anaconda3\lib\site-packages\pip\index.py", line 811, in get_page
inst = cls(resp.content, resp.url, resp.headers)
File "C:\ProgramData\Anaconda3\lib\site-packages\pip\index.py", line 731, in __init__
namespaceHTMLElements=False,
TypeError: parse() got an unexpected keyword argument 'transport_encoding'
Issue was indeed with html5lib and setuptools, I re-installed html5lib using
conda install -c anaconda html5lib
Although package versions were same, it said 'The following packages will be SUPERSEDED by a higher-priority channel', and installed conda, conda-env, html5lib. I'm unsure about higher-priority channel part.
But this answered my problem!!
Problem appears after installing tensorflow 1.3.0.
Reinstall html5lib.
conda uninstall html5lib --force
conda install html5lib
Solves the problem temporarily until next pip install --upgrade tensorflow.
Had the same problem with pip install --upgrade tensorflow-gpu as well.

Pycharm failed to install Python packaging tools

I try to make an easy Python gui, therefore I need to import tkinter. This package is available in Python 3, so I need that interpreter, but I can't get it started in Pycharm. It always asks to install packaging tools and then I get the same error everytime and I can't seem to fix it. I get the following:
Executed command:
/tmp/tmpu8m14jqspycharm-management/pip-9.0.1/setup.py install
Error occured:
AttributeError: module 'setuptools.dist' has no attribute 'check_specifier'
Command output:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2237, in resolve
return functools.reduce(getattr, self.attrs, module)
AttributeError: module 'setuptools.dist' has no attribute 'check_specifier'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/tmp/tmpu8m14jqspycharm-management/pip-9.0.1/setup.py", line 92, in <module>
cmdclass={'test': PyTest},
File "/usr/lib/python3.5/distutils/core.py", line 108, in setup
_setup_distribution = dist = klass(attrs)
File "/usr/lib/python3/dist-packages/setuptools/dist.py", line 272, in __init__
_Distribution.__init__(self,attrs)
File "/usr/lib/python3.5/distutils/dist.py", line 281, in __init__
self.finalize_options()
File "/usr/lib/python3/dist-packages/setuptools/dist.py", line 327, in finalize_options
ep.load()(self, ep.name, value)
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2229, in load
return self.resolve()
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2239, in resolve
raise ImportError(str(exc))
ImportError: module 'setuptools.dist' has no attribute 'check_specifier'
I already searched some fixes but none actually worked.
Python PIP might not be installed. In Debian based distribution:
sudo apt-get install python3-pip
should solve if you use Python 3.
For Python 2.7x
sudo apt-get install python-pip
Close PyCharm and re-open it.
In this case, it may be possible that python pip (package installer) is not installed. So try installing that first.:
sudo apt-get install python3-pip

TypeError: Using a `tf.Tensor` as a Python `bool` is not allowed

I want to use the Tensorflow and TFLearn libraries in my Python 3.5 code. I have installed both using pip. I have the following versions:
tensorflow 0.12.0rc0
tflearn 0.2.1
python 3.5.2
conda 4.2.13
However, when running some code, I get the following error:
TypeError: Using a `tf.Tensor` as a Python `bool` is not allowed.
From what I can tell from this issue on GitHub, this issue should be resolved already.
How can I fix this?
EDIT: Here is the full stack trace:
Traceback (most recent call last):
File "test.py", line 35, in <module>
agent = DQNAgent(create_mlp, n_actions, env.observation_space.shape, min_replay_size=10000, batch_size=64)
File "agents.py", line 86, in __init__
self.q_net = network_fn(self.s, self._n_actions)
File "test.py", line 23, in create_mlp
net = nn.input_data(placeholder=inputs)
File "//anaconda/lib/python3.5/site-packages/tflearn/layers/core.py", line 55, in input_data
if not shape and not placeholder:
File "//anaconda/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 533, in __bool__
raise TypeError("Using a `tf.Tensor` as a Python `bool` is not allowed. "
This bug is caused by the tflearn v0.2.1 library. The way to resolve this error is by installing the newest version via git:
pip install git+https://github.com/tflearn/tflearn.git
This will install tflearn v0.2.2, in which this bug no longer occurs.

Resources