因为之前重装了系统,很多软件设置需要恢复,特别是Python环境
。最近小玩了一下Pyqt
,接触到了Anaconda
这个巨无霸软件
看了官网介绍它的用途,觉得完全用不到这么多功能,安装Anaconda
后文件已经到达10G
再翻了翻发现还有个类似精简版的Miniconda
,觉得可以用来管理虚拟环境
这篇文记录安装和使用过程
下载Miniconda
官网下载链接:https://docs.anaconda.com/free/miniconda/miniconda-other-installer-links/
本着稳定能用的优先原则,并考虑到之前编写的代码一直使用Python3.10版本,我选择这个版本
注意:使用这个版本只是为了全局使用Python3.10,仍然可以通过指定高版本的Python创建虚拟环境,安装完成后可以通过命令conda search python
查看虚拟环境支持的版本
安装Miniconda
基本一路Next按钮,以下几个步骤需注意
安装类型,由于我的电脑叫PC,我选Just Me
虽然是Mini版本,但是安装完成后占用的磁盘空间还是挺大,选其他盘
高级选项
- 第一个复选框是创建开始菜单(觉得用处不大,保持默认)
- 第二个复选框是添加软件的环境变量,红字已经提示不推荐并列明了原因,也可以不勾后面自己手动去添加环境变量
- 第三个复选框是指定Python3.10为默认版本,相当于给电脑安装Python,所以如果在安装Miniconda前安装了Python,原先安装的就用不到了,可删;安装完成后Python的路径在minicoda3路径下;如果你不是这重装系统的情况,建议先把原来使用的全局环境或虚拟环境导出requirements.txt(pip freeze > requirements.txt),以便于恢复全局环境或虚拟环境
设置Miniconda
这是清华开源软件镜像站的一个网页,Anaconda 镜像使用帮助https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/
我看了一下目前用不到这些内容,conda软件自带包管理(它本身就是管理),例如
安装包:
conda install package_name 安装满足依赖关系的最新版本的包
conda install package_name=x.x 安装指定版本的包
pip install netmiko==3.4.0 安装指定版本的包
pip install netmiko>=3.4.0 安装指定版本及以上的包
卸载包:
conda uninstall xxx,conda remove xxx
pip uninstall SomePackage
我还是习惯用pip命令,但是还是照着网页的说明操作了,也不知道为什么
基本使用
查看全局的Python版本和Python路径
D:\>python -V Python 3.10.13 D:\>where python D:\miniconda3\python.exe
结果显示当前的Python版本是3.10.13,安装路径是D:\miniconda3\python.exe,以上输出表明安装完成
查看conda信息,命令:
conda info
结果显示
package cache
和envs directories
的路径在D:\miniconda3\
, package cache是缓存已下载的包文件,envs directories是存放虚拟环境文件创建一个名称为
pyqt-base
的虚拟环境(指定Python版本3.8,默认安装3.8的最高版本,也可以指定具体的版本如python=3.8.8)D:\>conda create -n pyqt-base python=3.8 Channels: - defaults Platform: win-64 Collecting package metadata (repodata.json): done Solving environment: done ## Package Plan ## environment location: D:\miniconda3\envs\pyqt-base added / updated specs: - python=3.8 The following packages will be downloaded: package | build ---------------------------|----------------- ca-certificates-2023.12.12 | haa95532_0 127 KB defaults pip-23.3.1 | py38haa95532_0 2.8 MB defaults python-3.8.18 | h1aa4202_0 20.5 MB defaults setuptools-68.2.2 | py38haa95532_0 933 KB defaults sqlite-3.41.2 | h2bbff1b_0 894 KB defaults vc-14.2 | h21ff451_1 8 KB defaults wheel-0.41.2 | py38haa95532_0 126 KB defaults ------------------------------------------------------------ Total: 25.3 MB The following NEW packages will be INSTALLED: ca-certificates anaconda/pkgs/main/win-64::ca-certificates-2023.12.12-haa95532_0 libffi anaconda/pkgs/main/win-64::libffi-3.4.4-hd77b12b_0 openssl anaconda/pkgs/main/win-64::openssl-3.0.13-h2bbff1b_0 pip anaconda/pkgs/main/win-64::pip-23.3.1-py38haa95532_0 python anaconda/pkgs/main/win-64::python-3.8.18-h1aa4202_0 setuptools anaconda/pkgs/main/win-64::setuptools-68.2.2-py38haa95532_0 sqlite anaconda/pkgs/main/win-64::sqlite-3.41.2-h2bbff1b_0 vc anaconda/pkgs/main/win-64::vc-14.2-h21ff451_1 vs2015_runtime anaconda/pkgs/main/win-64::vs2015_runtime-14.27.29016-h5e58377_2 wheel anaconda/pkgs/main/win-64::wheel-0.41.2-py38haa95532_0 Proceed ([y]/n)? y Downloading and Extracting Packages: Preparing transaction: done Verifying transaction: done Executing transaction: done # # To activate this environment, use # # $ conda activate pyqt-base # # To deactivate an active environment, use # # $ conda deactivate D:\>
命令:conda create -n pyqt-base python=3.8
虚拟环境查看
D:\>conda activate pyqt-base (pyqt-base) D:\>python -V Python 3.8.18 (pyqt-base) D:\>pip list Package Version ---------- ------- pip 23.3.1 setuptools 68.2.2 wheel 0.41.2 (pyqt-base) D:\>
结果显示Python版本为Python 3.8.18,已安装的包有三个,窗口的提示符行前面多了虚拟环境的名称。
conda设置全局pip国内源
(pyqt-base) D:\>pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple Writing to C:\Users\xxx\AppData\Roaming\pip\pip.ini (pyqt-base) D:\>pip config list global.index-url='https://pypi.tuna.tsinghua.edu.cn/simple' (pyqt-base) D:\>
结果显示,在C盘的C:\Users\xxx\AppData\Roaming\pip\创建了一个pip.ini文件,查看配置可以看到对应配置
在虚拟环境下安装pyqt5
(pyqt-base) D:\>pip install pyqt5 Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting pyqt5 Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ca/ac/596e8ca16fd0634542d874c0d79219fc527ea7de73a5000092f60ecbf6e9/PyQt5-5.15.10-cp37-abi3-win_amd64.whl (6.8 MB) ---------------------------------------- 6.8/6.8 MB 3.3 MB/s eta 0:00:00 Collecting PyQt5-sip<13,>=12.13 (from pyqt5) Downloading https://pypi.tuna.tsinghua.edu.cn/packages/88/cd/dd21cdb92d053ca71c02c75ab7bd32874b82b33bef61d6d70b5d898e684b/PyQt5_sip-12.13.0-cp38-cp38-win_amd64.whl (78 kB) ---------------------------------------- 78.3/78.3 kB 189.0 kB/s eta 0:00:00 Collecting PyQt5-Qt5>=5.15.2 (from pyqt5) Downloading https://pypi.tuna.tsinghua.edu.cn/packages/37/97/5d3b222b924fa2ed4c2488925155cd0b03fd5d09ee1cfcf7c553c11c9f66/PyQt5_Qt5-5.15.2-py3-none-win_amd64.whl (50.1 MB) ---------------------------------------- 50.1/50.1 MB 7.8 MB/s eta 0:00:00 Installing collected packages: PyQt5-Qt5, PyQt5-sip, pyqt5 Successfully installed PyQt5-Qt5-5.15.2 PyQt5-sip-12.13.0 pyqt5-5.15.10 (pyqt-base) D:\>pip list Package Version ---------- ------- pip 23.3.1 PyQt5 5.15.10 PyQt5-Qt5 5.15.2 PyQt5-sip 12.13.0 setuptools 68.2.2 wheel 0.41.2 (pyqt-base) D:\>
一些conda命令
Command | Useage |
---|---|
conda create -n myenv sqlite | python=x.x | Create an environment containing the package sqlite or python |
conda remove -n myenv –all | Remove all packages from environment myenv and the environment itself |
conda env list | List the Conda environments |
conda clean –all | Remove index cache, lock files, unused cache packages, tarballs, and logfiles |
conda clean –i | Remove index cache |
conda clean –p | Remove unused packages from writable package caches. WARNING: This does not check for packages installed using symlinks back to the package cache. |
conda search xxx | Search for packages and display |
conda activate myenv | To activate this environment |
conda deactivate | To deactivate an active environment |
conda create -n env2 –clone path/to/file/env1 | Create an environment (env2) as a clone of an existing environment (env1) |