【python】Anaconda及虚拟环境使用

一、基本介绍

在介绍Anaconda之前,我们使用python常见使用的IDE是pyCharm,而安装第三方包或python库而两种形式:
1.使用命令
pip install 包名
2.IDE内安装

界面.png
简单的包可以通过以上任意两种形式安装到本地环境,但复杂的工程情况下,存在两个问题:1.安装包存在依赖包(依赖的先后顺序不确定/多个)安装繁琐,2.本地环境安装多个包容易污染本地环境,不方便管理。而Anaconda正是为了解决优化以上问题而诞生。
Anaconda是一款可以获取包及对包进行管理、环境统一管理的软件。其中包含conda、Python在内的超过180个科学包及其依赖项。
虚拟/本机环境包管理.png

二、环境搭建(pyCharm+Anaconda)

1、安装pyCharm
2、安装Anaconda

三、基础命令

新增虚拟环境

(base) faria-2:anaconda3$ sudo conda create --name pymysql
Password:
Collecting package metadata (repodata.json): done
Solving environment: done

## Package Plan ##
  environment location: /Users/xxx/anaconda3/envs/pymysql

Proceed ([y]/n)? y
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate pymysql
#
# To deactivate an active environment, use
#
#     $ conda deactivate

删除keyconda config --remove-key channels
查看已有配置

(base) faria-2:anaconda3$ conda config --show
add_anaconda_token: True
add_pip_as_python_dependency: True
aggressive_update_packages:
  - ca-certificates
  - certifi
  - openssl
allow_conda_downgrades: False
allow_cycles: True
allow_non_channel_urls: False
allow_softlinks: False
always_copy: False
always_softlink: False
always_yes: None
anaconda_upload: None
auto_activate_base: True
auto_stack: 0
auto_update_conda: True
bld_path: 
changeps1: True
channel_alias: https://conda.anaconda.org
channel_priority: flexible
channels:
  - defaults
client_ssl_cert: None
client_ssl_cert_key: None
clobber: False
conda_build: {}
create_default_packages: []
croot: /Users/xxx/anaconda3/conda-bld
custom_channels:
  pkgs/main: https://repo.anaconda.com
  pkgs/r: https://repo.anaconda.com
  pkgs/pro: https://repo.anaconda.com
custom_multichannels:
  defaults: 
    - https://repo.anaconda.com/pkgs/main
    - https://repo.anaconda.com/pkgs/r

新增镜像(清华镜像用http)

(base) faria-2:anaconda3$ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/osx-64/

查看虚拟环境(现有的虚拟环境*)

(pymysql) faria-2:~$ conda env list
# conda environments:
#
base                     /Users/xxx/anaconda3
pymysql               *  /Users/xxx/anaconda3/envs/pymysql

切换虚拟环境

conda activate 环境名称

激活镜像

(base) faria-2:anaconda$ source activate pymysql

常见问题

1、使用的镜像(清华)用了https协议

CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/osx-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.
'https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/osx-64'

解决思路:重新添加镜像为http
2、新增虚拟环境报错

CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://repo.anaconda.com/pkgs/main/osx-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.
If your current network has https://www.anaconda.com blocked, please file
a support request with your network engineering team.

'https://repo.anaconda.com/pkgs/main/osx-64'

解决步骤:

(base) faria-2:anaconda3$ conda config --set ssl_verify no
(base) faria-2:anaconda3$ conda config --set ssl_verify false
(base) faria-2:anaconda3$ vim ~/.condarc
(base) faria-2:anaconda3$ sudo conda create --name pymysql

3、无法安装镜像

(base) faria-2:anaconda3 xxx$ sudo  conda install -c anaconda redis
Password:
Collecting package metadata (repodata.json): failed

# >>>>>>>>>>>>>>>>>>>>>> ERROR REPORT <<<<<<<<<<<<<<<<<<<<<<

    Traceback (most recent call last):
      File "/Users/xxx/anaconda3/lib/python3.7/site-packages/conda/exceptions.py", line 1080, in __call__
        return func(*args, **kwargs)
      File "/Users/xxx/anaconda3/lib/python3.7/site-packages/conda/cli/main.py", line 84, in _main
        exit_code = do_call(args, p)
      File "/Users/xxx/anaconda3/lib/python3.7/site-packages/conda/cli/conda_argparse.py", line 83, in do_call
        return getattr(module, func_name)(args, parser)
      File "/Users/xxx/anaconda3/lib/python3.7/site-packages/conda/cli/main_install.py", line 20, in execute
        install(args, parser, 'install')
      File "/Users/xxx/anaconda3/lib/python3.7/site-packages/conda/cli/install.py", line 263, in install
        should_retry_solve=(_should_retry_unfrozen or repodata_fn != repodata_fns[-1]),
      File "/Users/xxx/anaconda3/lib/python3.7/site-packages/conda/core/solve.py", line 117, in solve_for_transaction
        should_retry_solve)
      File "/Users/xxx/anaconda3/lib/python3.7/site-packages/conda/core/solve.py", line 158, in solve_for_diff
        force_remove, should_retry_solve)
      File "/Users/xxx/anaconda3/lib/python3.7/site-packages/conda/core/solve.py", line 262, in solve_final_state
        ssc = self._collect_all_metadata(ssc)
      File "/Users/xxx/anaconda3/lib/python3.7/site-packages/conda/common/io.py", line 88, in decorated
        return f(*args, **kwds)
      File "/Users/xxx/anaconda3/lib/python3.7/site-packages/conda/core/solve.py", line 425, in _collect_all_metadata
        index, r = self._prepare(prepared_specs)
      File "/Users/xxx/anaconda3/lib/python3.7/site-packages/conda/core/solve.py", line 1021, in _prepare
        self.subdirs, prepared_specs, self._repodata_fn)
      File "/Users/xxx/anaconda3/lib/python3.7/site-packages/conda/core/index.py", line 289, in get_reduced_index
        repodata_fn=repodata_fn)
      File "/Users/xxx/anaconda3/lib/python3.7/site-packages/conda/core/subdir_data.py", line 140, in query_all
        result = tuple(concat(executor.map(subdir_query, channel_urls)))
      File "/Users/xxx/anaconda3/lib/python3.7/concurrent/futures/_base.py", line 586, in result_iterator
        yield fs.pop().result()
      File "/Users/xxx/anaconda3/lib/python3.7/concurrent/futures/_base.py", line 432, in result
        return self.__get_result()
      File "/Users/xxx/anaconda3/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
        raise self._exception
      File "/Users/xxx/anaconda3/lib/python3.7/concurrent/futures/thread.py", line 57, in run
        result = self.fn(*self.args, **self.kwargs)
      File "/Users/xxx/anaconda3/lib/python3.7/site-packages/conda/core/subdir_data.py", line 133, in <lambda>
        package_ref_or_match_spec))
      File "/Users/xxx/anaconda3/lib/python3.7/site-packages/conda/core/subdir_data.py", line 145, in query
        self.load()
      File "/Users/xxx/anaconda3/lib/python3.7/site-packages/conda/core/subdir_data.py", line 210, in load
        _internal_state = self._load()
      File "/Users/xxx/anaconda3/lib/python3.7/site-packages/conda/core/subdir_data.py", line 394, in _load
        mod_etag_headers.get('_mod'))
      File "/Users/xxx/anaconda3/lib/python3.7/site-packages/conda/core/subdir_data.py", line 442, in _read_local_repdata
        _internal_state = self._process_raw_repodata_str(raw_repodata_str)
      File "/Users/xxx/anaconda3/lib/python3.7/site-packages/conda/core/subdir_data.py", line 480, in _process_raw_repodata_str
        json_obj = json.loads(raw_repodata_str or '{}')
      File "/Users/xxx/anaconda3/lib/python3.7/json/__init__.py", line 348, in loads
        return _default_decoder.decode(s)
      File "/Users/xxx/anaconda3/lib/python3.7/json/decoder.py", line 337, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/Users/xxx/anaconda3/lib/python3.7/json/decoder.py", line 355, in raw_decode
        raise JSONDecodeError("Expecting value", s, err.value) from None
    json.decoder.JSONDecodeError: Expecting value: line 291378 column 33 (char 8808000)

`$ /Users/xxx/anaconda3/bin/conda install -c anaconda redis`

  environment variables:
                 CIO_TEST=<not set>
               CONDA_ROOT=/Users/xxx/anaconda3
           CURL_CA_BUNDLE=<not set>
                     PATH=/usr/local/bin:/usr/local/sbin:/Users/xxx/anaconda3/bin:/Users/c
                          henfenyu/anaconda3/condabin:/Library/Java/JavaVirtualMachines/jdk1.8.0
                          _181.jdk/Contents/Home/bin:/usr/local/anaconda3/bin:/usr/local/bin:/us
                          r/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/usr/local/bin/ChromeDriv
                          er:/usr/local/mysql/bin:.:/Users/xxx/Work/apache-jmeter-5.4.1/bi
                          n:/usr/local/anaconda3/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbi
                          n:/usr/local/go/bin:/usr/local/bin/ChromeDriver:/usr/local/mysql/bin
       REQUESTS_CA_BUNDLE=<not set>
            SSL_CERT_FILE=<not set>
             SUDO_COMMAND=/Users/xxx/anaconda3/bin/conda install -c anaconda redis
                 SUDO_GID=20
                 SUDO_UID=501
                SUDO_USER=xxx

     active environment : None
       user config file : /Users/xxx/.condarc
 populated config files : /Users/xxx/.condarc
          conda version : 4.11.0
    conda-build version : 3.17.6
         python version : 3.7.1.final.0
       virtual packages : __osx=10.16=0
                          __unix=0=0
                          __archspec=1=x86_64
       base environment : /Users/xxx/anaconda3  (writable)
      conda av data dir : /Users/xxx/anaconda3/etc/conda
  conda av metadata url : None
           channel URLs : https://conda.anaconda.org/anaconda/osx-64
                          https://conda.anaconda.org/anaconda/noarch
                          http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/osx-64
                          http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/noarch
                          http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/osx-64
                          http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/noarch
          package cache : /Users/xxx/anaconda3/pkgs
                          /Users/xxx/.conda/pkgs
       envs directories : /Users/xxx/anaconda3/envs
                          /Users/xxx/.conda/envs
               platform : osx-64
             user-agent : conda/4.11.0 requests/2.21.0 CPython/3.7.1 Darwin/20.6.0 OSX/10.16
                UID:GID : 0:0
             netrc file : None
           offline mode : False


An unexpected error has occurred. Conda has prepared the above report.

Upload successful.

解决思路:

(base) faria-2:~ xxx$ cd ~
(base) faria-2:~ xxx$ rm .condarc

举例:pyautogui配合anaconda的安装

方式1-虚拟环境:sudo  conda install -c anaconda redis    (指定版本https://anaconda.org/search?q=redis 可查询)
方式2-虚拟环境离线包:conda install --use-local gdal-2.3.0-py36h04863e7_1.tar.bz2
方式2-本机环境:
pip install /路径/cacheout-0.14.1-py3-none-any.whl(https://pypi.org/project/cacheout/#files  whl文件)
python (解压后路径)setup.py install (https://pypi.org/project/cacheout/#files tar.gz文件)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,456评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,370评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,337评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,583评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,596评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,572评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,936评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,595评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,850评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,601评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,685评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,371评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,951评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,934评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,167评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,636评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,411评论 2 342

推荐阅读更多精彩内容