ubuntu普通用戶編譯安裝Python3教程


一、背景

眾所周知,root用戶在linux系統中擁有至高無上的權力,為所欲為,想干嘛就干嘛。所以當然不能隨隨便便給人家用root賬戶去搞事情啊,這里就有了用普通用戶安裝使用python的想法,一起來看看吧。

二、前期准備

更新下源,並安裝相應的更新

sudo apt-get install upgrade && apt-get install update

安裝相關的編譯輔助工具

sudo apt-get install make build-essential zlib1g-dev  -y

-y表示不用你確認了,告訴ubuntu你直接給我裝完就行

源碼包下載

認准這個網站哦,https://www.python.org/downloads/source/, 當然你再細心觀察下,直接訪問這里不是更快嗎?https://www.python.org/ftp/python/

三、安裝(以3.8.1為例)

下載相應的Python源碼包,這里我們用wget一步到位

wget https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tgz

對下載下來的Python源碼包進行解壓

tar -zxvf Python-3.8.1.tgz

這里為了便於管理,我們在當前用戶家目錄下新建software表示我們要裝的軟件,一步到位我們就定義好安裝python的目標文件夾了

mkdir -p /home/ataola/software/python3.8

進入到解壓后的源碼目錄進行編譯安裝

cd Python-3.8.1
./configure --prefix="/home/ataola/software/python3.8"
make && make install 

追加到當前用戶環境變量

# 打開文件
vim ~/.bashrc
# 視具體情況粘貼樓下這句話
export PATH=PATH/software/python3.8/bin:$PATH
# 保存退出 按RSC, 然后輸入:wq
# 更新使其生效
source ~/.bashrc

驗證一下吧

ataola@ataola-ubuntu:~$ python3 -V
Python 3.8.1
ataola@ataola-ubuntu:~$ pip3 -V
pip 19.2.3 from /home/ataola/software/python3.8/lib/python3.8/site-packages/pip (python 3.8)
ataola@ataola-ubuntu:~$ python -V
Python 3.8.1
ataola@ataola-ubuntu:~$ pip -V

Command 'pip' not found, but can be installed with:

apt install python3-pip
Please ask your administrator.

ataola@ataola-ubuntu:~$

這里你會發現每次都要輸入pip3,輸入pip沒用。你只需在環境變量加個別名就好

# 打開文件
vim ~/.bashrc

# 貼下面的話,視個人配置
alias pip=/home/ataola/software/python3.8/bin/pip3.8

# 保存退出 按RSC, 然后輸入:wq

# 更新使其生效
source ~/.bashrc

這樣不就好了嘛

ataola@ataola-ubuntu:~$ python -V
Python 3.8.1
ataola@ataola-ubuntu:~$ python3 -V
Python 3.8.1
ataola@ataola-ubuntu:~$ pip -V
pip 19.2.3 from /home/ataola/software/python3.8/lib/python3.8/site-packages/pip (python 3.8)
ataola@ataola-ubuntu:~$ pip3 -V
pip 19.2.3 from /home/ataola/software/python3.8/lib/python3.8/site-packages/pip (python 3.8)
ataola@ataola-ubuntu:~$

不高興按樓上一步一步來,我這里貼了一份.bashrc,你直接看着改吧

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# don't put duplicate lines in the history. See bash(1) for more options
# ... or force ignoredups and ignorespace
HISTCONTROL=ignoredups:ignorespace

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

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

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

# 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-color, unless we know we "want" color)
case "$TERM" in
    xterm-color) 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

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

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

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
#if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
#    . /etc/bash_completion
#fi

搞完以后,這是國內環境啊,首先想到的是換源啊,不然每次都等個半死,時間都浪費了,阿里、豆瓣、清華源隨便選一個吧。

# 升級pip
pip install --upgrade pip

# 如果沒有.pip文件創建,有就略過
mkdir ~/.pip

# 編輯配置文件
vim ~/.pip/pip.conf

粘貼如下內容

[global]
index-url=http://mirrors.aliyun.com/pypi/simple
[install]
trusted-host=mirrors.aliyun.com

注意這里是走http的,因為最開始編譯的時候沒有選--with-ssl, 當然你完全可以重新編譯一次帶上這個ssl./configure --prefix="/home/ataola/software/python3.8" --enable-optimizations --with-ssl, 后面就可以用https了

試一下安裝速度,起飛

ataola@ataola-ubuntu:~$ pip install wordcloud
Looking in indexes: http://mirrors.aliyun.com/pypi/simple
Collecting wordcloud
  Downloading http://mirrors.aliyun.com/pypi/packages/50/5b/f588bbd1a7805a742e8d8316740383822b160c4e36b6c14793b57ebe7360/wordcloud-1.8.1-cp38-cp38-manylinux1_x86_64.whl (371 kB)
     |████████████████████████████████| 371 kB 3.2 MB/s
Collecting matplotlib
  Downloading http://mirrors.aliyun.com/pypi/packages/22/1c/d5e535b36c1de4eef4205656e76ac993c6d01b62cfdcac579edb63cd82e0/matplotlib-3.5.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl (11.3 MB)
     |████████████████████████████████| 11.3 MB 129 kB/s
Requirement already satisfied: numpy>=1.6.1 in ./software/python3.8/lib/python3.8/site-packages (from wordcloud) (1.21.4)
Collecting pillow
  Downloading http://mirrors.aliyun.com/pypi/packages/fe/f9/cd8b11ec15e27581c5e7affdf04d618d44fa9524dbeb429e5e728df6dc4c/Pillow-8.4.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.1 MB)
     |████████████████████████████████| 3.1 MB 47.9 MB/s
Requirement already satisfied: python-dateutil>=2.7 in ./software/python3.8/lib/python3.8/site-packages (from matplotlib->wordcloud) (2.8.2)
Collecting packaging>=20.0
  Downloading http://mirrors.aliyun.com/pypi/packages/05/8e/8de486cbd03baba4deef4142bd643a3e7bbe954a784dc1bb17142572d127/packaging-21.3-py3-none-any.whl (40 kB)
     |████████████████████████████████| 40 kB 8.1 MB/s
Collecting cycler>=0.10
  Downloading http://mirrors.aliyun.com/pypi/packages/5c/f9/695d6bedebd747e5eb0fe8fad57b72fdf25411273a39791cde838d5a8f51/cycler-0.11.0-py3-none-any.whl (6.4 kB)
Collecting fonttools>=4.22.0
  Downloading http://mirrors.aliyun.com/pypi/packages/7d/05/5c446faf632f1a9c386bd9a56555cbcbe6c71e6b523025a4fbde396e9d39/fonttools-4.28.3-py3-none-any.whl (884 kB)
     |████████████████████████████████| 884 kB 50.6 MB/s
Collecting kiwisolver>=1.0.1
  Downloading http://mirrors.aliyun.com/pypi/packages/d0/2c/c3cba6c1ec54c82bfc56204c712dd2e9b069e2590f78a18841dafbdf2ced/kiwisolver-1.3.2-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl (1.2 MB)
     |████████████████████████████████| 1.2 MB 47.2 MB/s
Collecting pyparsing>=2.2.1
  Downloading http://mirrors.aliyun.com/pypi/packages/a0/34/895006117f6fce0b4de045c87e154ee4a20c68ec0a4c9a36d900888fb6bc/pyparsing-3.0.6-py3-none-any.whl (97 kB)
     |████████████████████████████████| 97 kB 10.9 MB/s
Requirement already satisfied: six>=1.5 in ./software/python3.8/lib/python3.8/site-packages (from python-dateutil>=2.7->matplotlib->wordcloud) (1.16.0)
Installing collected packages: pyparsing, pillow, packaging, kiwisolver, fonttools, cycler, matplotlib, wordcloud
Successfully installed cycler-0.11.0 fonttools-4.28.3 kiwisolver-1.3.2 matplotlib-3.5.1 packaging-21.3 pillow-8.4.0 pyparsing-3.0.6 wordcloud-1.8.1
ataola@ataola-ubuntu:~$

四、使用

輸入Python,隨便打點什么吧

ataola@ataola-ubuntu:~$ python
Python 3.8.1 (default, Dec 11 2021, 19:38:06)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
>>>

python吧,就是包庫多,有的時候我們可能期望 a和b組合做一個實驗或者開發, 有的時候期望是a和c組合,那么這里推薦你用virtualenv

# 安裝virtualenv
pip install virtualenv

# 創建項目文件並進入
mkdir play-py-a && cd play-py-a

## 創建虛擬環境
python -m venv venv

詳細信息如下:

ataola@ataola-ubuntu:~/play-py-a/venv/bin$ ls
activate  activate.csh  activate.fish  Activate.ps1  easy_install  easy_install-3.8  pip  pip3  pip3.8  python  python3  python3.8
ataola@ataola-ubuntu:~/play-py-a/venv/bin$ pwd
/home/ataola/play-py-a/venv/bin
ataola@ataola-ubuntu:~/play-py-a/venv/bin$

具體的可以看這里,https://virtualenv.pypa.io/en/latest/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM