Mac python virtual environment

install python3

brew install python3

Update pip source configuration

  • Create a pip configuration file
cd ~
mkdir .pip; cd .pip; touch pip.conf;
  • Add the following configuration items in pip.conf, using Tsinghua pip source
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple/
[install]
trusted-host=pypi.tuna.tsinghua.edu.cn

Manage virtual environments with virtualenv

  • Install related software and configure environment variables
# virtualenv相对于把python环境拷贝一份,在虚拟环境下通过pip安装的工具只在该虚拟环境下有效
pip install virtualenv

# 安装管理工具 virtualenvwrapper
pip install virtualenvwrapper

# 设置环境变量 便于管理
export WORKON_HOME=~/Envs
sh /usr/local/bin/virtualenvwrapper.sh 
source ~/.bashrc
source ~/.bash_profile
  • basic usage
# virtualenvwrapper基本用法
# 创建虚拟环境 名称为venv 会放到WORKON_HOME中
mkvirtualenv venv

# 可以指定对应解释器的虚拟环境
mkvirtualenv --python=/usr/local/python3.5.3/bin/python venv

# 基本命令
workon     # 列出已有的虚拟环境 类似于 source venv/bin/activation
workon venv    # 进入venv虚拟环境中
deactivate    # 推出虚拟环境
rmvirtualenv venv    # 删除命名为venv的虚拟环境

# 建立虚拟环境目录
virtualenv venv
# 启动虚拟环境python
source venv/bin/activate
# 安装相关依赖
pip install -r requirements.txt

Reference: https://www.cnblogs.com/technologylife/p/6635631.html


install brew

Execute the following command to install the latest version of brew (https://github.com/Homebrew/install)
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

update brew

  • The simplest and rude way: uninstall the existing version brew, reinstall the latest version
    Uninstall the existing version, execute
    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"

  • To install the latest version, execute
    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

  • If you don't want to uninstall first and then install, you can do as follows

    • Execute the following command brew update
      If an error is reported: Error: /usr/local must be writable!
      you need to assign permissions to the /usr/local directory, execute sudo chown -R $(whoami) /usr/local
      If you continue to report an error: chown: /usr/local: Operation not permitted
      This is the enhanced permission restriction after Mac OS 10.13, especially for the /usr/local directory, SIP (System Intergrity Protection) is enabled by default ), which prohibits the software from running on Mac as root (refer to https://blog.csdn.net/shaobo8910/article/details/81121314).
      Solution: turn off SIP
  • Restart the Mac, hold down Command + Rthe key until the Apple Logo appears, and enter Recovery Mode

  • Click on Terminal in the tool

  • implementcsrutil disable

  • restart the mac

  • After the reboot is complete, executesudo chflags norestricted /usr/local && sudo chown -R $(whoami)/usr/local

Reference: https://blog.csdn.net/fxp850899969/article/details/53284193

Guess you like

Origin blog.csdn.net/iling5/article/details/96013253