提问者:小点点

内核在Python中导入库时冻结


我使用Anaconda,Jupyter笔记本,并安装了Python 3.9.4的最新版本。

在安装Python和Anaconda(anaconda3)之后,我通过以下命令安装了一些库pip install-scikit-learn-graphviz,但是如果已经满足了要求,就没有必要了。

这是给我带来问题的有问题的代码:

在[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

正如我已经预料到的,我已经满足了要求

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

如何解决这种情况?

更新:

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


共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!")

匿名用户

首先从这个网站安装Graphviz:Graphviz。

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

  • 转到计算机

在此之后,打开Anaconda提示符并使用命令pip install--force reinstall Graphviz重新安装Graphviz

匿名用户

可能的解决方案:

  1. 水蟒似乎只支持蟒蛇3。截至今天:巨蟒下载页面,所以我怀疑蟒蛇3。您首先安装的9与您安装的最新anaconda和python存在冲突,有些内容已损坏。索恩:1

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