python环境配置 一个系统多个python版本的管理,日常使用如何做到不污染系统python。
1 2 3 $ git clone https://github.com/pyenv/pyenv.git ~/.pyenv $ git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
1 2 3 4 5 6 7 export LDFLAGS="-L/usr/local/opt/bzip2/lib -L/usr/local/opt/zlib/lib -L/usr/local/opt/sqlite/lib" export CPPFLAGS="-I/usr/local/opt/bzip2/include -I/usr/local/opt/zlib/include -I/usr/local/opt/sqlite/include" export PYTHON_CONFIGURE_OPTS="--enable-framework" //需要加入tkinter: --with-tcl-tklinux用:env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install pyenv install 3.8.6 ⚠️注意一个问题:/private/tmp的权限问题,可以sudo chown一下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 https://stackoverflow.com/questions/60469202/unable-to-install-tkinter-with-pyenv-pythons-on-macos 要让python使用更新的tk库(macOS自带了8.5),可以按照如下步骤 1. 安装tck-tk ※ brew install tcl-tk ※ brew info tcl-tk tcl-tk: stable 8.6.10 (bottled) [keg-only] ... ==> Caveats tcl-tk is keg-only, which means it was not symlinked into /usr/local , because tk installs some X11 headers and macOS provides an (older) Tcl/Tk. If you need to have tcl-tk first in your PATH run: echo 'export PATH="/usr/local/opt/tcl-tk/bin:$PATH"' >> ~/.zshrc For compilers to find tcl-tk you may need to set : export LDFLAGS="-L/usr/local/opt/tcl-tk/lib" export CPPFLAGS="-I/usr/local/opt/tcl-tk/include" For pkg-config to find tcl-tk you may need to set : export PKG_CONFIG_PATH="/usr/local/opt/tcl-tk/lib/pkgconfig" ... 2. 重装python env \ PATH="$(brew --prefix tcl-tk) /bin:$PATH " \ LDFLAGS="-L$(brew --prefix tcl-tk) /lib" \ CPPFLAGS="-I$(brew --prefix tcl-tk) /include" \ PKG_CONFIG_PATH="$(brew --prefix tcl-tk) /lib/pkgconfig" \ CFLAGS="-I$(brew --prefix tcl-tk) /include" \ PYTHON_CONFIGURE_OPTS="--enable-framework --with-tcltk-includes='-I$(brew --prefix tcl-tk) /include' --with-tcltk-libs='-L$(brew --prefix tcl-tk) /lib -ltcl8.6 -ltk8.6'" \ pyenv install 3.8.6
更多配置教程:https://opensource.com/article/19/6/python-virtual-environments-mac https://opensource.com/article/20/4/pyenv https://opensource.com/article/19/5/python-3-default-mac
python virtual 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 $ pyenv global 3.7.3 $ $(pyenv which python) -m pip install virtualenvwrapper virtualenv $ mkvirtualenv test1 $ mkvirtualenv test2 $ mkdir -p ~/src/pyfun && cd ~/src/pyfun $ mkvirtualenv $(basename $(pwd )) (pyfun)$ workon pyfun test1 test2 (pyfun)$ deactivate $ $ cd ~/src/pyfun (pyfun)$ workon . $ cd ~/src/pyfun $ rmvirtualenv $(basename $(pwd )) $ mkvirtualenv $(basename $(pwd ))
创建虚拟环境 1 pyenv virtualenv 2.7.15 env-name
若不指定 python 版本,会默认使用当前环境 python 版本。如果指定 Python 版本,则一定要是已经安装过的版本,否则会出错。环境的真实目录位于 ~/.pyenv/versions
下
列出当前虚拟环境 1 2 3 pyenv virtualenvs pyenv activate env-name # 激活虚拟环境 pyenv deactivate #退出虚拟环境,回到系统环境
删除虚拟环境 1 2 pyenv uninstall env-name rm -rf ~/.pyenv/versions/env-name # 或者删除其真实目录
使用 pyenv 来管理 python,使用 pyenv-virtualenv 插件来管理多版本 python 包。此时,还需注意,当我们将项目运行的 env 环境部署到生产环境时,由于我们的 python 包是依赖 python 的,需要注意生产环境的 python 版本问题。
所有命令 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 $ pyenv commands activate commands completions deactivate doctor exec global help hooks init install installer local offline-installer prefix rehash root shell shims uninstall update version --version version-file version-file-read version-file-write version-name version-origin versions virtualenv virtualenv-delete virtualenv-init virtualenv-prefix virtualenvs whence which
http://einverne.github.io/post/2017/04/pyenv.html
MacVim安装于配置 Brew cask编译好的macvim依赖于brew的python@3.9 ,所以需要修改编译参数:
1 2 $ brew tap macvim-dev/macvim $ brew edit macvim-dev/macvim/macvim
需要把配置文件里依赖python3的路径修改为你pyenv的路径:
1 2 3 4 5 ENV.append 'vi_cv_path_python3' , "/users/<user name>/. Pyenv/Versions/3.5.1/Bin/Python3" ENV.append 'vi_cv_dll_name_python3' , "/users/<user name>/. pyenv/versions/3.5.1/lib/libpython3.5m.dylib"
然后安装:
1 brew install --HEAD --with-properly-linked-python2-python3 macvim-dev/macvim/macvim
⚠️最近big sur编译的时候有bug,导致make失败 https://github.com/macvim-dev/homebrew-macvim/issues/16
可以brew edit的时候修改system ‘make’为system ‘sleep 120’,趁它休息的时候手动去目录make(手动可以成功),然后在brew跳过了make就成功了。
1 2 brew install --keep-tmp --HEAD --with-properly-linked-python2-python3 macvim-dev/macvim/macvim -v --keep-tmp可以保留临时文件到/tmp下