一、下載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-hant/n/219735.html