Anaconda使用

Anaconda使用

Anaconda使用

一、安装

Anaconda官网安装地址
image.png

二、配置环境

记住Anaconda安装的路径,在环境变量中添加以下三个路径,例如我是安装在C:\ProgramData\Anaconda3,则添加
C:\ProgramData\Anaconda3
C:\ProgramData\Anaconda3\Scripts
C:\ProgramData\Anaconda3\Library\bin

image.png
配置完成后输入

conda --version

可以看到版本信息则配置正确
image.png

三、使用

1. 创建环境

conda create --name <env_name> <package_names>

例如

conda create --name test python=3.7 numpy pandas

其中--name可以是-n

2. 切换环境

  • 查看所有的环境
conda info --envs
或
conda info -e
或
conda env list

image.png
其中*所在的为当前环境

  • 切换环境
    输入
conda activate <env_name>
例如
conda activate test

就可切换到test环境

  • 退出当前环境
conda deactivate

会退回到base环境

三、安装包

对于Anaconda中已有的包,可以使用

conda install <package_names>
如
conda install numpy

对于Anaconda中没有的包,可以使用

pip install numpy
需要注意的是使用pip的时候会把包安装到哪里,使用pip -V可以查看当前pip的环境,如果当前环境不存在pip,需要使用conda install pip,否则使用pip会将包安装到其他地方,可能导致版本冲突

四、windows环境使用中遇到的部分问题

1. Anaconda Navigator打不开

  • 问题
    弹出网页报错
Navigator Error
An unexpected error occurred on Navigator start-up

Report
Please report this issue in the anaconda issue tracker

Main Error
check_hostname requires server_hostname
Traceback
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\anaconda_navigator\exceptions.py", line 72, in exception_handler
    return_value = func(*args, **kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\anaconda_navigator\app\start.py", line 146, in start_app
    window = run_app(splash)
  File "C:\ProgramData\Anaconda3\lib\site-packages\anaconda_navigator\app\start.py", line 65, in run_app
    window = MainWindow(splash=splash)
  File "C:\ProgramData\Anaconda3\lib\site-packages\anaconda_navigator\widgets\main_window.py", line 165, in __init__
    self.api = AnacondaAPI()
  File "C:\ProgramData\Anaconda3\lib\site-packages\anaconda_navigator\api\anaconda_api.py", line 1518, in AnacondaAPI
    ANACONDA_API = _AnacondaAPI()
  File "C:\ProgramData\Anaconda3\lib\site-packages\anaconda_navigator\api\anaconda_api.py", line 83, in __init__
    self._client_api = ClientAPI(config=self.config)
  File "C:\ProgramData\Anaconda3\lib\site-packages\anaconda_navigator\api\client_api.py", line 659, in ClientAPI
    CLIENT_API = _ClientAPI(config=config)
  File "C:\ProgramData\Anaconda3\lib\site-packages\anaconda_navigator\api\client_api.py", line 95, in __init__
    self.reload_client()
  File "C:\ProgramData\Anaconda3\lib\site-packages\anaconda_navigator\api\client_api.py", line 326, in reload_client
    client.user()
  File "C:\ProgramData\Anaconda3\lib\site-packages\binstar_client\__init__.py", line 244, in user
    res = self.session.get(url, verify=self.session.verify)
  File "C:\ProgramData\Anaconda3\lib\site-packages\requests\sessions.py", line 555, in get
    return self.request('GET', url, **kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\requests\sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\requests\sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\requests\adapters.py", line 439, in send
    resp = conn.urlopen(
  File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 696, in urlopen
    self._prepare_proxy(conn)
  File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 964, in _prepare_proxy
    conn.connect()
  File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\connection.py", line 359, in connect
    conn = self._connect_tls_proxy(hostname, conn)
  File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\connection.py", line 500, in _connect_tls_proxy
    return ssl_wrap_socket(
  File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\util\ssl_.py", line 432, in ssl_wrap_socket
    ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls)
  File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\util\ssl_.py", line 474, in _ssl_wrap_socket_impl
    return ssl_context.wrap_socket(sock)
  File "C:\ProgramData\Anaconda3\lib\ssl.py", line 500, in wrap_socket
    return self.sslsocket_class._create(
  File "C:\ProgramData\Anaconda3\lib\ssl.py", line 997, in _create
    raise ValueError("check_hostname requires server_hostname")
ValueError: check_hostname requires server_hostname
  • 解决
    这种情况下将网络代理设置为关闭即可
    image.png

2. 无法加载文件

  • 问题
    当使用conda init <shell_name>后,打开终端,提示 “无法加载文件C:\XXX\WindowsPowerShell\profile.ps1,因为在此系统上禁止运行脚本” 的错误
  • 解决
    这是安全策略的问题。
    使用管理员模式打开powershell,
    输入
set-ExecutionPolicy RemoteSigned

并选择同意即可