一、python 运行环境、虚拟环境与包管理
1、Python 独立安装(不使用开发工具自动安装,如PyCharm、Anaconda)
1)安装位置目录,设置于 环境变量 “Path” 中,方便命令行使用;
2)安装内容解释:
Scripts 目录: 一些执行文件,比如 pip.exe。可以用 pip 安装其它包管理工具,如 virtualenv.exe、uv.exe、poetry.exe。
Lib 文件夹:通常包含的是 Python 标准库 和 一些第三方库的源代码。这些库在 Python 解释器运行时被加载和使用。
Lib \site-packages 目录: 通过所属Python的 pip 安装的第三方库,存方的位置。如果虚拟环境激活,则安装在相应的虚拟环境对应目录下。
libs 目录:主要包含的是与 Python 解释器自身运行相关的动态链接库(.lib 文件)。include目录:包含头文件(XXX.h)。
DLLs目录 :包含 .dll文件、.pyd 文件。tcl 目录:Tcl/Tk GUI 工具包。
3) 可为Python 设置一个环境变量:“PYTHONPATH”。在所有环境(含虚拟)中都可用,用于指定额外的搜索路径,以便Python解释器可以找到要导入的模块和包。
4) pip 的包源设置: 使用 pip config --user [或--global] debug 进行查询 global(全局)/site(虚拟环境)/user(用户) 级配置文件 pip.ini 是否存在。
首先检测一下pip 版本, pip -V ( 或 --version ) , 然后升级一下: pip install --upgrade (或-U) pip
一般手动设置一个用户级配置就OK了,位置 C:\Users\UserNameXXX\pip\pip.ini ,可用命令: pip config --user set xxx 进行设置,使用国内镜像时确保添加 trusted-host,避免 SSL 错误。 一般源设置为(清华,网上推荐):
[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple //清华 , 已试过,比官方默认快很多! 官方网:https://mirrors.tuna.tsinghua.edu.cn/ extra-index-url = https://mirrors.aliyun.com/pypi/simple //阿里?
https://mirrors.cloud.tencent.com/pypi/simple
trusted-host =
pypi.tuna.tsinghua.edu.cn
mirrors.aliyun.com
mirrors.cloud.tencent.com
腾讯云镜像源
index-url = https://mirrors.cloud.tencent.com/pypi/simple
trusted-host = mirrors.cloud.tencent.com
华为云镜像源
index-url = https://mirrors.huaweicloud.com/repository/pypi/simple
trusted-host = mirrors.huaweicloud.com
2、Anaconda 安装并创建 虚拟环境
1)安装位置目录,可设置于 环境变量 Path 中,但官方不建议这样做; 安装目录 属于 base environment 。
2)设置 虚拟环境目录 env_dirs, 这样可以保证在使用 Anaconda Navigator 创建 Anaconda虚拟环境时,直接放置在设置的位置。
默认另一个位置是:C:\Users\\UserNameXXX\.conda\envs,使用 conda info 命令可以查看到(envs directories)。
当 PyCharm 用 Conda 选择虚拟环境时。建议!:不用 PyCharm 在 Anaconda 的 envs 中创建虚拟环境。
A、修改User用户级 或Site级配置文件 .condarc,增加:
envs_dirs: - X:\XXXX\miniconda3安装目录\envs
B、使用命令增加 : conda config --add envs_dirs X:\XXXX\Anaconda3\envs
3) 使用 命令:conda info 检查是否安装、配置成功。
4) C:\Users\UserNameXXX下的: .condarc 是conda 的用户级配置文件(user config file),配置下载包的源头,配置选项会覆盖conda的默认行为。
anaconda 应用安装目录下也用一个(Site级,优先级最高) .condarc (populated config files)。
A、可用 conda config 命令配置,或直接修改。
示例 : conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
B、或者 通过 Anaconda Navigator 的 Channels 功能 Add...(增加)。用户级 channel 放后面。前面内容是Anaconda 安装目录下 .condarc的 channel 内容。
主要配置选项如下:
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
show_channel_urls: true
channel_priority: strict (默认) 或 flexible 或 disabled
envs_dirs:
- /path/to/envs
- /another/path/to/envs
pkgs_dirs: 默认为: 安装目录 X:\xxx\anaconda3\pkgs 或 C:\Users\UserNameXXX\.conda\pkgs
- /path/to/pkgs
- /another/path/to/pkgs
3、uv、poetry 等 虚拟环境与包管理工具,建议!:直接安装在 特定 Python 安装下的 Scripts 目录,这必须用特定于 当前 Python 带的 pip 工具。
如果不指定,通常安装于用户工作目录。
4、PyCharm配置
1)PyCharm 安装中有一个选择项,Add launchers dir to the PATH,(PyCharm = X:\PyCharmXXXX\bin)方便在控制台中命令行启动。其实我们一般都是用 “桌面图标icon” 或启动菜单。
2)关键是 Python Interpreter(解释器)配置,在 File\Settings\Project:XXXX 处设置。
支持多种 虚拟环境创建工具:Uv、Poetry、Virtualenv、Pipenv、Conda。 本人仅考虑使用:Virtualenv。Uv(网上说最快)总产生格式不对的错误,Poetry 也没用对。
在选择 Python Interpreter 的下拉框的最下面,有一个 Show All... 可以清理一些 历史记录。