【问题标题】:The Kernel freezes with the import of the libraries in Python内核随着 Python 中库的导入而冻结
【发布时间】:2021-07-04 12:19:44
【问题描述】:

我使用 Anaconda、Jupyter Notebook 并安装了最新版本的 Python 3.9.4。

安装 Python 和 Anaconda (anaconda3) 后,我继续通过以下命令 pip install pandas scikit-learn graphviz 安装一些库,但结果证明没有必要获取 Requirement already satisfied

这是导致我出现问题的有问题的代码:

在 [1] 中:

# standard libraries
import pandas as pd
import numpy as np
import statistics
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import pydotplus #<---------------------------------------------ModuleNotFoundError: No module named 'pydotplus'
from sklearn.preprocessing import MinMaxScaler
from math import log
from six import StringIO 
from graphviz import Source #<----------------------------------ModuleNotFoundError: No module named 'graphviz'
import graphviz as gr #<----------------------------------------ModuleNotFoundError: No module named 'graphviz'
#from IPython.display import SVG
#from IPython.display import display
#from IPython.core.display import SVG
from IPython.display import Image

%matplotlib inline
#%pylab inline
from sklearn.feature_selection import mutual_info_classif

# pca libraries
from sklearn import decomposition
from sklearn.feature_selection import VarianceThreshold
from sklearn.preprocessing import scale
from sklearn.decomposition import PCA
#import seaborn as sb

# kfold stratification libraries
from sklearn.model_selection import StratifiedKFold

# libraries for building classifiers
from sklearn.ensemble import RandomForestClassifier        
from sklearn import tree
from sklearn.model_selection import train_test_split
  

# libraries for evaluation metrics
from sklearn.metrics import f1_score
from sklearn.metrics import accuracy_score 
from sklearn.metrics import balanced_accuracy_score
from sklearn.metrics import recall_score
from sklearn.metrics import precision_score

from sklearn.model_selection import cross_val_score
from sklearn.model_selection import cross_validate
from sklearn.metrics import confusion_matrix 

from sklearn.metrics import classification_report 

from datetime import datetime
from os import system
#from simple_colors import *

import bisect
import time 
import sys

# import warnings filter
from warnings import simplefilter
# ignore all future warnings
simplefilter(action='ignore', category=FutureWarning)

# elimination of the conversion warning
import warnings
from sklearn.exceptions import DataConversionWarning
simplefilter(action='ignore', category=DataConversionWarning)
warnings.filterwarnings('error')

在[2]中:

#dataframe import
#command parameters:
#filepath_or_buffer : str, pathlib.Path, py._path.local.LocalPath or any object with a read() method (such as a file handle or StringIO) The string could be a URL. Valid URL schemes include http, ftp, s3, and file. For file URLs, a host is expected. For instance, a local file could be file ://localhost/path/to/table.csv
#na_values : scalar, str, list-like, or dict, default None, Additional strings to recognize as NA/NaN. 
#low_memory : boolean, default True Internally process the file in chunks, resulting in lower memory use while parsing, but possibly mixed type inference. To ensure no mixed types either set False, or specify the type with the dtype parameter. Note that the entire file is read into a single DataFrame regardless, use the chunksize or iterator parameter to return the data in chunks. (Only valid with C parser)

def loadData(path):
    dataset=pd.read_csv(path, na_values=['Infinity'],low_memory=False)
    
    return dataset

最初,出于某种荒谬的原因,在代码的第一部分,对应于箭头,我得到了这些错误:

ModuleNotFoundError: No module named 'pydotplus'
ModuleNotFoundError: No module named 'graphviz'

尽管已经安装了必要的库。为了进一步验证,我从终端运行了以下命令:

pip install graphviz
pip install pydotplus

正如我已经预料的那样,我得到了Requirement already satisfied

尽管如此,我仍然尝试在错误处注释代码,但是当我尝试执行第一部分(以及稍后,第二部分)时,我的内核冻结,显示 In [*]:强>

如何解决这种情况?

更新

如果它可以帮助某人找到解决方案并解决问题,我意识到内核仅在运行导入后才会冻结,这只是第一块代码。但是,问题仍未解决,因为我需要导入这些库才能使我的代码正常工作(并且不要让内核冻结)。

【问题讨论】:

  • 可能又是一个PATH问题,我认为关于python找不到点的这类问题有很多问题。
  • 我已经阅读了有关 PATH 问题的信息,但是在安装此版本时,它并没有要求我禁用 PATH 长度限制。我也尝试下载旧版本,但此功能已被删除
  • 可能在谈论一些不同的东西,我在谈论系统路径。你重启终端了吗?
  • 当然,但没有任何改变
  • 考虑到您已经安装了 Anaconda,我建议您使用 conda 安装软件包以避免许多安装和依赖问题。

标签: python anaconda graphviz pydotplus


【解决方案1】:

可能的解决方案:

  1. Anaconda 目前似乎只支持 python3.8:Anaconda Download page ,所以我怀疑您首先安装的 python3.9 与您安装的最新 anaconda 和 python 发生冲突,并且某些内容已损坏。 Soln: 1 > 我建议你卸载 anaconda,从 C:\Users\your_user_name\Anaconda3 路径中删除 Anaconda3 文件夹,现在也完全卸载 python3.9,从环境变量中删除它们还。 2 > 现在,只需从上面的 url 下载最新的 anaconda,在安装过程中询问您是否将 python3.8 添加到您的路径时,请选中该复选框。 3 > 接下来在 anaconda3 命令提示符下从 conda 安装所需的包。
  2. 我已经检查过 SO,似乎 graphviz 的 pip 版本存在问题。请在此处查看答案:Graphviz - Can't import after installation 他们似乎找到了一个名为 python-graphviz 的替代包。

希望这些有助于解决您的问题。

【讨论】:

    【解决方案2】:

    首先从这个站点安装 Graphviz:Graphviz

    将 Graphviz 添加到环境变量“Path”中:

    • 转到Computer &gt; Properties &gt; Advanced system settings &gt; Environment Variables,然后在系统变量框中找到“路径”。点击路径,然后点击编辑。
    • ;C:\Program Files (x86)\Graphviz2.38\bin 附加到Path 中已存在的许多路径的末尾。请注意,Graphviz 的路径对您来说可能不同,因此请确保输入正确的路径。文件夹“bin”应该有很多文件,包括 dot.exe 应用程序。
    • 要检查安装,请转到命令提示符并输入:dot -V 这应该返回已安装的 Graphviz 版本。例如,dot - graphviz version 2.38.0。如果这不起作用,请输入 set 并查找 Graphviz 路径

    在此之后打开 Anaconda Prompt 并使用命令 pip install --force-reinstall graphviz 重新安装 Graphviz

    【讨论】:

      【解决方案3】:

      这可能是由于您的默认 pip 安装与 jupyter 使用的不同。以下脚本应在 jupyter 中运行,并将生成并执行一个命令,该命令可用于安装具有正确 pip 版本的软件包。

      import sys
      import os
      
      executable_path = sys.executable
      print(executable_path)
      command = f'"{executable_path}" -m pip install pandas scikit-learn graphviz pydotplus'
      print(command)
      # Uncomment the following line if you prefer to run the command yourself
      # exit()
      exit_code = os.system(command)
      if exit_code == 0:
          print("success!")
      else:
          print("failure!")
      

      【讨论】:

        猜你喜欢
        • 2017-03-24
        • 2017-04-10
        • 2016-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多