如何在Linux上安裝Python並添加到環境變數中

一、下載Python安裝包

首先,在Linux上下載Python安裝包。一般情況下,Python的安裝包可以從官方網站獲取。打開終端,輸入以下命令:

sudo apt-get update
sudo apt-get install python3

這將會在系統上安裝最新的Python3版本。

二、添加Python到環境變數

為了使用Python,需要將其添加到系統的環境變數中。在Linux操作系統中,這意味著編輯Bash shell配置文件。打開終端,輸入以下命令:

sudo nano ~/.bashrc

打開的文件應該類似於如下的內容:

# ~/.bashrc: executed by bash(1) for non-login shells.

# Note: PS1 and umask are already set in /etc/profile. You should not
# need to override these settings here.

# You may want to put all your additions into a separate file like
# ~/.bashrc.local, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-colored, unless we know we "want" colors)
case "$TERM" in
    xterm-color|*-256color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
    # We have color support; assume it's compliant with Ecma-48
    # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
    # a case would tend to support setf rather than setaf.)
    color_prompt=yes
    else
    color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# colored GCC warnings and errors
#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'

# some more ls aliases
#alias ll='ls -l'
#alias la='ls -A'
#alias l='ls -CF'

找到文件末尾,並添加以下代碼,代表將Python3路徑添加到環境變數中:

export PATH=/usr/bin/python3:\$PATH

現在,重新載入Bash shell配置文件:

source ~/.bashrc

這樣,Python3將被添加到系統的環境變數中,你可以從終端中的任何位置直接運行Python3。

三、安裝Python包管理程序pip

如果你希望更好地管理和安裝Python中的包,建議安裝Python包管理程序pip。

打開終端,輸入以下命令:

sudo apt-get update
sudo apt-get install python3-pip

安裝完成後,你可以使用以下命令下載和安裝Python包:

pip3 install package_name

其中,”package_name”是你希望安裝的Python包的名稱。

四、安裝Python虛擬環境

當你需要在不同的項目之間使用不同版本的Python和不同的包時,建議使用Python虛擬環境。這樣,每個項目可以有自己的Python環境和依賴項。

打開終端,輸入以下命令:

sudo apt-get update
sudo apt-get install python3-venv

現在,進入你的項目目錄,並創建一個Python虛擬環境:

cd my_project
python3 -m venv my_project_env

此命令將在當前目錄中創建一個名為”my_project_env”的虛擬環境。

要激活虛擬環境,請輸入以下命令:

source my_project_env/bin/activate

現在,您可以在虛擬環境中安裝項目特定的依賴項。安裝完後,您可以使用以下命令退出虛擬環境:

deactivate

總結

以上是安裝Python和將其添加到Linux環境變數中的細節和過程。使用虛擬環境和包管理程序,可以更加輕鬆地管理項目和依賴項。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/219735.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-09 11:00
下一篇 2024-12-09 11:00

相關推薦

  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • 如何在PyCharm中安裝OpenCV?

    本文將從以下幾個方面詳細介紹如何在PyCharm中安裝OpenCV。 一、安裝Python 在安裝OpenCV之前,請確保已經安裝了Python。 如果您還沒有安裝Python,可…

    編程 2025-04-29
  • Python字元串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字元串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字元串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智慧、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29
  • Python編程二級證書考試相關現已可以上網購買

    計算機二級Python考試是一項重要的國家級認證考試,也是Python編程的入門考試。與其他考試一樣,Python編程二級證書的考生需要進入正式考試,而為了備考,這篇文章將詳細介紹…

    編程 2025-04-29
  • Python清華鏡像下載

    Python清華鏡像是一個高質量的Python開發資源鏡像站,提供了Python及其相關的開發工具、框架和文檔的下載服務。本文將從以下幾個方面對Python清華鏡像下載進行詳細的闡…

    編程 2025-04-29

發表回復

登錄後才能評論