您的位置:

如何在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环境变量中的细节和过程。使用虚拟环境和包管理程序,可以更加轻松地管理项目和依赖项。