pycharm使用conda调用远程服务器

PyCharm彻底删除项目的方法

https://www.pycharm.com.cn/t/62

pycharm连接远程服务器操作

配合远程文件传输助手 RaiDrive,可视化管理远程服务器的存储
在这里插入图片描述

Linux安装conda

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

pycharm

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
home—>dll—>annacoda3—>bin—>python3.9
在这里插入图片描述

window的powershell安装conda链接远程,并创建环境

#登录远程
ssh 用户名@网址 -p 端口
conda info -e
#创建虚拟环境
conda create -n name_pycharm python=3.9#创建
conda activate name_pycharm#激活
conda install jupyter#就、虚拟环境中下载软件
conda list#查看
jupyter notebook#启动

1、 ImportError: cannot import name ‘BatchNormalization‘ from ‘keras.layers.normalization‘

版本问题

解决方案:将from keras.layers.normalization import BatchNormalization
改成from keras.layers.normalization.batch_normalization_v1 import BatchNormalization

2、FutureWarning: pandas.Int64Index is deprecated and will be removed from pandas in a future version. Use pandas.Index with the appropriate dtype instead. from pandas import MultiIndex, Int64Index

pip install --upgrade pandas

3、Error! C:\Users\18437\jupyterProjectsTotalNPL\复旦大学中文文本分类语料.xlsx is not UTF-8 encodedSaving disabled.

解决方法:

无论是txt文件还是csv文件出现这样的问题,解决方法是一样的:
**右击文件-以记事本的方式打开-另存为-注意窗口的下方–选择UTF-8 编码格式-保存。
**。
https://www.php.cn/topic/excel/455341.html#:~:text=excel%E8%AE%BE%E7%BD%AE%E6%88%90,%E5%9B%9E%E4%BF%9D%E5%AD%98%E6%96%87%E4%BB%B6%E5%8D%B3%E5%8F%AF%E3%80%82

4、SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape

https://www.cnblogs.com/rychh/p/9743864.html

总结有三种方法:

一:更换为绝对路径的写法
func1("C:\\Users\\renyc")

二:显式声明字符串不用转义(加r)
func1(r"C:\Users\renyc")

三:使用Linux的路径/
func1("C:/Users/renyc")

5、ImportError: Missing optional dependency ‘openpyxl’. Use pip or conda to install openpyxl.

pip install openpyxl

6、tensorflow调用GPU

环境安装

首先点开anaconda里面的Anaconda powershell prompt
#在里面创建虚拟环境tensorflowMy
conda create -n tensorflowMy python=3.9

#激活虚拟环境
conda activate tensorflowMy

#安装cudatoolkit
conda install cudatoolkit -c conda-forge

#安装cudnn
conda install cudnn -c conda-forge

#在虚拟环境里面下载tensorflow包
pip install tensorflow

#在虚拟环境里面下载keras包
pip install keras

代码调用

import os
import tensorflow as tf
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
gpus = tf.config.list_physical_devices("GPU")

#查看使用的是GPU还是CPU:
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())

#查看GPU使用率:在终端执行nvidia-smi

7、Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.

经过网上搜索,这种一般是由于GPU显存不够导致的原因,我的解决办法是直接调小了batch_size的大小,将batch_size的值由原来的16修改为了8,即可正常运行

8、豆瓣镜像 快速安装

pip install -i https://pypi.douban.com/simple <需要安装的包>
比如:
pip install -i https://pypi.douban.com/simple requests

tensorflow 2.5.0 
pip install -i https://pypi.douban.com/simple grpcio==1.34.0
pip install -i https://pypi.douban.com/simple  h5py==3.1.0
pip install -i https://pypi.douban.com/simple numpy==1.19.2
pip install -i https://pypi.douban.com/simple six==1.15.0
pip install -i https://pypi.douban.com/simple tensorflow-estimator==2.5.0
pip install -i https://pypi.douban.com/simple typing-extensions==3.7.4
pip install -i https://pypi.douban.com/simple wrapt==1.12.1

9、pytorch安装GPU

python:3.8.5
conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch

(pytorch_envs) C:\Users\Fishc>python
Python 3.8.12 (default, Oct 12 2021, 03:01:40) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.__version__
'1.10.2'
>>>torch.cuda.is_available()

10、TensorFlow调用GPU

虚拟环境里面安装tensorflow2.9.0+keras2.9.0+cudnn8.2.1.32+cudatoolkit11.4.2+python3.9
首先点开anaconda里面的Anaconda powershell prompt

在里面创建虚拟环境tensorflowMy

conda create -n tensorflowMy python=3.6

激活虚拟环境

conda activate tensorflowMy
pip install tensorflow-gpu==1.12.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
安装cudatoolkit

conda install cudatoolkit -c conda-forge

安装cudnn

conda install cudnn -c conda-forge

在虚拟环境里面下载tensorflow包

pip install tensorflow

在虚拟环境里面下载keras包

pip install keras
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '/gpu:0' 
  #这句关键
import time
#查看使用的是GPU还是CPU:
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())

11、配置环境

#创建环境
conda create -n rebot python=3.6
conda activate rebot
#安装包
#升级指令
python -m pip install --upgrade pip
conda install jupyter 
pip install  tornado==4.4.3 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install tensorflow==1.2 -i https://pypi.tuna.tsinghua.edu.cn/simple
#(tensorflow 2.0以后没有 tensorflow.contrib)
pip list



12、显存不足

import tensorflow as tf
import os
os.environ["CUDA_VISIBLE_DEVICES"] = '0'   #指定第一块GPU可用
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.5  # 程序最多只能占用指定gpu50%的显存
config.gpu_options.allow_growth = True      #程序按需申请内存
sess = tf.Session(config = config)

13、tensorflow1版本GPU

conda create -n robot python=3.6
conda activate robot


conda install jupyter 
python -m pip install --upgrade pip
pip install  tornado==4.4.3 -i https://pypi.tuna.tsinghua.edu.cn/simple

pip install tensorflow-gpu==1.12  -i https://pypi.tuna.tsinghua.edu.cn/simple
conda install cudatoolkit=9.0 
conda install cudnn=7.3

pip install numpy==1.16.0

import tensorflow as tf
tf.test.is_gpu_available()

14、gitgit

开始使用我们的和界面gitgit-lfs

如果需要从命令行创建模型存储库(如果从网站创建了存储库,则跳过)

pip install huggingface_hub
Or use transformers-cli if you have transformers

huggingface-cli login
Log in using the same credentials as huggingface.co/join
Create a model repo from the CLI if needed
huggingface-cli repo create model_name
在本地克隆模型

Make sure you have git-lfs installed
(https://git-lfs.github.com)
git lfs install
git clone https://huggingface.co/username/model_name
然后添加,提交和推送权重,分词器和配置

save files via `.save_pretrained()` or move them here
git add .
git commit -m "commit from $USER"
git push
然后,您的模型将通过其标识符进行访问:用户名/model_name

任何人都可以从代码中加载它:

tokenizer = AutoTokenizer.from_pretrained("username/model_name")
model = AutoModel.from_pretrained("username/model_name")

克隆环境

environment.yaml

#添加镜像源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
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/r
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2

#显示检索路径
conda config --set show_channel_urls yes

#显示镜像通道
conda config --show channels

# -n 后面的参数是服务器A要克隆的环境名称
conda env export -n cmd > environment.yaml
# -n 后的参数是在B上要创建的虚拟环境名称, -f 后参数指的是yaml环境内容
conda env create -n cmd -f environment.yaml

#导出环境
conda install --yes --file requirements.txt

#安装环境
conda create -n ner python=3.8#python版本与导出环境统一
conda env list
activate ner
pip install -r requirements.txt -i https://pypi.douban.com/simple

conda创建环境过程中出现“Solving environment: failed”报错

1、把environment.yaml第二个等号,包括等号全删了
2、还会有几个包没找到,直接在environment.yaml删掉对应的即可

查看torch和cuda版本

import torch
print(torch.__version__)  #注意是双下划线
print(torch.version.cuda)
print(torch.cuda.is_available())
print(torch.cuda.get_device_name())

安装1torch-geometric

我的torch是1.12.0,cuda是11.3

pip install torch-scatter -f https://data.pyg.org/whl/torch-1.8.0+cu111.html
pip install torch-sparse -f https://data.pyg.org/whl/torch-1.8.0+cu111.html
此外还需要安装补充包(可选的)。


pip install torch-cluster -f https://data.pyg.org/whl/torch-1.10.0+cu102.html
pip install torch-spline-conv -f https://data.pyg.org/whl/torch-1.10.0+cu102.html

pip install torch-geometric

https://pytorch.org/get-started/previous-versions/#linux-and-windows-1

# CUDA 10.2
conda install pytorch==1.12.0 torchvision==0.13.0 torchaudio==0.12.0 cudatoolkit=10.2 -c pytorch
# CUDA 11.3
conda install pytorch==1.12.0 torchvision==0.13.0 torchaudio==0.12.0 cudatoolkit=11.3 -c pytorch
# CUDA 11.6
conda install pytorch==1.12.0 torchvision==0.13.0 torchaudio==0.12.0 cudatoolkit=11.6 -c pytorch -c conda-forge
# CPU Only
conda install pytorch==1.12.0 torchvision==0.13.0 torchaudio==0.12.0 cpuonly -c pytorch


# ROCM 5.1.1 (Linux only
pip install torch==1.12.0+rocm5.1.1 torchvision==0.13.0+rocm5.1.1 torchaudio==0.12.0 --extra-index-url  https://download.pytorch.org/whl/rocm5.1.1
# CUDA 11.6
pip install torch==1.12.0+cu116 torchvision==0.13.0+cu116 torchaudio==0.12.0 --extra-index-url https://download.pytorch.org/whl/cu116
# CUDA 11.3
pip install torch==1.12.0+cu113 torchvision==0.13.0+cu113 torchaudio==0.12.0 --extra-index-url https://download.pytorch.org/whl/cu113
# CUDA 10.2
pip install torch==1.12.0+cu102 torchvision==0.13.0+cu102 torchaudio==0.12.0 --extra-index-url https://download.pytorch.org/whl/cu102
# CPU only
pip install torch==1.12.0+cpu torchvision==0.13.0+cpu torchaudio==0.12.0 --extra-index-url https://download.pytorch.org/whl/cpu

安装离线下载 torch-geometric

在linux下配置pytorch1.8.0+cuda11.1+pytorch-geometric
https://pytorch-geometric.com/whl/torch-1.8.0%2Bcu111.html

pip install torch_scatter-2.0.6-cp38-cp38-linux_x86_64.whl
pip install torch_sparse-0.6.9-cp38-cp38-linux_x86_64.whl
pip install torch_cluster-1.5.9-cp38-cp38-linux_x86_64.whl
pip install torch_spline_conv-1.2.1-cp38-cp38-linux_x86_64.whl
pip install torch-geometric

使用九天

conda info -e
conda activate pytorch1.8

conda list
conda update -n base conda

conda install python==3.8
conda install pytorch torchvision torchaudio cudatoolkit=11.1 -c pytorch
conda list

python
>>> import torch
>>> torch.__version__
>>>torch.cuda.is_available()


conda install torch_scatter-2.0.6-cp38-cp38-linux_x86_64.whl
pip install torch_sparse-0.6.9-cp38-cp38-linux_x86_64.whl
pip install torch_cluster-1.5.9-cp38-cp38-linux_x86_64.whl
pip install torch_spline_conv-1.2.1-cp38-cp38-linux_x86_64.whl
pip install torch-geometric

安装之前需要查看服务器的cuda版本,
再查询cuda与pytorch版本的对应关系,
找到合适的pytorch进行安装。

torch版本适配

python=3.8​​​​​​​

           torch==1.8.0+cu111
           torch-scatter==2.0.6
           torch-cluster==1.5.9
           torch-sparse==0.6.9
           torch-geometric==1.6.3

python=3.7

           torch==1.1.0
           torch-scatter==1.3.2
           torch-cluster==1.4.5
           torch-sparse==0.4.3
           torch-spline-conv==1.1.1
           torch-geometric==1.3.2
           torchvision==0.3.0

python==3.6

     torch==1.3.0
     torch-scatter==1.3.2
     torch-cluster==1.4.5
     torch-sparse==0.4.3
     torch-spline-conv==1.1.1
     torch-geometric==1.3.2
     torchvision==0.4.1

docker 安装

通过docker安装
在有些情况下是需要使用docker来跑深度学习环境的(现在很多大公司都是使用paas平台来部署的)。那么我们就需要使用pytorch官方的docker镜像了。我们可以在docker hub上去搜索相关镜像,https://registry.hub.docker.com/。下图是我搜索的pytorch字段的结果(点击Tags后)。

我们可以看到当前最新的docker 镜像有pytorch/pytorch:1.8.1-cuda11.1-cudnn8-devel和pytorch/pytorch:1.8.1-cuda11.1-cudnn8-runtime,对于普通开发者下载pytorch/pytorch:1.8.1-cuda11.1-cudnn8-runtime就行了。关于安装docker的过程这里不赘述。

1)我们直接通过以下指令就能pull这个镜像了

docker pull pytorch/pytorch:1.8.1-cuda11.1-cudnn8-runtime
1
2)注意,在启动镜像前需要确保已安装NVIDIA Container Toolkit,否则会报错(若已安装可直接跳过此步骤):

docker: Error response from daemon: could not select device driver “” with capabilities: [[gpu]].
安装NVIDIA Container Toolkit,参考官方文档:https://github.com/NVIDIA/nvidia-docker
这里以Centos7为例:

首先根据你的系统类型以及版本下载对应.repo文件到/etc/yum.repos.d
distribution=$(. /etc/os-release;echo I D ID IDVERSION_ID)
&& curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.repo | sudo tee /etc/yum.repos.d/nvidia-docker.repo

清空yum的过期缓存数据(如果不是root用户需要加sudo)
yum clean expire-cache
安装NVIDIA Container Toolkit(如果不是root用户需要加sudo)
yum install -y nvidia-docker2
重启docker服务(如果不是root用户需要加sudo)
systemctl restart docker
3)通过docker启动pytorch1.8.1容器
docker run --gpus all --rm -it --ipc=host pytorch/pytorch:1.8.1-cuda11.1-cudnn8-runtime
4)进入容器后可以通过nvidia-smi看到所有的GPU设备信息

5)接着进入python环境简单测试下pytorch能否正常调用GPU(打印True为成功)

import torch
torch.cuda.is_available()

### 解决 PyCharm 远程 SSH 连接时 Conda 找不到的问题 当通过 PyCharm 使用远程解释器配置并通过 SSH 配置环境时,如果遇到 `conda` 不被识别的情况,通常是因为 `.bashrc` 或者 `.zshrc` 文件中的路径未正确加载或者环境变量设置不完整。以下是可能的原因以及解决方案: #### 可能原因分析 1. **Shell 初始化脚本未执行** 当通过 SSH 登录到服务器时,默认情况下可能会跳过某些初始化脚本(如 `.bashrc`),这可能导致 Conda 的路径未正确加载[^1]。 2. **Conda 路径未添加到 PATH 中** 如果用户的 Shell 初始化脚本未能正确导出 Conda 的安装路径,则 Python 环境无法找到 Conda 命令[^3]。 3. **SSH 会话模式不同** 某些服务器上的默认行为可能是非交互式的登录 shell,这种情况下不会自动运行 `.bash_profile` 或 `.bashrc` 文件[^2]。 --- #### 解决方案 ##### 方法一:修改 Shell 初始化脚本 确保在用户家目录下的 `.bashrc` 或 `.bash_profile` 文件中包含以下内容: ```bash export PATH="/path/to/conda/bin:$PATH" source /path/to/conda/etc/profile.d/conda.sh ``` 上述命令的作用是将 Conda 添加到系统的 PATH 环境变量中,并激活其功能支持。注意替换 `/path/to/conda` 为实际的 Conda 安装路径。 ##### 方法二:强制加载初始化脚本 在 PyCharm远程解释器配置中,可以尝试指定自定义的启动命令来手动加载必要的环境变量。例如,在 “Deployment” 设置页面下,勾选“Use this command instead of the default one”,并输入如下命令: ```bash bash --login -i -c "/usr/bin/python3 $@" ``` 此方法可确保每次连接都以交互方式运行 Bash 并加载相应的初始化脚本。 ##### 方法三:直接调用绝对路径 如果仍然存在问题,可以直接使用 Conda 的全路径代替简单的 `conda` 命令。假设 Conda 安装在 `/opt/miniconda3/` 下面,则可以在调试或运行前缀中显式写入完整的路径: ```bash /opt/miniconda3/bin/conda activate myenv && python script.py ``` ##### 方法四:调整 SSH Server 配置 有时需要编辑服务器端的 SSH 配置文件 (`/etc/ssh/sshd_config`) 来允许更灵活的行为。具体来说,确认下面两个选项已启用: ```plaintext PermitUserEnvironment yes ForceCommand none ``` 重启 SSH 服务使更改生效: ```bash sudo systemctl restart sshd ``` --- ### 示例代码片段 以下是一个典型的 PyCharm 配置流程示例,用于验证是否成功解决了问题: ```python import sys print(sys.executable) ``` 运行该程序后应返回类似于以下的结果,表明当前使用的正是由 Conda 提供的 Python 版本: ```plaintext /path/to/conda/envs/myenv/bin/python ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值