conda环境配置

conda环境是python中相当于工具包的一个存在,因为python的使用中需要调用大量的库。不同的库之间难免有版本冲突和不同的Python版本依赖。

为了解决这个问题,Python开发者提出了conda环境的概念,可以把库和python版本安装到虚拟环境中,运行时在不同的Python虚拟环境里面运行即可。

步骤如下:

  • 安装Anaconda
  • 找到Anaconda Prompt
  • 打开Anaconda Prompt,创建自己的新环境,配置python版本

    现在以python3.11为例,math是这个环境的名字

    1
    conda create -n math python=3.11
  • 后续激活操作
    后续可以在Anaconda Prompt中或者其他编译器的Python控制台输入下列指令去激活环境
    1
    2
    3
    4
    5
    # To activate this environment, use
    $ conda activate math

    # To deactivate an active environment, use
    $ conda deactivate
  • 镜像源配置
    1
    2
    3
    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/conda-forge/
    可利用指令检查conda config --show
    提示:
    如果想在cmd里面像Anaconda Prompt一样使用conda命令,需要激活conda环境
    C:\Users\21013> D:\apps\Anaconda3\Scripts\activate.bat
    输入这行命令即可激活base环境,后续操作与上面类似

pytorch库安装

官网已经不提供conda,尝试pip下载
报错:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
(pytorch_learn) C:\Users\21013>pip3 install torch torchvision torchaudio
ERROR: Exception:
Traceback (most recent call last):
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_internal\cli\base_command.py", line 173, in _main
status = self.run(options, args)
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_internal\cli\req_command.py", line 203, in wrapper
return func(self, options, args)
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_internal\commands\install.py", line 316, in run
reqs, check_supported_wheels=not options.target_dir
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_internal\resolution\resolvelib\resolver.py", line 95, in resolve
collected.requirements, max_rounds=try_to_avoid_resolution_too_deep
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_vendor\resolvelib\resolvers.py", line 472, in resolve
state = resolution.resolve(requirements, max_rounds=max_rounds)
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_vendor\resolvelib\resolvers.py", line 341, in resolve
self._add_to_criteria(self.state.criteria, r, parent=None)
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_vendor\resolvelib\resolvers.py", line 172, in _add_to_criteria
if not criterion.candidates:
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_vendor\resolvelib\structs.py", line 151, in __bool__
return bool(self._sequence)
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_internal\resolution\resolvelib\found_candidates.py", line 140, in __bool__
return any(self)
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_internal\resolution\resolvelib\found_candidates.py", line 128, in <genexpr>
return (c for c in iterator if id(c) not in self._incompatible_ids)
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_internal\resolution\resolvelib\found_candidates.py", line 29, in _iter_built
for version, func in infos:
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_internal\resolution\resolvelib\factory.py", line 275, in iter_index_candidate_infos
hashes=hashes,
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_internal\index\package_finder.py", line 851, in find_best_candidate
candidates = self.find_all_candidates(project_name)
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_internal\index\package_finder.py", line 798, in find_all_candidates
page_candidates = list(page_candidates_it)
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_internal\index\sources.py", line 134, in page_candidates
yield from self._candidates_from_page(self._link)
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_internal\index\package_finder.py", line 758, in process_project_url
html_page = self._link_collector.fetch_page(project_url)
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_internal\index\collector.py", line 490, in fetch_page
return _get_html_page(location, session=self.session)
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_internal\index\collector.py", line 400, in _get_html_page
resp = _get_html_response(url, session=session)
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_internal\index\collector.py", line 132, in _get_html_response
"Cache-Control": "max-age=0",
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_vendor\requests\sessions.py", line 555, in get
return self.request('GET', url, **kwargs)
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_internal\network\session.py", line 454, in request
return super().request(method, url, *args, **kwargs)
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_vendor\requests\sessions.py", line 542, in request
resp = self.send(prep, **send_kwargs)
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_vendor\requests\sessions.py", line 655, in send
r = adapter.send(request, **kwargs)
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_vendor\cachecontrol\adapter.py", line 53, in send
resp = super(CacheControlAdapter, self).send(request, **kw)
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_vendor\requests\adapters.py", line 449, in send
timeout=timeout
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_vendor\urllib3\connectionpool.py", line 696, in urlopen
self._prepare_proxy(conn)
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_vendor\urllib3\connectionpool.py", line 964, in _prepare_proxy
conn.connect()
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_vendor\urllib3\connection.py", line 359, in connect
conn = self._connect_tls_proxy(hostname, conn)
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_vendor\urllib3\connection.py", line 506, in _connect_tls_proxy
ssl_context=ssl_context,
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_vendor\urllib3\util\ssl_.py", line 453, in ssl_wrap_socket
ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls)
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\site-packages\pip\_vendor\urllib3\util\ssl_.py", line 495, in _ssl_wrap_socket_impl
return ssl_context.wrap_socket(sock)
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\ssl.py", line 407, in wrap_socket
_context=self, _session=session)
File "D:\apps\Anaconda3\envs\pytorch_learn\lib\ssl.py", line 773, in __init__
raise ValueError("check_hostname requires server_hostname")
ValueError: check_hostname requires server_hostname

报错了
最后还是用找到的conda指令:

1
conda install pytorch torchvision torchaudio cpuonly -c pytorch

安装了一个老掉牙的1.10版本,

可在Anaconda Prompt内使用pip list指令显示目前环境的包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
(pytorch_learn) C:\Users\21013>
(pytorch_learn) C:\Users\21013>pip list
Package Version
----------------- ---------
certifi 2021.5.30
dataclasses 0.8
mkl-fft 1.3.0
mkl-random 1.1.1
mkl-service 2.3.0
numpy 1.19.2
olefile 0.46
Pillow 8.3.1
pip 21.2.2
setuptools 58.0.4
six 1.16.0
torch 1.10.2
torchaudio 0.10.2
torchvision 0.11.3
typing_extensions 4.1.1
wheel 0.37.1
wincertstore 0.2

安装完成

Pycharm配环境

选择现有python解释器,找到之前配好的Anaconda里面的conda环境python.exe即可

熟悉python调包

1
2
3
4
dir()
# directory,目录,获得包内函数
help()
# 帮助,获得函数具体操作