如何在Ubuntu上将Python版本从3.7.5降级到3.6.5

8

目前,我使用的是Ubuntu 19操作系统,默认安装了Python 3.7.5版本。但是我需要将其降级为3.6.5版本。

修改后:

我正在使用虚拟环境。


这个可行吗?https://askubuntu.com/a/138327 - xiaoyu2006
@AMC 是的。它有什么不同之处? - Bear Bile Farming is Torture
@unathletic_coder 这有什么不同吗?你在使用什么环境? - AMC
@AMC Python 3.7.5 - Bear Bile Farming is Torture
更改系统Python可能会带来不良后果。请尝试使用Pyenv代替。 - taari
显示剩余11条评论
4个回答

26

以下内容讲解从3.6.7升级到3.7.0的过程,但您可以使用相同的步骤进行降级。除非您确切知道自己在做什么,否则不应更改系统python。

首先安装Pyenv

安装说明在此处

查看Pyenv选项

$ pyenv 
pyenv 1.2.14
Usage: pyenv <command> [<args>]

Some useful pyenv commands are:
   commands    List all available pyenv commands
   activate    Activate virtual environment
   commands    List all available pyenv commands
   deactivate   Deactivate virtual environment
   doctor      Verify pyenv installation and deevlopment tools to build pythons.
   exec        Run an executable with the selected Python version
   global      Set or show the global Python version
   help        Display help for a command
   hooks       List hook scripts for a given pyenv command
   init        Configure the shell environment for pyenv
   install     Install a Python version using python-build
   local       Set or show the local application-specific Python version
   prefix      Display prefix for a Python version
   rehash      Rehash pyenv shims (run this after installing executables)
   root        Display the root directory where versions and shims are kept
   shell       Set or show the shell-specific Python version
   shims       List existing pyenv shims
   uninstall   Uninstall a specific Python version
   --version   Display the version of pyenv
   version     Show the current Python version and its origin
   version-file   Detect the file that sets the current pyenv version
   version-name   Show the current Python version
   version-origin   Explain how the current Python version is set
   versions    List all Python versions available to pyenv
   virtualenv   Create a Python virtualenv using the pyenv-virtualenv plugin
   virtualenv-delete   Uninstall a specific Python virtualenv
   virtualenv-init   Configure the shell environment for pyenv-virtualenv
   virtualenv-prefix   Display real_prefix for a Python virtualenv version
   virtualenvs   List all Python virtualenvs found in `$PYENV_ROOT/versions/*'.
   whence      List all Python versions that contain the given executable
   which       Display the full path to an executable

See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme

看 Python 版本

$ pyenv versions
  system
 * 3.6.7 (set by /home/taarimalta/.pyenv/version)

安装新的Python

$ pyenv install 3.7.0
Installing Python-3.7.0...
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
WARNING: The Python readline extension was not compiled. Missing the GNU readline lib?
WARNING: The Python sqlite3 extension was not compiled. Missing the SQLite3 lib?
Installed Python-3.7.0 to /home/taarimalta/.pyenv/versions/3.7.0

如果您在_ctypes安装时遇到问题,需安装libffi-dev库

现在查看版本


$ pyenv versions
  system
* 3.6.7 (set by /home/taarimalta/.pyenv/version)
  3.7.0

选择3.7.0进行本地环境设置

$ pyenv local 3.7.0

查看版本更改

$ pyenv versions
  system
  3.6.7
 * 3.7.0 (set by /home/taarimalta/.python-version)
$ python
Python 3.7.0 (default, Jan  1 2020, 10:52:57) 
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

切换到不同的文件夹

cd ../project2
pyenv versions
  system
* 3.6.7 (set by /home/taarimalta/.pyenv/version)
  3.7.0

根据您本地设置的Python版本,此处的Python版本可能会有所不同。

全局设置pyenv版本

这将为用户全局设置Python版本。

pyenv global 3.7.0
请注意,pyenv通过添加一个.python-version文件来设置本地版本。
$ pyenv local 3.7.0
$ cat .python-version
3.7.0
注意,pyenv 通过查看 ~/.pyenv/version 文件来确定全局版本。
cat ~/.pyenv/version
3.8.2


1
这非常详细和清晰,但它没有让我到达正确的位置。我在Ubuntu 20.4上,它有Python 3.8.2;我需要3.6来完成我的工作。我能够使用pyenv安装它,并使用pyenv local进行切换,但是当我运行python3时,我仍然得到3.8.2版本。另外,我没有看到/usr/bin/python3.6。我的“pyenv versions”输出看起来正确。有什么想法可能是缺失了什么? - Apollo Grace
如果你切换了文件夹,Python 版本可能会切换到默认版本。因此,请检查你是否在同一个文件夹中。Python 安装在 /home/<username>/.pyenv 中的某个位置。你可以使用 which python3 命令来查看。 - taari
原来我没有正确设置我的.bashrc文件来运行"pyenv init -"和"pyenv virtualenv-init -"。一旦我设置好了,就一切正常了。 export PATH="/home/vagrant/.pyenv/bin:$PATH" eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)" - Apollo Grace

1

或者你可以轻松地创建一个虚拟环境。

假设你的系统安装了 Python3.8(或更高版本),但是对于特定任务,你需要Python 3.7(或更低版本)。最好的方法是使用 Python 3.7(或任何3.x,根据您想要的版本更改下面的命令。以下是使用 Python 3.7 实现虚拟环境的示例)创建虚拟环境。

步骤:(2022年8月检查)

安装Python 3.7及其虚拟环境包:
sudo apt-get install python3.7-dev python3.7-venv
通过以下命令找到Python 3.7的位置:
which python3.7 (应该是类似于 /usr/bin/python3.7,如果没有找到,则手动安装Python 3.7)
在主目录中创建虚拟环境:
cd mkdir virtual_env /usr/bin/python3.7 -m venv ~/virtual_env/venv_with_python3.7 source ~/virtual_env/venv_with_python3.7/bin/activate python --version(现在应该是Python 3.7)
完成。在这个虚拟环境中可以使用Python 3.7。输入 which python,您会发现已经在虚拟环境中创建了Python 3.7,而不是在系统全局中创建。
需要退出虚拟环境时,请运行 deactivate。

0

从预编译的Ubuntu deb软件包仓库中安装它。

$ sudo apt update
$ sudo apt install software-properties-common

$ sudo add-apt-repository ppa:deadsnakes/ppa

当提示时,请按ENTER键继续。

sudo apt install python3.6

-3

你还可以使用virtualenv来使用不同的Python环境。

如果你输入python并按两次Tab键,你可能会看到许多可用的Python版本。我之所以这样说,是因为当我键入时

python3.6 -V

我有

Python 3.6.9

可用。我的Python别名是3.7.5

当你输入Python时,你可能指的是在.bashrc中定义的别名


它根本没有回答问题。 - Pierre
这个回答与问题无关。 - duddu venkatesh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接