Ubuntu14.04下安装flask(Python虚拟环境管理与切换)

  • 首先是在Ubuntu下安装多版本的Python。
    首先我的配置是Ubuntu14.04 32位下默认的是Python2.7和Python3.4,我想安装一个Python3.6。这里是一个安装教程,跟着一步步走没问题:
    http://yanghuangblog.com/index.php/archives/9/
    注意最后安装成功后也更新一下sudo apt-get update

  • 然后把当前Python3版本默认更新为3.6, 因为3.6之下已经不怎么用了。


cd /usr/bin

# 查看 python3 是否存在
dorom@ubuntu:/usr/bin$ ls |grep python3

dh_python3
python3
python3.4
python3.4m
python3.6
python3.6m
python3m


# 如果存在  
sudo rm python3

#新建python3 的软连接
sudo ln - s  python3.6m python3

# 检查python3的版本
xxcc@ubuntu:/usr/bin$ python3 --version
Python 3.6.4 

# 更新Python3的pip
sudo apt-get install python3-pip

#最后更新整个包
 sudo apt-get update

  • 注意因为Python3.6对venv会有奇怪的错误,所以不使用通常的办法进行安装。我们采用pyenv进行安装。
    (PS:我到这里才发现pyenv需要重新下载Python3.6,之前的无法识别。)
    跟着这个教程来:https://askubuntu.com/questions/865554/how-do-i-install-python-3-6-using-apt-get
    其中第一步是先安装git,第二步是通过git自动安装,这里会遇到问题:ModuleNotFoundError: No module named '_gdbm'
    在这里插入图片描述
    此时需要先安装gdm:sudo apt-get install python3.6-gdbm

    其中如果不想每次都要手动操作一遍的话,记得把临时配置环境变量的语句写入bashrc。这样我们将一直处于这个虚拟环境的Python下。
    vim ~/.bashrc然后source ~/.bashrc让环境立刻生效。
    在这里插入图片描述
    这样如果需要更改虚拟环境参考这个:
    http://einverne.github.io/post/2017/04/pyenv.html
    顺便补充一下官方对想读入已经在系统中存在的Python的解决办法:

For example, to set your path to first use yoursystemPython and Python3 (set to 2.7.9 and 3.4.2 in this example), but also have Python 3.3.6, 3.2, and 2.5 available on yourPATH, one would firstpyenv installthe missing versions, then setpyenv global system 3.3.6 3.2 2.5. At this point, one should be able to find the full executable path to each of these usingpyenv which, e.g.pyenv which python2.5(should display$(pyenv root)/versions/2.5/bin/python2.5), orpyenv which python3.4(should display path to system Python3).

发布了38 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_41337100/article/details/96973962