栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

linux、更换源、python常用操作、GPU服务器配置、python虚拟环境迁移、离线部署

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

linux、更换源、python常用操作、GPU服务器配置、python虚拟环境迁移、离线部署

linux配置 更换源

sudo chmod 777 /etc/apt/sources.list

vim /etc/apt/sources.list

先输入10000,再不断按d键删除现有的

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse

sudo apt-get install net-tools

anaconda
wget -P /tmp https://repo.anaconda.com/archive/Anaconda3-2020.02-Linux-x86_64.sh
bash /tmp/Anaconda3-2020.02-Linux-x86_64.sh
source ~/.bashrc  # 激活环境变量
conda update --all # 升级anaconda

创建虚拟环境conda create -n name python==3.7

anaconda更换源

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/`
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/`
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/`
conda config --add channels httlsps://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/`
conda config --set show_channel_urls yes

移除源
conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/

pip源
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

pycocotools在linux下的安装

https://blog.csdn.net/wlsccc/article/details/112728239

解压与压缩

tar -xvf 解压tar.gz

tar -vczf test.tar.gz test压缩

https://www.cnblogs.com/jyaray/archive/2011/04/30/2033362.html

删除环境

conda remove -n xxx --all

conda remove package
conda remove -n learn --all // 删除learn环境及下属所有包
pip uninstall package

python常用操作 1. 时间字符串
now_time = datetime.datetime.strptime(now_time, "%Y-%m-%d %H:%M:%S") #转datetime
time.strftime("%Y-%m-%d %H:%M:%S")  #转成字符串
2. pandas
  • concat
pd.concat(objs=[df1,df2],axis=0)

参考链接

3. jupyter notebook的虚拟环境使用
conda install nb_conda

添加和删除虚拟环境
参考链接1:知乎
参考链接2:CSDN

4. TensorFlow1.9和Python3.6配置 pip安装

pip install --user tensorflow==1.9.0 -i https://pypi.doubanio.com/simple/

https://pypi.tuna.tsinghua.edu.cn/simple/

报错numpy

FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’.
_np_qint8 = np.dtype([(“qint8”, np.int8, 1)])

解决方法

pip uninstall numpy

pip install numpy==1.16.4

5. 数据集下载
print("Downloading " + file_name + " ... ")  # 开始下载
urllib.request.urlretrieve(url_base + file_name, file_path)
print("Done")
数据集的转换
print("Converting " + file_name + " to NumPy Array ...")
with gzip.open(file_path, 'rb') as f:   # 打开读取二进制数据,也可以是文本数据
    data = np.frombuffer(f.read(), np.uint8, offset=16)  # np.frombuffer 从流读取数据,
data = data.reshape(-1, img_size)  # 一行是一个图片
print("Done")

np.uint8  0~255

>>> s = b'hello world'   # b是代表bytes,还有u和r
>>> np.frombuffer(s, dtype='S1', count=5, offset=6)  #偏置是6
array([b'w', b'o', b'r', b'l', b'd'], dtype='|S1')
数据集的封装与打开
print("Creating pickle file ...")  #这里是把一个字典进行了存储
with open(save_file, 'wb') as f:   #f是保存的文件名字
    pickle.dump(dataset, f, -1)    #-1是指定python3的序列化方式
print("Done!")


# 打开pickle文件。

with open(save_file, 'rb') as f:
    dataset = pickle.load(f)
数据集pickle打开的常见错误

在pickle.load(f)时,可能会碰到编码错误,就需要改为pickle.load(f, encoding=‘bytes’)

6. python程序加密 打包成exe

https://zhuanlan.zhihu.com/p/109266820

https://zhuanlan.zhihu.com/p/59442292

Cython生成pyd文件

需要安装个vsstudio c++那些

去官网下载wheel文件(直接下载的不对)

在安装,

建立一个setup文件

from distutils.core import setup
from Cython.Build import cythonize

setup(name='api_sign', ext_modules=cythonize(['in_main.py','main_use.py','data_load.py','subprogram.py','unique.py']))

# python setup.py build_ext --inplace
这里可以放多个文件

执行命令生成pyd文件:

python setup.py build_ext --inplace

生成pyd的报错处理

https://blog.csdn.net/Gavinmiaoc/article/details/84340736?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-13.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-13.control

easycython生成pyd

http://www.xoxxoo.com/index/index/article/id/772.html

GPU服务器配置

Linux 系统目录结构:https://www.runoob.com/linux/linux-system-contents.html

使用windows作为ssh服务端,linux作为ssh服务端,进行连接。

1.使用windows生成公钥和私钥

ssh-keygen -t rsa

之后选择位置

一般存储在.ssh/下面

生成两个文件 [Ubuntu 20.04 LTS.lnk](Ubuntu 20.04 LTS.lnk)

id_rsa, id_rsa.pub

2. 把id_rsa.pub传到linux服务器上

cat id_rsa.pub >> ~/.ssh/authorized_key

把这个新的公钥添加进去

3. windows的config配置

在.ssh/下新建一个config文件,没有后缀

Host my(简单常用名)
	HostName ip
	User myname
	Port 22
	IdentityFile (用的哪一个私钥)
Host my(简单常用名)
	HostName ip
	User myname
	IdentityFile (用的哪一个私钥)

https://www.xylnk.com/2020/206.html

服务端的config一般是端口和权限配置

4. 文件传输

scp 本地文件 my:服务器路径

scp -r 本地文件夹 my:服务器路径

rsync -avP 本地文件 my:服务器路径(速度非常快,用于大文件夹)

https://www.bilibili.com/video/BV1nk4y1k742

python虚拟环境迁移 安装miniconda
curl -k -o Miniconda2-latest-Linux-x86_64.sh https://repo.anaconda.com/miniconda/Miniconda2-latest-Linux-x86_64.sh

https://docs.conda.io/en/latest/miniconda.html

-k是跳过ssl证书检测

-o是保存为目标文件

https://www.ruanyifeng.com/blog/2019/09/curl-reference.html这是curl的参考网站

不可靠的方法 pip安装的包

pip freeze > requirements.txt

pip install -r requirements.txt

同平台离线

pip download -d ~/my_env_pkgs/ -r requirements.txt

pip install -r requirements.txt --no-index --find-links=file:///home/user_name/my_env_pkgs

跨平台离线

pip download --no-binary=:all: -d ~/my_env_pkgs/ -r requirements.txt

pip install -r requirements.txt --no-index --find-links=file:///home/user_name/my_env_pkgs

conda安装的包

conda env export -f environment.yml

conda env create -f environment.yml

手动删去其中的pip的那部分,只安装里面包含的conda部分

离线部署

安装(测试机器和待部署机器上都需要安装,建议在base环境安装)

conda install -c conda-forge conda-pack

导出环境

conda pack -n my_env -o out_name.tar.gz、

目标环境新建目录,一般放在.conda/envs/下面

mkdir -p my_env

解压缩进去

tar -xzf out_name.tar.gz -C my_env

目标机器激活环境,会将my_env/bin添加到path环境变量,之后方可支持conda activate

source my_env/bin/activate

部署机器上替换prefix

conda-unpack

此时如遇“Files managed by conda were found to have been deleted/overwritten …”的问题,则应该是在使用过程中误用pip卸载了用conda安装的包,此时首先需要用conda正确卸载这些包,然后再执行打包的操作

跨平台部署

删去yml文件中的build字段

==

注意

conda --clone还有–offline不建议使用

如果是离线主机,就直接使用联网的虚拟机,进行conda pack进去。

如果是在线主机,就使用txt和yml文件如在线下载。

参考链接

https://forskamse.blog.csdn.net/article/details/98665869

项目离线部署并启动虚拟环境进行运行

https://blog.csdn.net/shirukai/article/details/108822729

plt.rcParams.update({'figure.figsize':(9,7), 'figure.dpi':120})

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GJOlmpI0-1636362171870)(python%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C.assets/image-20210815115527645.png)]

p = d = q = range(0, 2)
pdq = list(itertools.product(p, d, q))
# 忽略警告
with warnings.catch_warnings():
    warnings.filterwarnings('ignore')
    error = walk_forward_validation(data, n_test, cfg)
key = str(cfg) #参数配置是元组
print(f'> Model{key} {error:.3f}')
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/444724.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号