pip安装包、conda 安装环境 、linux修改conda镜像源、python生成以及安装项目配置文件
当我们在一台新的设备,或者新的环境中运行一个项目的时候,需要在环境中安装项目需要的配置文件,否则运行项目就会各种报错。这里介绍一种简单的安装“项目所需配置文件”得快速方法。
conda安装虚拟环境
1、创建虚拟环境
conda create --name you-env-name python=x.x.x
conda create -n you-env-name python=x.x.x
执行以上操作可能会报:…URL 错误,需要将镜像源改为国内镜像源。执行如下操作:
2、修改conda源(linux)
2.1、打开conda镜像源配置文件:
vim ~/.condarc
2.2、删除所有原内容,复制以下内容
channels:
- defaults
show_channel_urls: true
default_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
custom_channels:
conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
注意,有时候换了镜像源还是会报错:
Solving environment: failed
CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/linux-64/repodata.json>
Elapsed: -
An HTTP error occurred when trying to retrieve this URL.
HTTP errors are often intermittent, and a simple retry will get you on your way.
ConnectionError(MaxRetryError('HTTPSConnectionPool(host=\'mirrors.tuna.tsinghua.edu.cn\', port=443): Max retries exceeded with url: /anaconda/cloud/menpo/linux-64/repodata.json (Caused by ReadTimeoutError("HTTPSConnectionPool(host=\'mirrors.tuna.tsinghua.edu.cn\', port=443): Read timed out. (read timeout=9.15)"))'))
!!!!需要把源的 https 换成 http
2.3、清除索引缓存(重要)
conda clean -i
再次执行创建虚拟环境语句
pip安装所需包
1、pip 安装
pip install some-package
2、pip国内镜像源(临时使用)
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
pip install some-package -i https://pypi.tuna.tsinghua.edu.cn/simple
生成配置文件
1、第一步:进入工程目录
运行环境中进入项目目录下:
2、安装如下模块:
pip install pipreqs
3、生成配置文件
pipreqs ./ --encoding='utf-8'
然后可以看到在项目目录中就产生了配置文件requirements
安装配置文件
有了上边的配置文件,就可以在虚拟幻境或者新的设备环境中安装该项目所需要的配置文件
同样需要在环境中cd进入该项目然后:
pip install -r requirements.txt
补充:有时候生成文件的时候会报错:
requests.exceptions.ConnectionError: HTTPSConnectionPool(host=‘pypi.python.org’, port=443): Max retries exceeded with url: /pypi/Flask/json (Caused by NewConnectionError(‘<urllib3.connection.HTTPSConnection object at 0x000001C8926ACF40>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失 败。’))
这个是时候确保两点:
- 程序没有在编译器中打开
- 如果还是不行,可以将环境换成baes环境,生成文件以后再在虚拟环境重安装即可