簡體   English   中英

如何在 ubuntu 上將我的 python 版本從 3.7.5 降級到 3.6.5

[英]How do I downgrade my version of python from 3.7.5 to 3.6.5 on ubuntu

所以目前,我有 ubuntu 19。它默認帶有 python 3.7.5。 我需要降級到 3.6.5。

編輯:

我正在使用虛擬環境

下面討論從 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

從預編譯的 Ubuntu deb 包 repos 安裝它。

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

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

出現提示時,按 ENTER 繼續。

sudo apt install python3.6

或者,您可以輕松創建虛擬環境。

假設您在系統上安裝了 python 3.8(或更高版本) ,但對於特定任務,您需要python 3.7(或更低版本) 最好的想法是使用 python 3.7(或任何 3.x,根據您想要的版本更改以下命令。下面是使用 python 3.7 的虛擬環境的實現)創建一個虛擬環境

步驟:(2022 年 8 月檢查)

  1. 安裝 python 3.7 及其虛擬環境包。

    sudo apt-get install python3.7-dev python3.7-venv

  2. 通過以下命令找出您的 python 3.7 的位置:

    which python3.7 (應該類似於/usr/bin/python3.7 ,如果找不到,則手動安裝 python 3.7)

  3. 在主目錄中創建虛擬環境。

    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

  4. python --version (現在應該是 python 3.7)

  5. 完畢。 Python 3.7 可以在這個虛擬環境中使用。 which python ,你會看到你已經在虛擬環境中創建了 python 3.7,而不是在全局系統中。

    需要停用時運行deactivate

您還可以使用virtualenv來使用不同的 Python 環境。

如果您鍵入 python 和 tab 兩次左右,您可能會看到許多可用的 Python 版本。 我這樣說是因為當我打字時

python3.6 -V

我有

Python 3.6.9

可用的。 我的 python 別名是 3.7.5

當您鍵入 python 時,您可能指的是 .bashrc 中定義的別名

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM