Using Anaconda perfectly solves the coexistence problem of Python 2 and python 3

  • 2020-06-01 10:19:56
  • OfStack

preface

Now that Python3 is being accepted by more and more developers, it is embarrassing that many legacy systems still run in Python2 environments, so sometimes you have to develop and debug in both versions at the same time.

How to co-exist Python2 and Python3 in the system is a problem that developers have to face. The good news is that Anaconda can perfectly solve the co-existence problem of Python2 and Python3, and the frequent failure of installing dependent packages (such as MySQL-python) on the Windows platform can also be solved.

What is Anaconda?

Anaconda is a distribution of Python. If Python is Linux, then Anancoda is CentOS or Ubuntu. It addresses two major pain points for Python developers.

1. Provide package management, function similar to pip, Windows platform installation third party package often failed to solve the scenario. 2. Virtual environment management, similar to virtualenv, solves the problem of multiple versions of Python.

Download the Anaconda installation package

Official website to download address: https: / / www continuum. io/downloads

Local download address:

64:3.6 for Anaconda3 4.3.1 Python windows https: / / www ofstack. com softs / 556361. html

Anaconda3 4.3.1 Python 3.6 for windows 32-bit: https: / / www ofstack. com softs / 556363. html

Anaconda3 4.3.1 Python 3.6 for linux 32-bit: https: / / www ofstack. com softs / 556380. html

64:3.6 for Anaconda3 4.3.1 Python linux https: / / www ofstack. com softs / 556392. html

We choose the installation package of Python3.6 version, and install it directly after downloading. The installation process can be completed by selecting the default configuration, which requires about 1.8G disk space.

conda is a command-line tool for package management and environment management under Anaconda and is a combination of pip and vitualenv. After a successful installation, conda is added to the environment variable by default, so you can run the conda command directly from the command line window

If you're familiar with virtualenv, it's easy to get started with conda. If you're not familiar with virtulenv, it offers a few simple commands. We can freely switch between Python2 and Python3 with the virtual environment management function of conda.

Multiversion switching


#  Based on the  python3.6  create 1 called test_py3  The environment of 
conda create --name test_py3 python=3.6 

#  Based on the  python2.7  create 1 called test_py2  The environment of 
conda create --name test_py2 python=2.7

#  The activation  test  The environment 
activate test_py2 # windows
source activate test_py2 # linux/mac

#  Switch to the python3
activate test_py3

For more commands, see help conda -h

Package management tool

The package management feature of conda is a complement to pip, and if an Python environment is currently active, you can start installing the third package in the current environment.


#  The installation  matplotlib 
conda install matplotlib
#  View installed packages 
conda list 
#  Package update 
conda update matplotlib
#  Delete the package 
conda remove matplotlib

For those modules that cannot be successfully installed with pip, you can try to install with conda. If you cannot find the corresponding package with conda, you can still choose pip to install the package.

Improved download speed

The mirror address of Anaconda is in foreign countries by default, and it will be very slow to install the package with conda. Currently, the available domestic mirror source address is provided by tsinghua university. Modify the ~/.condarc (Linux/Mac) or C:\Users\ current username.condarc (Windows) configuration


channels:
 - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
 - defaults
show_channel_urls: true

In addition, you can also change the mirror source address of pip to the domestic one. The douban source is faster. Modify the ~ /. pip/pip. conf (Linux/Mac) or C: \ Users \ username \ pip \ pip current ini (Windows) configuration:


[global]
trusted-host = pypi.douban.com
index-url = http://pypi.douban.com/simple

conclusion


Related articles: