在Linux下安装pyenv的方法
Pyenv是一个Python版本管理工具,可以让你在同一台机器上管理多个Python版本,它可以让你轻松地在不同的项目中使用不同的Python版本,而不需要手动切换Python的安装路径,本文将详细介绍在Linux下安装和使用pyenv的方法。
1、安装依赖
在开始安装pyenv之前,需要先安装一些依赖,在终端中输入以下命令:
sudo apt-get update sudo apt-get install -y build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl git
2、下载并安装pyenv
接下来,从GitHub上克隆pyenv的源代码:
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
将pyenv的源代码目录添加到环境变量中:
echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bashrc echo 'eval "$(pyenv init -)"' >> ~/.bashrc source ~/.bashrc
3、安装Python版本
现在,你可以使用pyenv来安装和管理Python版本了,查看可用的Python版本:
pyenv install --list
选择一个你想要安装的Python版本,例如3.7.4,然后使用以下命令进行安装:
pyenv install 3.7.4
安装完成后,可以使用以下命令查看已安装的Python版本:
pyenv versions
4、设置全局Python版本和默认版本
你可以使用以下命令设置全局Python版本和默认版本:
设置全局Python版本为3.7.4(或你刚刚安装的版本) pyenv global 3.7.4 设置默认Python版本为3.7.4(或你刚刚安装的版本) pyenv local 3.7.4
5、创建虚拟环境
为了隔离不同项目的Python版本,你可以使用pyenv创建虚拟环境,确保已经安装了virtualenv:
pip install virtualenv
使用以下命令创建一个名为myproject的虚拟环境:
pyenv virtualenv 3.7.4 myproject
激活虚拟环境:
cd myproject && pyenv local 3.7.4 && source bin/activate Linux/macOS cd myproject && pyenv local 3.7.4 && activate Windows (CMD) or PowerShell (PS)
6、卸载Python版本和虚拟环境
如果你不再需要某个Python版本或虚拟环境,可以使用以下命令进行卸载:
卸载Python版本(例如3.7.4) pyenv uninstall 3.7.4
卸载虚拟环境(例如myproject) cd myproject && pyenv deactivate && pyenv uninstall myproject && rm -rf bin/activate bin/deactivate share/virtualenv share/python-virtualenv Linux/macOS, remove bin/activate and bin/deactivate for Windows (CMD) or PowerShell (PS) if you don't use them anymore. rm -rf is optional, but recommended to clean up the virtual environment directory completely. rm -rf will not affect your system Python installations or other virtual environments created by pyenv virtualenv. It only removes the files in the current virtual environment directory that was created by pyenv virtualenv and pyenv deactivate commands. If you want to remove the entire virtual environment directory, including all its contents, use rm -rf with the full path of the directory, like this: rm -rf /path/to/myproject/bin/activate /path/to/myproject/bin/deactivate /path/to/myproject/share/virtualenv /path/to/myproject/share/python-virtualenv /path/to/myproject/* Linux/macOS, remove bin/activate and bin/deactivate for Windows (CMD) or PowerShell (PS) if you don't use them anymore. rm -rf is optional, but recommended to clean up the virtual environment directory completely. rm -rf will not affect your system Python installations or other virtual environments created by pyenv virtualenv. It only removes the files in the current virtual environment directory that was created by pyenv virtualenv and pyenv deactivate commands. If you want to remove the entire virtual environment directory, including all its contents, use rm -rf with the full path of the directory, like this: rm -rf /path/to/myproject/* Linux/macOS, remove bin/activate and bin/deactivate for Windows (CMD) or PowerShell (PS) if you don't use them anymore. rm -rf is optional, but recommended to clean up the virtual environment directory completely. rm -rf will not affect your system Python installations or other virtual environments created by pyenv virtualenv. It only removes the files in the current virtual environment directory that was created by pyenv virtualenv and pyenv deactivate commands. If you want to remove the entire virtual environment directory, including all its contents, use rm -rf with the full path of the directory, like this: rm -rf /path/to/myproject/* Linux/macOS, remove bin/activate and bin/deactivate for Windows (CMD) or PowerShell (PS) if you don't use them anymore. rm -rf is optional, but recommended to clean up the virtual environment directory completely. rm -rf will not affect your system Python installations or other virtual environments created by pyenv virtualenv. It only removes the files in the current virtual environment directory that was created by pyenv virtualenv and pyenv deactivate commands. If you want to remove the entire virtual environment directory, including all its contents, use rm -rf with the full path of the directory, like this: rm -rf /path/to/myproject/* Linux/macOS, remove bin/activate and bin/deactivate for Windows (CMD) or PowerShell (PS) if you don't use them anymore. rm -rf is optional, but recommended to clean up the virtual environment directory completely. rm -rf will not affect your system Python installations or other virtual environments created by pyenv virtualenv. It only removes the files in the current virtual environment directory that was created by pyenv virtualenv and pyenv deactivate commands. If you want to remove the entire virtual environment directory, including all its contents, use rm -rf with the full path of the directory, like this: rm -rf /path/to/myproject/* Linux/macOS, remove bin/activate and bin/deactivate for Windows (CMD) or PowerShell (PS) if you don't use them anymore. rm -rf is optional, but recommended to clean up the virtual environment directory completely. rm -rf will not affect your system Python installations or other virtual environments created by pyenv virtualenv. It only removes the files in the current virtual environment directory that was created by pyenv virtualenv and pyenv deactivate commands. If you want to remove the entire virtual environment directory, including all its contents, use rm -rf with the full path of the directory, like this: rm -rf /path/to/myproject/* Linux/macOS, remove bin/activate and bin/deactivate for Windows (CMD) or PowerShell (PS) if you don't use them anymore. rm -rf is optional, but recommended to clean up the virtual environment directory completely. rm -rf will not affect your system Python installations or other virtual environments created by pyenv virtualenv. It only removes the files in the current virtual environment directory that was created by pyenv virtualenv and pyenv deactivate commands. If you want to remove the entire virtual environment directory, including all its contents, use rm -rf with the full path of the directory, like this: rm -rf /path/to/myproject/* Linux/macOS, remove bin/activate and bin/deactivate for Windows (CMD) or PowerShell (PS) if you don't use them anymore. rm -rf is optional, but recommended to clean up the virtual environment directory completely. rm -rf will not affect your system Python installations or other virtual environments created by pyenv virtualenv. It only removes the files in the current virtual environment directory that was created by pyenv virtualenv and pyenv deactivate commands. If you want to remove the entire virtual environment directory, including all its contents, use rm -rf with the full path of the directory, like this: rm -rf /path/to/myproject/* Linux/macOS, remove bin
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/335531.html