Python缺pydotplus包和缺sklearn包和Python 中使用 zipfile 以及中文乱码问题和Python连接Hive

一 关于缺包报错,且无法直接通过pip安装包

在运行Python代码时候报错
Traceback (most recent call last):
File “/var/dana/dodox/filemanager/file/danastudio-unsubmit/s99JMJT7IWH”, line 15, in
import pydotplus
ModuleNotFoundError: No module named 'pydotplus

问题1:报错缺pydotplus包

在这里插入图片描述
此报错是缺pydotplus包报错,我按照要求运行python install后
仍然无法成功升级,提示如下:
在这里插入图片描述
在这里插入图片描述
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pydotplus
在这里插入图片描述
pip list | grep wheel
pip install -i https://pypi.douban.com/simple pydotplus
在这里插入图片描述
python -m pip install -U pip
pip install -i https://pypi.douban.com/simple pydotplus
在这里插入图片描述PS:到此pydotplus包安装成功
PS:到此pydotplus包安装成功

问题2:报错缺sklearn包![在这里插入图片描述]

pip install -i https://pypi.douban.com/simple sklearn
pip list|grep sklearn
在这里插入图片描述

二 所有重要的文件创建备份的程序

问题:Python 中使用 zipfile 以及中文乱码问题在这里插入图片描述

出现上述问题的原因是Windows下,没有默认是没有命令行zip的,需要在Windows下装个压缩软件(我安装的是7zip然后使用7z命令)
解决办法:在cmd 命令中写入7z.exe所在的目录
zip_command = '“D:\Program Files (x86)\7-Zip\7z.exe” a -tzip {0} {1} '.format(target, ’ '.join(source))

修改后具体代码如下:
import os
import time
1.需要备份的文件与目录将被指定在一个列表中。
#例如在 Windows 下:
source = [r’D:\list\win-i386’]
#又例如在 Mac OS X 与 Linux 下:
#source = [’/Users/swa/notes’]
#在这里要注意到我们必须在字符串中使用双引号用以括起其中包含空格的名称。
2. 备份文件必须存储在一个主备份目录中
#例如在 Windows 下:
target_dir = r’D:\Backup’

#又例如在 Mac OS X 和 Linux 下:
#target_dir = ‘/Users/swa/backup’
#要记得将这里的目录地址修改至你将使用的路径
#如果目标目录还不存在,则进行创建
if not os.path.exists(target_dir):
os.mkdir(target_dir) # 创建目录
3. 备份文件将打包压缩成 zip 文件。
4. 将当前日期作为主备份目录下的
#子目录名称
today = target_dir + os.sep + time.strftime(’%Y%m%d’)
#将当前时间作为 zip 文件的文件名
now = time.strftime(’%H%M%S’)

#添加一条来自用户的注释以创建
#zip 文件的文件名
comment = input('Enter a comment --> ')
#检查是否有评论键入
if len(comment) == 0:
target = today + os.sep + now + ‘.zip’
else:
target = today + os.sep + now + ‘’ +
comment.replace(’ ', '
’) + ‘.zip’

#如果子目录尚不存在则创建一个
if not os.path.exists(today):
os.mkdir(today)
print(‘Successfully created directory’, today)
5. 我们使用 zip 命令将文件打包成 zip 格式
zip_command = '“D:\Program Files\7-Zip\7z.exe” a -tzip -mcu {0} {1} ‘.format(target,’ '.join(source))
运行备份
print(‘Zip command is:’)
print(zip_command)
print(‘Running:’)
if os.system(zip_command) == 0:
print(‘Successful backup to’, target)
else:
print(‘Backup FAILED’)
最终结果截图如下:
在这里插入图片描述

三 Python 脚本连接 Hive 最佳实践

准备工作
底层安装连接 Hive 所需依赖

pip install sasl
pip install thrift
pip install thrift-sasl
pip install pyhive
pip install krbcontext
yum -y install gcc-c++ python-devel krb5-devel cyrus-sasl cyrus-sasl-lib cyrus-sasl-plain cyrus-sasl-devel cyrus-sasl-gssapi cyrus-sasl-md5
连接代码
场景一 连接未开启认证的 Hive

from pyhive import hive
try:
#连接未开启认证的 Hive
#hosts - Hive 所在节点
#port - Hive 端口
conn = hive.Connection(host=‘dn144’,
port=10000)
cursor = conn.cursor()
#显示当前数据库中所有表的名称
cursor.execute(‘show tables’)
for result in cursor.fetchall():
print(result)
cursor.close()
#执行完毕,断开连接
conn.close()

#打印错误信息
except Exception as e:
print("Error: " + str(e))
PS:一般情况下都用这种方式
场景二 连接自定义认证的 Hive
from pyhive import hive
try:
#连接未开启认证的 Hive
#hosts - Hive 所在节点
#port - Hive 端口
conn = hive.Connection(host=‘dn144’,
port=10000)
cursor = conn.cursor()
#显示当前数据库中所有表的名称
cursor.execute(‘show tables’)
for result in cursor.fetchall():
print(result)
cursor.close()
#执行完毕,断开连接
conn.close()

#打印错误信息
except Exception as e:
print("Error: " + str(e))

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值