As a developer, Python is often inseparable from the environment (even if you are Java-er, Js-er, Php-er .etc.).
Why keep Python2
Most Mac comes with python 2 environment, but Python2 2020 No. 1 January has been the EOF. So now the mainstream is python 3, but we can not immediately uninstall python 2, because there are many software packages are based on python (this is very helpless, can only wait for Apple to completely remove Python2 dependent) 2. So, we will retain the python 2 in the case of re-install a python 3 environment.
Python3 release selected
Usually we talk about Python refers to the official version of CPython, this version provides the most basic and most common functions to achieve. But this version default lacks many advanced features, such as virtual environment, JIT, math and other relevant software packages, or require the user to set their own installation after installation. This is very time consuming, so I chose the Anaconda Python version.
More releases can compare reference on each Python distribution, we need to know everything .
Anaconda installation
Homebrew in cask has integrated Anaconda
$ brew search anaconda
==> Casks
anaconda ✔
$ brew cask info anaconda
anaconda: 2019.10 (auto_updates)
https://www.anaconda.com/
/usr/local/Caskroom/anaconda/2019.10 (424.2MB)
From: https://mirrors.ustc.edu.cn/homebrew-cask.git
==> Name
Continuum Analytics Anaconda
==> Artifacts
Anaconda3-2019.10-MacOSX-x86_64.sh (Installer)
==> Caveats
Cask anaconda installs files under /usr/local. The presence of such
files can cause warnings when running `brew doctor`, which is considered
to be a bug in Homebrew Cask.
So we just need to brew cask install anaconda
be able to complete the installation very simple.
Context switching
anaconda to the default installation /usr/local/anaconda3
, you can set the environment variable using the following command:
$ echo 'export PATH=/usr/local/anaconda3/bin:$PATH' >> ~/.bash_profile
$ source ~/.bash_profile
At this time, the terminal performs python
will see the default environment has been switched to the python Anaconda:
$ python
Python 3.7.4 (default, Aug 13 2019, 15:17:50)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
So Python2 environment go of it? Do not panic, just python command links to the Anaconda, the original Python2 environment unaffected by python2
will be able to evoke:
$ python2
WARNING: Python 2.7 is not recommended.
This version is included in macOS for compatibility with legacy software.
Future versions of macOS will not include Python 2.7.
Instead, it is recommended that you transition to using 'python3' from within Terminal.
Python 2.7.16 (default, Nov 9 2019, 05:55:08)
[GCC 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.32.4) (-macos10.15-objc-s on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
If you want to change back to the default Python environment Python2, simply execute the following command:
$ echo 'alias python="/usr/bin/python"' >> ~/.bash_profile
$ source ~/.bash_profile
Executed again pyhton
will change back Python2 the environment
$ python
WARNING: Python 2.7 is not recommended.
This version is included in macOS for compatibility with legacy software.
Future versions of macOS will not include Python 2.7.
Instead, it is recommended that you transition to using 'python3' from within Terminal.
Python 2.7.16 (default, Nov 9 2019, 05:55:08)
[GCC 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.32.4) (-macos10.15-objc-s on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Anaconda will need to use this time to start python3
commands:
$ python3
Python 3.7.4 (default, Aug 13 2019, 15:17:50)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Setting domestic mirror source
And other package manager, like, Anaconda comes conda and pip also need to set the domestic image. USTC recently stopped updating the image source Anaconda, as I wrote none of the recovery so far this blog, see Anaconda source use help . Fortunately, Tsinghua University image has been restored updates, see Anaconda related image has been restored .
conda source settings
First, create a .condarc
configuration file:
$ conda config --set show_channel_urls yes
Then refer to Anaconda mirroring help modify .condarc
files.
pip source settings
And conda, as here, the source of Tsinghua University, reference pypi mirroring Help
$ pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U
$ pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
Anaconda uses
The following instructions are in the case of default python environment of anaconda.
# ~ [15:49:51]
$ python --version
Python 3.7.4
# ~ [15:49:55]
$ python
Python 3.7.4 (default, Aug 13 2019, 15:17:50)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Virtual Environment
anaconda python supports management of multiple virtual environments.
Create a virtual environment
Create an environment of very simple, such as creating an environment python2.7:
conda create --name python27 python=2.7
anaconda When you are ready, we will request /usr/local/anaconda3/evns/python27
to install the latest version of python 2.7.x:
$ conda create --name python27 python=2.7
Collecting package metadata (current_repodata.json): done
Solving environment: done
## Package Plan ##
environment location: /usr/local/anaconda3/envs/python27
added / updated specs:
- python=2.7
The following packages will be downloaded:
package | build
---------------------------|-----------------
ca-certificates-2019.11.27 | 0 131 KB defaults
certifi-2019.11.28 | py27_0 156 KB defaults
libcxx-4.0.1 | hcfea43d_1 947 KB defaults
libcxxabi-4.0.1 | hcfea43d_1 350 KB defaults
libedit-3.1.20181209 | hb402a30_0 136 KB defaults
libffi-3.2.1 | h475c297_4 37 KB defaults
ncurses-6.1 | h0a44026_1 732 KB defaults
pip-19.3.1 | py27_0 1.9 MB defaults
python-2.7.17 | h97142e2_0 9.3 MB defaults
readline-7.0 | h1de35cc_5 316 KB defaults
setuptools-44.0.0 | py27_0 513 KB defaults
sqlite-3.30.1 | ha441bb4_0 2.4 MB defaults
tk-8.6.8 | ha441bb4_0 2.8 MB defaults
wheel-0.33.6 | py27_0 40 KB defaults
zlib-1.2.11 | h1de35cc_3 90 KB defaults
------------------------------------------------------------
Total: 19.7 MB
The following NEW packages will be INSTALLED:
ca-certificates anaconda/pkgs/main/osx-64::ca-certificates-2019.11.27-0
certifi anaconda/pkgs/main/osx-64::certifi-2019.11.28-py27_0
libcxx anaconda/pkgs/main/osx-64::libcxx-4.0.1-hcfea43d_1
libcxxabi anaconda/pkgs/main/osx-64::libcxxabi-4.0.1-hcfea43d_1
libedit anaconda/pkgs/main/osx-64::libedit-3.1.20181209-hb402a30_0
libffi anaconda/pkgs/main/osx-64::libffi-3.2.1-h475c297_4
ncurses anaconda/pkgs/main/osx-64::ncurses-6.1-h0a44026_1
pip anaconda/pkgs/main/osx-64::pip-19.3.1-py27_0
python anaconda/pkgs/main/osx-64::python-2.7.17-h97142e2_0
readline anaconda/pkgs/main/osx-64::readline-7.0-h1de35cc_5
setuptools anaconda/pkgs/main/osx-64::setuptools-44.0.0-py27_0
sqlite anaconda/pkgs/main/osx-64::sqlite-3.30.1-ha441bb4_0
tk anaconda/pkgs/main/osx-64::tk-8.6.8-ha441bb4_0
wheel anaconda/pkgs/main/osx-64::wheel-0.33.6-py27_0
zlib anaconda/pkgs/main/osx-64::zlib-1.2.11-h1de35cc_3
Proceed ([y]/n)?
After agreeing performs download and install:
Downloading and Extracting Packages
libffi-3.2.1 | 37 KB | ################################################################################################################################################################### | 100%
pip-19.3.1 | 1.9 MB | ################################################################################################################################################################### | 100%
readline-7.0 | 316 KB | ################################################################################################################################################################### | 100%
ca-certificates-2019 | 131 KB | ################################################################################################################################################################### | 100%
sqlite-3.30.1 | 2.4 MB | ################################################################################################################################################################### | 100%
tk-8.6.8 | 2.8 MB | ################################################################################################################################################################### | 100%
certifi-2019.11.28 | 156 KB | ################################################################################################################################################################### | 100%
ncurses-6.1 | 732 KB | ################################################################################################################################################################### | 100%
libedit-3.1.20181209 | 136 KB | ################################################################################################################################################################### | 100%
libcxx-4.0.1 | 947 KB | ################################################################################################################################################################### | 100%
libcxxabi-4.0.1 | 350 KB | ################################################################################################################################################################### | 100%
zlib-1.2.11 | 90 KB | ################################################################################################################################################################### | 100%
setuptools-44.0.0 | 513 KB | ################################################################################################################################################################### | 100%
python-2.7.17 | 9.3 MB | ################################################################################################################################################################### | 100%
wheel-0.33.6 | 40 KB | ################################################################################################################################################################### | 100%
Preparing transaction: done
Verifying transaction: / WARNING conda.core.path_actions:verify(963): Unable to create environments file. Path not writable.
environment location: /Users/zhang/.conda/environments.txt
done
Executing transaction: \ WARNING conda.core.envs_manager:register_env(52): Unable to register environment. Path not writable or missing.
environment location: /usr/local/anaconda3/envs/python27
registry file: /Users/zhang/.conda/environments.txt
done
#
# To activate this environment, use
#
# $ conda activate python27
#
# To deactivate an active environment, use
#
# $ conda deactivate
You can see a warning (influence conda activate / conda deactivate
switching environment, although you can use source activate / source deactivate
instead of)
Verifying transaction: / WARNING conda.core.path_actions:verify(963): Unable to create environments file. Path not writable.
environment location: /Users/zhang/.conda/environments.txt
done
Executing transaction: \ WARNING conda.core.envs_manager:register_env(52): Unable to register environment. Path not writable or missing.
environment location: /usr/local/anaconda3/envs/python27
registry file: /Users/zhang/.conda/environments.txt
done
This is because when you install the Homebrew anaconda automatically creates the root account ~/.conda
, and the current lack of user rights cause, the solution is simple, is to file permissions in this directory all into the current user can edit:
$ sudo chmod 777 ~/.conda/*
Then delete the offending environment and rebuild:
$ conda remove -n python27 --all
$ conda create --name python27 python=2.7
Switched virtual environment
After the installation is complete, in fact, anaconda has tips on how to switch the environment:
#
# To activate this environment, use
#
# $ conda activate python27
#
# To deactivate an active environment, use
#
# $ conda deactivate
Python27 prompted to switch to the environment, and with python --version
verification:
# ~ [16:45:54]
$ conda activate python27
(python27)
# ~ [16:48:06]
$ python --version
Python 2.7.17 :: Anaconda, Inc.
(python27)
Exit python27 environment, and with python --version
verification:
# ~ [16:53:10]
$ conda deactivate
# ~ [16:53:25]
$ python --version
Python 3.7.4
Queries virtual environment
conda info -e
You can list all environments.
Package Management
anaconda has two default package manager conda
and pip
, conda
are uniform across the environment, but pip
are each isolated from the environment.
The default Python3 environment:
# ~ [17:12:31]
$ conda info
active environment : None
shell level : 0
user config file : /Users/zhang/.condarc
populated config files : /Users/zhang/.condarc
conda version : 4.7.12
conda-build version : 3.18.9
python version : 3.7.4.final.0
virtual packages :
base environment : /usr/local/anaconda3 (writable)
channel URLs : https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/osx-64
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/noarch
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/osx-64
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/noarch
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r/osx-64
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r/noarch
package cache : /usr/local/anaconda3/pkgs
/Users/zhang/.conda/pkgs
envs directories : /usr/local/anaconda3/envs
/Users/zhang/.conda/envs
platform : osx-64
user-agent : conda/4.7.12 requests/2.22.0 CPython/3.7.4 Darwin/19.2.0 OSX/10.15.2
UID:GID : 501:20
netrc file : None
offline mode : False
# ~ [17:12:36]
$ pip --version
pip 19.3.1 from /usr/local/anaconda3/lib/python3.7/site-packages/pip (python 3.7)
Cut to the Python27 environment:
# ~ [17:06:10]
$ conda info
active environment : python27
active env location : /usr/local/anaconda3/envs/python27
shell level : 1
user config file : /Users/zhang/.condarc
populated config files : /Users/zhang/.condarc
conda version : 4.7.12
conda-build version : 3.18.9
python version : 3.7.4.final.0
virtual packages :
base environment : /usr/local/anaconda3 (writable)
channel URLs : https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/osx-64
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/noarch
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/osx-64
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/noarch
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r/osx-64
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r/noarch
package cache : /usr/local/anaconda3/pkgs
/Users/zhang/.conda/pkgs
envs directories : /usr/local/anaconda3/envs
/Users/zhang/.conda/envs
platform : osx-64
user-agent : conda/4.7.12 requests/2.22.0 CPython/3.7.4 Darwin/19.2.0 OSX/10.15.2
UID:GID : 501:20
netrc file : None
offline mode : False
(python27)
# ~ [17:06:22]
$ pip --version
pip 19.3.1 from /usr/local/anaconda3/envs/python27/lib/python2.7/site-packages/pip (python 2.7)
(python27)
It can be clearly seen in the difference between the two terms of environmental isolation. Thus, every time create a virtual environment, we need to set about pip
domestic image
$ pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U
$ pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
Package Installation
Because pip
a separate set of each environment, and therefore there is no difference between using a single environment, the package is installed into each virtual environment /site-packages/
directory, for example /usr/local/anaconda3/envs/python27/lib/python2.7/site-packages/
.
conda
Because each environment share a set, you need to specify the environment in the implementation, it is not specified, the default is the current active environment:
# 查看当前环境下已安装的包
conda list
# 查看某个指定环境的已安装包
conda list -n python27
# 查找package信息
conda search numpy
# 安装package
conda install -n python27 numpy
# 如果不用-n指定环境名称,则被安装在当前活跃环境
# 也可以通过-c指定通过某个channel安装
# 更新package
conda update -n python27 numpy
# 删除package
conda remove -n python27 numpy
conda
You can also manage conda
, python
, anaconda
version:
# 更新conda,保持conda最新
conda update conda
# 更新anaconda
conda update anaconda
# 更新python
conda update python
# 假设当前环境是python 3.7, conda会将python升级为3.7.x系列的当前最新版本
IDE settings
Always inseparable from the development IDE, just to introduce PyCharm settings, VS Code can refer to the official documentation the Using Python Environments in VS Code .
PyCharm
First you need to create your virtual environment, or to python27
the environment as an example.
Select the environment when creating Project
- Open PyCharm, select Create New Project
- Modify the project name, and then select Project Interpreter == "Existing Interpreter ==> ...
- Select Conda Environment == "..., set python27 environment previously created folder
You've created your project, and then create a python file output Hello world:
print("Hello world")
Project has been modified environment