如何在 ipython 中修复 "DistributionNotFound: The 'jsonschema' distribution ..."

How to fix "DistributionNotFound: The 'jsonschema' distribution ..." in ipython

在安装了 jsonschema 3.0.1 的虚拟环境 (venv) 中,尝试从 ipython import jsonschema 会导致以下错误。

(venv) COMPUTER:prj user.name$ ipython                                                                                       
/usr/local/Cellar/ipython/7.5.0/libexec/lib/python3.7/site-packages/IPython/core/interactiveshell.py:925: UserWarning: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv.
  warn("Attempting to work in a virtualenv. If you encounter problems, please "                                                                                     
Python 3.7.3 (default, Mar 27 2019, 09:23:15)             
Type 'copyright', 'credits' or 'license' for more information             
IPython 7.5.0 -- An enhanced Interactive Python. Type '?' for help.                                                                                           

[ins] In [1]: import jsonschema                                                                                                                                     
---------------------------------------------------------------------------
DistributionNotFound                      Traceback (most recent call last)                                                                                           
<ipython-input-1-a1a01cfa025e> in <module>                
----> 1 import jsonschema                                                                                                                                           

~/prj/venv/lib/python3.7/site-packages/jsonschema/__init__.py in <module>
     31                                                                                                                                                         
     32 from pkg_resources import get_distribution        
---> 33 __version__ = get_distribution(__name__).version                                                                                                       

/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py in get_distribution(dist)                                                                            
    479         dist = Requirement.parse(dist)                                      
    480     if isinstance(dist, Requirement):                                                                                                              
--> 481         dist = get_provider(dist)                                                                                                                                          
    482     if not isinstance(dist, Distribution):
    483         raise TypeError("Expected string, Requirement, or Distribution", dist)                                                                                                                                                             

/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py in get_provider(moduleOrReq)                   
    355     """Return an IResourceProvider for the named module or requirement"""
    356     if isinstance(moduleOrReq, Requirement):                                                                                                                                                                               
--> 357         return working_set.find(moduleOrReq) or require(str(moduleOrReq))[0]
    358     try:                                                                                                                                           
    359         module = sys.modules[moduleOrReq]                                                                                                                                  

/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py in require(self, *requirements)                                                                                                                                                   
    898         included, even if they were already activated in this working set.                                                   
    899         """                                                       
--> 900         needed = self.resolve(parse_requirements(requirements))                                   
    901                                                                                                                                                                                                                            
    902         for dist in needed:                                                                                  

/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py in resolve(self, requirements, env, installer, replace_conflicting, extras)
    784                     if dist is None:                                                                
    785                         requirers = required_by.get(req, None)                                    
--> 786                         raise DistributionNotFound(req, requirers)                                                           
    787                 to_activate.append(dist)                                                                                                                                                                                   
    788             if dist not in req:                                                                   

DistributionNotFound: The 'jsonschema' distribution was not found and is required by the application

python -c 'import jsonschema' 有效。

如警告所示,运行以下命令解决了该问题:

(venv) COMPUTER:prj user.name$ pip install ipython
(venv) COMPUTER:prj user.name$ venv/bin/ipython
Python 3.7.3 (default, Mar 27 2019, 09:23:15) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.5.0 -- An enhanced Interactive Python. Type '?' for help.

[ins] In [1]: import jsonschema

[ins] In [2]: