Mac上pyenv的安装与使用
目的:简单一句话,实现 python
环境隔离
# 一、pyenv
# 1、安装
# 方式一:Git Clone
git clone git://github.com/pyenv/pyenv.git ~/.pyenv
bash
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/shims:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
exec $SHELL -l
zsh
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/shims:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
exec $SHELL -l
# 方式二:Homebrew
安装
brew install pyenv
根据自身环境,将下方内容加到对应文件中: .bashrc
/ .zshrc
export PYENV_ROOT=/usr/local/var/pyenv
export PATH="$PYENV_ROOT/bin:$PATH"
export PATH="$PYENV_ROOT/shims:$PATH"
if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
# 2、验证
验证安装是否成功:
pyenv
# 3、使用
基本使用
命令 | 描述 |
---|---|
pyenv --version | 查看 pyenv 的版本 |
pyenv versions | 罗列当前已安装的所有 python 环境,如果是当前正在使用的环境,则前面会有个 * |
pyenv help | 查看帮助 |
pyenv init | 如果输入 pyenv 之后使用 tab 不补全,可以使用该命令进行初始即可使用补全命令 |
安装环境
命令 | 描述 |
---|---|
pyenv install -l | 显示可以安装的版本列表 |
pyenv install 版本号 | 安装指定版本的 python |
pyenv rehash | 更新本地数据库,安装指定版本的 python 后使用 |
环境应用
命令 | 描述 |
---|---|
pyenv global 版本号 | 更改本机版本,重启不会造成再次更改 |
pyenv local 版本号 | 会在当前目录创建 .python-version 文件,并记录设置的 python 环境,每次进入该目录会自动设置成该 python 环境 |
pyenv shell 版本号 | 更改当前 shell 下使用的 python 版本,临时生效,优先级高于 global |
# 二、virtualenv插件
# 1、安装
# 方式一:Git Clone
cd .pyenv/plugins
git clone https://github.com/pyenv/pyenv-virtualenv.git # 安装virtualenv插件
# 方式二:Homebrew
brew install pyenv-virtualenv
# 2、配置
无论使用上述的哪种方式进行的安装,请根据自身环境,将下方内容加到对应文件中: .bashrc
/ .zshrc
eval "$(pyenv virtualenv-init -)"
# 3、使用
# 基本使用
命令 | 描述 |
---|---|
pyenv virtualenv 3.8.3 env383 | 创建 3.8.3 版本虚拟环境 |
pyenv virtualenvs | 显示环境 |
pyenv activate env383 | 激活使用指定的虚拟环境 |
pyenv deactivate | 退出当前虚拟环境 |
rm -rf .pyenv/versions/3.8.3 | 删除版本环境 |
rm -rf .pyenv/versions/env383 | 删除虚拟环境 |
# 自动激活和退出虚拟环境
在需要使用虚拟环境的目录(通常是项目目录)中:
- 建立一个
.python-version
的文本文件 - 将虚拟环境名称(如
env383
)写在里面
之后每次进/出该目录时,虚拟环境都将自动激活/退出。
# 三、报错
# 1、激活指定的虚拟环境时报错
# 命令
pyenv activate env383
# 报错信息
Failed to activate virtualenv.
Perhaps pyenv-virtualenv has not been loaded into your shell properly.
Please restart current shell and try again.
别慌,重启你的终端,或者执行如下命令即可
exec $SHELL -l
# 2、还是系统版本
如果你用 pyenv versions
查看,明明已经切换成功,但是用 python -V
却还是系统版本。
原因是 pyenv
没有加到 $PATH
环境变量里去,解决办法如下:
export PYENV_ROOT=~/.pyenv
export PATH=$PYENV_ROOT/shims:$PATH
# 3、安装 python
报错
安装 python
报错,如
pyenv install 3.8.3
报错内容:
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.8.3.tar.xz...
-> https://www.python.org/ftp/python/3.8.3/Python-3.8.3.tar.xz
Installing Python-3.8.3...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
BUILD FAILED (OS X 12.0.1 using python-build 20180424)
Inspect or clean up the working tree at /var/folders/cy/8zkx5hys2tsgb1d_x42_r8qc0000gn/T/python-build.20220122174148.1531
Results logged to /var/folders/cy/8zkx5hys2tsgb1d_x42_r8qc0000gn/T/python-build.20220122174148.1531.log
Last 10 log lines:
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration -I./Include/internal -I. -I./Include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/usr/local/var/pyenv/versions/3.8.3/include -I/usr/local/opt/sqlite/include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/usr/local/var/pyenv/versions/3.8.3/include -I/usr/local/opt/sqlite/include -DPy_BUILD_CORE_BUILTIN -c ./Modules/_codecsmodule.c -o Modules/_codecsmodule.o
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration -I./Include/internal -I. -I./Include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/usr/local/var/pyenv/versions/3.8.3/include -I/usr/local/opt/sqlite/include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/usr/local/var/pyenv/versions/3.8.3/include -I/usr/local/opt/sqlite/include -DPy_BUILD_CORE_BUILTIN -c ./Modules/_weakref.c -o Modules/_weakref.o
./Modules/posixmodule.c:9221:15: error: implicit declaration of function 'sendfile' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
ret = sendfile(in, out, offset, &sbytes, &sf, flags);
^
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration -I./Include/internal -I. -I./Include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/usr/local/var/pyenv/versions/3.8.3/include -I/usr/local/opt/sqlite/include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/usr/local/var/pyenv/versions/3.8.3/include -I/usr/local/opt/sqlite/include -DPy_BUILD_CORE_BUILTIN -DPy_BUILD_CORE_BUILTIN -I./Include/internal -c ./Modules/_functoolsmodule.c -o Modules/_functoolsmodule.o
1 error generated.
make: *** [Modules/posixmodule.o] Error 1
make: *** Waiting for unfinished jobs....
1 warning generated.
使用下面的命令安装即可,注意修改成相应的版本号
CFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk-path)/usr/include" LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib" pyenv install --patch 3.8.3 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch?full_index=1)
# 四、注意
- 安装好的版本不做任何修改,我们只操作虚拟环境,如
env383
- 可以通过版本来多个虚拟环境,如
env383_1
、env383_2
- 导出当前环境的依赖库:
pip freeze > requirements.txt
- 导入依赖库到当前环境:
pip install -r requirements.txt
- 01
- Flutter - 危!3.24版本苹果审核被拒!11-13
- 02
- Flutter - 轻松搞定炫酷视差(Parallax)效果09-21
- 03
- Flutter - 轻松实现PageView卡片偏移效果09-08