如何在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/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

发表回复

登录后才能评论