WSL Ubuntu18.04 搭建python3开发环境

分类:Linux

win10系统下进行Python相关开发工作时, 常常会遇到一些诸如包安装失败之类的问题, 所幸通过win10专业版的WSL(Windows Subsystem for Linux)功能, 可以将Ubuntu或其他Linux发行版作为子系统运行, 通过bash或X-Window等方式访问Linux子系统, 为开发带来了不小的便利. 通过WSL可以搭建Python数据科学或爬虫开发环境都十分方便, 以下是环境搭建搭建过程记录.

修改apt源为阿里云源

# 国内访问官方apt源速度不佳, 所以将其换成阿里云源.
# 备份/etc/apt/sources.list
$ sudo cp /etc/apt/sources.list /etc/apt/sources.list.bakcat /etc/apt/sources.list.bak >>  /etc/apt/sources.list
$ sudo vim /etc/apt/sources.list
# 修改sources.list中的内容为:
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

安装zsh

$ sudo apt-get install zsh

安装 oh-my-zsh

# 为 zsh 增加语法高亮等功能, 使其美观易用
$ sudo sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"

安装pip3

# Python包管理工具
$ sudo apt-get -y install python3-pip

为指定Python3安装pip

$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3.7 get-pip.py --user
# 路径添加到path
$ python3.7 -m site --user-base
$ python3 -m site --user-base
/home/oratun/
$ sudo vim ~/.bashrc
# 在末尾加上 
PATH="$PATH:/home/ora/.local/bin"

解决pipenv lock速度慢

pypi官方源在国外, 国内连通性低, 会导致pipenv install [package] 后lock时速度很慢甚至不成 功, 解决办法同样是替换为国内的阿里云源

$ vim ~/.pip/Pipfile
# 修改url
url = "https://mirrors.aliyun.com/pypi/simple"

安装pyspider安装依赖pycurl出现问题

问题: 
    Could not run curl-config: [Errno 2] No such file or directory
解决方法:
$ sudo apt-get install libcurl4-gnutls-dev

问题: 
    error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
解决方法:
$ sudo apt-get install libghc-gnutls-dev

安装phantomjs

$ sudo apt-get install phantomjs

问题: pyspider Could not connect to any X display.
解决方法:
$ vim .profile 
# 末尾加上
export QT_QPA_PLATFORM=offscreen

安装 mongodb 数据库

$ curl -LO https://www.mongodb.org/static/pgp/server-4.0.asc
$ sudo apt-key add server-4.0.ascecho "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
$ sudo apt-get update
$ sudo apt-get install -y mongodb

# 启动mogodb
$ sudo service mongodb start
# 如果使用mongodb官网上的命令sudo apt-get install -y mongodb-org进行安装将无法使用以上命令启动服务

使用软连接将python指向python3

$ sudo ln -s /usr/bin/python3 /usr/bin/python
$ python -V
python3.6.7