您当前所在位置:首页安装教程Linux下Python3安装&更改镜像源&虚拟环境管理

Linux下Python3安装&更改镜像源&虚拟环境管理

更新:2023-11-18 22:05:34编辑:tooool归类:安装教程人气:90

python是一门跨平台、面向对象的解释型语言,也是一种脚本语言。不仅可以运行在Windows系统,也可以运行在Linux系统下,本文主要内容:

  • Linux系统如何安装python
  • 创建、激活python虚拟环境
  • python指定临时镜像源&永久更换镜像源

文末彩蛋:shell一键安装python脚本~

1、安装依赖环境

yum-y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

2、下载Python3

yum -y install wget没安装wget工具时运行此命令wgethttps://www.python.org/ftp/python/3.7.5/Python-3.7.5.tgz

3、解压

tar-xzvfPython-3.7.5.tgz

4、编译安装

yuminstallgcc -y安装gcc编译器yuminstalllibffi-devel -y3.7版本之后需要一个新的包libffi-develcd Python-3.7.5进入解压后的文件夹./configure--prefix=/home/python  配置指定安装目录make && makeinstall编译&编译安装

5、配置python环境变量

export PYTHON_HOME=/home/python

export PATH=${PATH}:${PYTHON_HOME}/bin

vi /etc/profile在/etc/profile文件中加入上面两行内容source/etc/profile重新加载配置

6、安装python虚拟环境

在日常项目开发过程中,由于每个项目所用到的依赖包版本可能不一致,尤其是一些老项目。因此,为了避免各个项目之间环境相互污染,我们可以针对各个项目分别建立各自的虚拟环境。

python3 -m venv python_venv新建名为python_venv的虚拟环境目录cdpython_venv/bin进入虚拟环境所在文件夹的bin目录sourceactivate激活虚拟环境;Linux&Mac系统与Windows激活命令不一样,要用source命令

激活成功后,命令行最前面会显示一个:(虚拟环境名称)

7、配置pip国内安装镜像源

1)临时指定安装镜像源

pip install -ihttps://pypi.tuna.tsinghua.edu.cn/simple(镜像地址)module(模块名称)

国内镜像源:

清华:https://pypi.tuna.tsinghua.edu.cn/simple

阿里云:http://mirrors.aliyun.com/pypi/simple/

中国科技大学https://pypi.mirrors.ustc.edu.cn/simple/

华中理工大学:http://pypi.hustunique.com/

山东理工大学:http://pypi.sdutlinux.org/

豆瓣:http://pypi.douban.com/simple/

2)永久更改安装镜像源

如果不想每次安装第三方包的时候都指定镜像源,也可以使用如下方法永久指定安装镜像源,一劳永逸

pip configsetglobal.index-urlhttps://mirrors.aliyun.com/pypi/simple永久配置国内镜像源pip configlist查看下载源配置列表

配置好镜像源后,可以使用pip config list命令查看下载源配置列表,具体写入的配置文件路径为:/root/.config/pip/pip.conf

windows系统设置和查看方法相同

Linux下Python3安装&更改镜像源&虚拟环境管理

8.批量安装Python项目依赖包

pipfreeze > requirements.txt导出当前环境下已安装的依赖包列表到指定文件pip download -r requirements.txt -d .下载指定文件中的所有依赖包

通过shell脚本一键安装

以安装python3.7.5为例:

!/bin/shyum -y install wget 
yum -y install gcc 
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel 
yum -y install libffi-develif[ ! -f"Python-3.7.5.tgz"]thenechoPython-3.7.5.tgz not exist!!! 
       wget https://www.python.org/ftp/python/3.7.5/Python-3.7.5.tgzfi`tar -zxvf Python-3.7.5.tgz`if[ ! -d /home/python3 ]then`mkdir -p /home/python3`cdPython-3.7.5 
./configure --prefix=/home/python3 
make && make installfiecho"export PYTHON_HOME=/home/python3">>/etc/profileecho"export PATH=\${PATH}:\${PYTHON_HOME}\"/bin\"">>/etc/profileecho"=====`date`===== Python-3.7.5 install success !!!====="

我告诉你msdn版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

小编来告诉你没有系统可以进安全模式吗 win7玩暗黑破坏神2花屏怎么办|win7玩暗黑破坏神2花屏解决方法

游客 回复需填写必要信息