python3 and python2 virtual environment configuration under windows

Python3 is accepted by more and more developers, and it is embarrassing that many legacy systems are still running in the Python2 environment, so sometimes you have to develop and debug in two versions at the same time.

How to coexist Python2 and Python3 in the system at the same time is a problem that developers have to face. The good news is that Anaconda can perfectly solve the coexistence problem of Python2 and Python3, and the installation of dependent packages (such as MySQL-python) often occurs on the Windows platform. ) failures are also resolved.

What is Anaconda?

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

  • First: Provide package management, which is similar to pip, and solves the problem that third-party packages often fail to be installed on the Windows platform.
  • Second: Provide virtual environment management, similar to virtualenv, and solve the problem of coexistence of multiple versions of Python.
Download Anaconda

Download the latest version of the https://www.continuum.io/downloads installation package directly from the official website   , select the Python 3.6 version installation package, and install it directly after the download is complete. You can choose the default configuration during the installation process, which requires about 1.8G of disk. space.

You can also install a smaller miniconda, download address https://conda.io/miniconda.html

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

If you are familiar with virtualenv, it is very easy to get started with conda. It doesn't matter if you are not familiar with virtualenv. It provides only a few commands, which are very simple. We can switch freely between Python2 and Python3 using conda's virtual environment management capabilities.

Create virtual environments conda for python3 and python2
--create name=venv_py36 python=3.6
conda --create name=venv_py27 python=2.7

View virtual environment

conda env list

The package management function of the package management tool
conda is a supplement to pip. If a Python environment is currently activated, third-party packages can be installed in the current environment.

# Install matplotlib
conda install matplotlib
# View installed packages
conda list
# Package update
conda update matplotlib
# Remove package
conda remove matplotlib


Configure the domestic source of anaconda to improve the download speed
. The mirror address of Anaconda is abroad by default. When using conda to install the package, it will be very slow. The currently available domestic mirror source address is provided by Tsinghua University. Modify ~/.condarc (Linux/Mac) or condarc (Windows) configuration

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

proxy_servers:
http: http://proxy.xxx.com:8080
https: https://proxy.xxx.com:8080


C:\Users\current username\pip\pip.ini (Windows) configuration (create a new one without a directory)

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


configure sublime

Sublime configuration Use python3
to open Sublime Text 3, and enter Tools-->Build System-->New Build System in turn;

{
"cmd": ["C:/Users/perry/Miniconda3/envs/venv_py36/python.exe","-u","$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"encoding": "cp936",
}

Sublime configuration Use python2
to open Sublime Text 3, and enter Tools-->Build System-->New Build System in turn;

{
"cmd": ["C:/Users/perry/Miniconda3/envs/venv_py27/python.exe","-u","$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"encoding": "cp936",
}

 Remark:

1. If you do not add "encoding": "cp936", this will not display Chinese;
2. If you are using a simplified Chinese system, the encoding must be cp936 instead of utf8
3. Click Save after the configuration is complete, you can customize the file name , but be sure to use the default path for the configuration to take effect;

By default it is saved in C:\Users\Current User\AppData\Roaming\Sublime Text 3\Packages\User

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325166235&siteId=291194637