# Python环境 ## pip [更换pip源](https://developer.aliyun.com/article/652884) ```shell= # 临时 pip install scrapy -i https://pypi.tuna.tsinghua.edu.cn/simple # 永久 vi $HOME/.config/pip/pip.conf pip config set global.index-url 'https://mirrors.aliyun.com/pypi/simple' pip config set global.timeout '120' pip config set global.trusted-host 'mirrors.aliyun.com' ``` ```shell= # 查看pip配置 pip config list # 编辑配置 pip config edit --editor vim # 设置配置 pip config set global.index-url \ 'https://cmc.centralrepo.rnd.huawei.com/pypi/simple/' global.index-url='http://cmc-cd-mirror.rnd.huawei.com/pypi/simple/' install.trusted-host='cmc-cd-mirror.rnd.huawei.com' # proxy [global] index-url = https://cmc.centralrepo.rnd.huawei.com/pypi/simple/ trusted-host = cmc.centralrepo.rnd.huawei.com download.pytorch.org find-links = https://download.pytorch.org/whl/torch_stable.html timeout = 120 proxy = http://127.0.0.1:3128/ # huawei [global] index-url = https://mirrors.tools.huawei.com/pypi/simple trusted-host = mirrors.tools.huawei.com timeout = 120 ``` ## conda ### 安装 [官网教程](https://conda.io/projects/conda/en/latest/user-guide/install/linux.html) [安装包地址](https://www.anaconda.com/products/distribution) [anaconda - sjtu](https://mirror.sjtu.edu.cn/docs/anaconda) ```shell= wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2022.10-Linux-x86_64.sh https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2023.03-Linux-x86_64.sh ``` ### 基本操作 ```shell= # 检查python版本 conda search --full --name python #创建虚拟环境 conda create -n zr python=3.10.8 #激活虚拟环境 conda activate zr #退出虚拟环境 conda deactivate #删除虚拟环境 conda remove -n zr --all #查看当前存在哪些虚拟环境 conda env list #或 conda info -e #或 conda info --envs # 查看所有配置信息 conda config --show --json # 配置使用清华的源 conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config --set show_channel_urls yes # 配置安装软件时不要提示输入yes conda config --set always_yes true ``` ### condarc ```shell= auto_activate_base: false channels: - defaults default_channels: - http://mirrors.aliyun.com/anaconda/pkgs/main - http://mirrors.aliyun.com/anaconda/pkgs/r - http://mirrors.aliyun.com/anaconda/pkgs/msys2 custom_channels: conda-forge: http://mirrors.aliyun.com/anaconda/cloud msys2: http://mirrors.aliyun.com/anaconda/cloud bioconda: http://mirrors.aliyun.com/anaconda/cloud menpo: http://mirrors.aliyun.com/anaconda/cloud pytorch: http://mirrors.aliyun.com/anaconda/cloud simpleitk: http://mirrors.aliyun.com/anaconda/cloud show_channel_urls: true proxy_servers: http: 127.0.0.1:3128 https: 127.0.0.1:3128 ssl_verify: false ``` ### 安装和配置 [云道DevContainer Conda源配置](https://3ms.huawei.com/hi/group/3942456/wiki_6827138.html) ```shell= bash Anaconda3-2022.10-Linux-x86_64.sh # 自动激活环境 conda config --set auto_activate_base false # 源和代理 # 有阿里源、清华源、上海交通源等等 vim ~/.condarc # windows配置文件地址 C:\Users\z00581357\.condarc #查看当前conda配置 conda config --show channels 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 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/ #设置搜索是显示通道地址 conda config --set show_channel_urls yes auto_activate_base: false default_channels: - https://mirror.sjtu.edu.cn/anaconda/pkgs/r - https://mirror.sjtu.edu.cn/anaconda/pkgs/main custom_channels: conda-forge: https://mirror.sjtu.edu.cn/anaconda/cloud/ pytorch: https://mirror.sjtu.edu.cn/anaconda/cloud/ channels: - defaults show_channel_urls: true proxy_servers: http: 10.108.154.137:3128 https: 10.108.154.137:3128 ssl_verify: false # 清除索引缓存 conda clean -i ``` ```shell= channel_alias: http://10.155.97.225:8088/repository/conda-proxy default_channels: - main - r - conda-forge channel_priority: strict show_channel_urls: true ``` [Pycharm + Conda,实现不同版本python共存](http://3ms.huawei.com/km/blogs/details/13226093) ```shell= channels: - defaults channel_priority: strict show_channel_urls: true channel_alias: http://10.243.65.94:8088/repository/conda-proxy default_channels: - main custom_channels: conda-forge: http://10.243.65.94:8088/repository/conda-extra-proxy pytorch: http://10.243.65.94:8088/repository/conda-extra-proxy ``` ### python版本管理 ```shell= # 查看当前版本 conda --version # 更新conda conda update conda # 查看可用的python版本 conda search --full-name python # 安装不同版本的python conda create --name zxh python=3.11.3 ``` ### 环境管理 ```shell= # 创建一个名为snowflakes的环境 conda create --name snowflakes python=3.7 # 激活环境 conda activate snowflakes # 退出环境 conda deactivate # 查看所有的环境 conda info --envs # 复制snowflakes环境到flowers conda create --name flowers --clone snowflakes # 删除环境 conda remove --name flowers --all ``` ### packages管理 ```shell= # 查看当前环境下的已经安装的包 conda list # 查找包 conda search beautifulsoup4 # 安装包到环境bunnies,不加--name时,默认安装到当前环境 conda install --name bunnies beautifulsoup4 # 删除指定环境里的包 conda remove --name bunnies iopro ``` ## env ### proxy ```python= os.environ["http_proxy"] = "http://127.0.0.1:7890" os.environ["https_proxy"] = "http://127.0.0.1:7890" ``` ```python= import os os.environ["http_proxy"] = '' os.environ["https_proxy"] = '' os.environ["no_proxy"] = "*" ``` ## SSL ```shell= import ssl ssl._create_default_https_context = ssl._create_unverified_context ``` ```shell= import os os.environ['CURL_CA_BUNDLE'] = '' os.environ["http_proxy"] = 'http://10.108.218.221:3009/' os.environ["https_proxy"] = 'http://10.108.218.221:3009/' ``` ```shell= export CURL_CA_BUNDLE="" D:\app\anaconda3\envs\aiagent\Lib\site-packages\requests cd /d/app/anaconda3/envs/langchain/Lib/site-packages/requests /root/anaconda3/envs/dify/lib/python3.10/site-packages/requests/sessions.py os.environ.get("CURL_CA_BUNDLE") 修改这里的verify值 # 增加 verify = '' ``` ```shell= import urllib3 urllib3.disable_warnings(urllib3.connectionpool.InsecureRequestWarning) ``` ### 企业证书 ```shell= # 1. 合并所有华为证书到一个文件 cd /path/to/ca-certificates # 进入证书目录 cat HuaweiITMiniRootCA.crt \ HuaweiITRootCA.crt \ HuaweiSecureInternetPorxyCA.crt \ HuaweiWebSecureInternetGatewayCA.crt \ HWITEnterprise.crt > ~/huawei-ca-bundle.crt # 2. 设置环境变量 export SSL_CERT_FILE=~/huawei-ca-bundle.crt export REQUESTS_CA_BUNDLE=~/huawei-ca-bundle.crt # 3. 安装 Python uv python install 3.12 3.10 ``` # 软件 ## JupyterLab ```shell= # conda conda install -c conda-forge jupyterlab # pip pip install jupyterlab # 启动 jupyter lab --allow-root jupyter-lab --no-browser --ip "51.36.139.26" --port 5678 # 配置文件 jupyter lab --generate-config /Users/zhangxinhao/.jupyter/jupyter_lab_config.py c.ServerApp.notebook_dir = '/Users/zhangxinhao/code/test/demo/python' jupyter server password vi /home/zhangxinhao/.jupyter/jupyter_lab_config.py c.ServerApp.open_browser = False c.ServerApp.ip = '*' c.ServerApp.port = 9003 c.ServerApp.root_dir = '/opt/code/python/notebook' ``` ### 后台启动 &让命令后台运行,并把标准输出 写入jupyterlab.log中。 nohup 表示no hang up ,就是不挂起,这个命令执行后即使终端退出,jupyter 也不会停止运行。 ```shell= nohup jupyter lab --allow-root > jupyterlab.log 2>&1 & ``` ## pytorch ### install [Get Started](https://pytorch.org/get-started/locally/) ```shell= # mac conda install pytorch::pytorch torchvision torchaudio -c pytorch ``` ## gradio ```shell= pip install --trusted-host mirrors.tools.huawei.com -i https://mirrors.tools.huawei.com/pypi/simple gradio ``` ## Poetry [python-poetry](https://python-poetry.org/)