我创建了一个新的虚拟环境并通过运行 pipenv install jupyter 安装了 jupyter notebook。我现在无法运行 jupyter notebook :

Traceback (most recent call last):
  File "c:\users\user\.virtualenvs\jupyter_project-rx9grp37\lib\site-packages\jsonschema\__init__.py", line 31, in <module>
    from importlib import metadata
ImportError: cannot import name 'metadata' from 'importlib' (C:\anaconda\Lib\importlib\__init__.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\anaconda\Lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\anaconda\Lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\user\.virtualenvs\jupyter_project-RX9gRp37\Scripts\jupyter-notebook.EXE\__main__.py", line 4, in <module>
  File "c:\users\user\.virtualenvs\jupyter_project-rx9grp37\lib\site-packages\notebook\notebookapp.py", line 80, in <module>
    from .services.contents.manager import ContentsManager
  File "c:\users\user\.virtualenvs\jupyter_project-rx9grp37\lib\site-packages\notebook\services\contents\manager.py", line 17, in <module>
    from nbformat import sign, validate as validate_nb, ValidationError
  File "c:\users\user\.virtualenvs\jupyter_project-rx9grp37\lib\site-packages\nbformat\__init__.py", line 33, in <module>
    from .validator import validate, ValidationError
  File "c:\users\user\.virtualenvs\jupyter_project-rx9grp37\lib\site-packages\nbformat\validator.py", line 12, in <module>
    from jsonschema import ValidationError
  File "c:\users\user\.virtualenvs\jupyter_project-rx9grp37\lib\site-packages\jsonschema\__init__.py", line 33, in <module>
    import importlib_metadata as metadata
  File "c:\users\user\.virtualenvs\jupyter_project-rx9grp37\lib\site-packages\importlib_metadata\__init__.py", line 547, in <module>
    __version__ = version(__name__)
  File "c:\users\user\.virtualenvs\jupyter_project-rx9grp37\lib\site-packages\importlib_metadata\__init__.py", line 509, in version
    return distribution(distribution_name).version
  File "c:\users\user\.virtualenvs\jupyter_project-rx9grp37\lib\site-packages\importlib_metadata\__init__.py", line 482, in distribution
    return Distribution.from_name(distribution_name)
  File "c:\users\user\.virtualenvs\jupyter_project-rx9grp37\lib\site-packages\importlib_metadata\__init__.py", line 183, in from_name
    dist = next(dists, None)
  File "c:\users\user\.virtualenvs\jupyter_project-rx9grp37\lib\site-packages\importlib_metadata\__init__.py", line 452, in <genexpr>
    return (item for item in root.iterdir()
  File "C:\anaconda\Lib\pathlib.py", line 1090, in iterdir
    for name in self._accessor.listdir(self):
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Users\\user\\Documents\\Python\\jupyter_project\\ '

我正在运行 python 3.7。

该问题似乎是由 jsonschema 工作方式的最新变化引起的。在 3.0.2 版本中,它似乎按预期工作。

改变的是这个( jsonschema/__init__.py ):

前:
from pkg_resources import get_distribution
__version__ = get_distribution(__name__).version

后:
try:
    from importlib import metadata
except ImportError: # for Python<3.8
    import importlib_metadata as metadata
__version__ = metadata.version("jsonschema")

除了降级到 jsonschema==3.0.2 之外,还有其他方法可以解决这个问题吗?

谢谢

最佳答案

在您的环境中升级您的 jsonschema 版本

在 conda 环境中conda upgrade jsonschema
在pip环境中pip install --upgrade jsonschema
这对我有用!

关于python - 软件包升级后 Jupyter Notebook 无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59101246/

10-12 20:25