美文网首页
基于WSL的python环境安装(Ubuntu+python3.

基于WSL的python环境安装(Ubuntu+python3.

作者: 飞跑的蛤蟆 | 来源:发表于2020-11-20 19:36 被阅读0次

系统版本:VERSION="20.04.1 LTS (Focal Fossa)"

python版本:3.6.12

1. 更换国内镜像源

# 备份原来的sources.list文件
sudo mv /etc/apt/sources.list /etc/apt/sources.list.bak
# 写入新的镜像地址(清华镜像源)
sudo vim /etc/apt/sources.list

写入以下内容:

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse

# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse

更新镜像缓存:

sudo apt update

2. 安装oh-my-zsh

# 第一步 (确保你的系统中有 zsh、git、wget、curl)
sudo apt install zsh -y
# 第二步 (使用脚本进行安装,这里会自动修改默认shell)
sh -c "$(wget https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"

安装zsh插件:

# zsh-autosuggestions插件
git clone https://github.com/zsh-users/zsh-autosuggestions.git $ZSH_CUSTOM/plugins/zsh-autosuggestions
# zsh-syntax-highlighting插件
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $ZSH_CUSTOM/plugins/zsh-syntax-highlighting

修改配置文件 vim ~/.zshrc,添加 zsh-autosuggestionszsh-syntax-highlighting

# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(git zsh-autosuggestions zsh-syntax-highlighting)

使配置文件生效:source ~/.zshrc

安装 Powerlevel10k主题:

暂时先不进行安装

# 使用gitee加速安装
git clone --depth=1 https://gitee.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k

使用系统自带的ys主题:

# 将配置文件~/.zshrc修改为
ZSH_THEME="ys"

3. 安装python

# 官方镜像安装
sudo wget https://www.python.org/ftp/python/3.6.12/Python-3.6.12.tgz -O /opt/Python-3.6.12.tgz
# 阿里镜像加速
sudo wget http://npm.taobao.org/mirrors/python/3.6.12/Python-3.6.12.tgz -O /opt/Python-3.6.12.tgz
cd /opt
# 解压
sudo tar xvf Python-3.6.12.tgz
# 
cd Python-3.6.12

编译环境安装:

# GNU 编辑器集合,GNU 调试器,和其他编译软件所必需的开发库和工具
build-essential 

# OpenSSL的开发库(Secure Sockets Layer toolkit )
libssl-dev

# zlib是一个压缩解压缩库(compression library)
zlib1g-dev 

# libbz2是高品质块排序文件压缩程序库(high-quality block-sorting file compressor library)bzip2
libbz2-dev 

# GNU Readline 库提供了一组函数供应用程序使用,这些函数允许用户在键入命令行时编辑命令行。(GNU readline and history libraries)
libreadline-dev 

# LLVM 项目是模块化和可重用的编译器和工具链技术的集合。
llvm 

# GNU Ncurses 是一个编程库,它允许用户编写基于文本的用户界面(TUI)。
libncurses5-dev
# 用于终端处理的共享库(宽字符支持)
libncursesw5-dev 

# Tk 是 Tcl“图形工具箱”的扩展,它提供各种标准的 GUI 接口项,以利于迅速进行高级应用程序开发。(Toolkit for Tcl and X11 (default version) )
tk-dev 

# libffi的作用就相当于编译器,它为多种调用规则提供了一系列高级语言编程接口,然后通过相应接口完成函数调用,底层会根据对应的规则,完成数据准备,生成相应的汇编指令代码。(Foreign Function Interface library)
libffi-dev 

# xz(XZ-format compression library)
liblzma-dev

# xml处理(XSLT 1.0 processing library)
libxslt1-dev

# sqlite3支持库
libsqlite3-dev

# (cross-platform, open-source make system)
cmake

# xml解析 虚包 (XML parsing C library)
libexpat-dev

# (GNU dbm database routines)
libgdbm-dev
sudo apt install build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev llvm libncurses5-dev libncursesw5-dev tk-dev libffi-dev liblzma-dev libxslt1-dev libsqlite3-dev cmake libgdbm-dev -y

预编译python源代码

sudo ./configure --prefix=/opt/python3 --enable-optimizations --with-ensurepip=install --enable-loadable-sqlite-extensions

configure的作用是利用这个configure的脚本来查看你的linux的运行环境。然后生成一个Makefile文件,你就可以通过makefile文件编译你的项目了

编译源代码

sudo make -j `grep processor /proc/cpuinfo  | wc -l`

grep processor /proc/cpuinfo | wc -l得到的是CPU的虚拟核心数(线程数)

make实际就是编译源代码,按照上一步生成makefile文件进行编译,并生成执行文件。

进行安装

sudo make install

make install实际上是把生成的执行文件拷贝到之前configure命令指定的目录/opt/python3下。

添加软链接

sudo ln -s /opt/python3/bin/python3.6 /usr/bin/python
sudo ln -s /opt/python3/bin/pip3 /usr/bin/pip

验证安装

# jesse @ DESKTOP-TI6T145 in ~ [13:36:50]
$ python --version
Python 3.6.12

# jesse @ DESKTOP-TI6T145 in ~ [13:37:00]
$ pip --version
pip 18.1 from /opt/python3/lib/python3.6/site-packages/pip (python 3.6)

# jesse @ DESKTOP-TI6T145 in ~ [13:37:06]
$ python
Python 3.6.12 (default, Nov  7 2020, 05:36:26)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import dbm
>>> from dbm import gnu
>>> import ssl
>>> import lzma
>>> import sqlite3

配置pip镜像源

sudo pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

解决过的问题:

1. 找不到ssl模块(不能使用https地址的镜像)

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.

Could not fetch URL https://pypi.python.org/simple/XXXXXX/: There was a problem confirming the ssl certificate: Can't connect to HTTPS URL because the SSL module is not available. - skipping

安装的库: libssl-dev

验证:import ssl

2. 不能安装bz2格式的第三方库

unsupported archive format: .tar.bz2

安装的库: libbz2-dev

3. 不能使用依赖sqllite3的第三方库

例如:ScrapyDjangolac

ImportError: No module named _sqlite3

安装的库:libsqlite3-dev

新增的预编译配置项:--enable-loadable-sqlite-extensions

4. 编译后提示一些必要的库没有安装(_dbm_gdbm

Python build finished successfully!
The necessary bits to build these optional modules were not found:
_dbm                  _gdbm

安装的库:libgdbm-dev

再次进行预编译以及编译后发现_dbm还是没有,由于不影响使用以及找不到相关的解决方案所以就先不管了。

参考资料

比较全能解决大部分问题

外文可供参考

相关文章

网友评论

      本文标题:基于WSL的python环境安装(Ubuntu+python3.

      本文链接:https://www.haomeiwen.com/subject/oerjiktx.html