ubuntu安装python开发环境

发布时间 2023-04-14 15:40:00作者: PKGAME

一般ubuntu是自带python的,不需要安装。这里主要讲,更新pip,设置镜像源,配置虚拟环境,为后续开发做准备。

一、更新pip

  pip3 config list

  如果报没有 config 命令,说明pip版本不够高。

  apt install python3-pip

  这里可以重新安装pip, 一般不需要。

  pip3 install -U pip

  这个是使用pip3 将pip3 更新到最新版本。

  这时候list就不会报错了,如果没有数据就可以开始设置数据了

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

  这个是设置清华大学镜像源

  其他源:

  阿里

  http://mirrors.aliyun.com/pypi/simple/

  清华大学

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

  中国科技大学

  https://pypi.mirrors.ustc.edu.cn/simple/

二、配置虚拟环境

  sudo pip3 install virtualenv

  给所有用户安装virtualenv

  sudo pip3 install virtualenvwrapper

  给所有用户安装虚拟环境扩展包,方便操作

  测试安装成功没有

  mkvirtualenv test

  如果提示找不到mkvirtualenv命令,须配置环境变量

  1、创建目录用来存放虚拟环境
    mkdir $HOME/.virtualenvs
 
  2、打开~/.bashrc文件
    vim ~/.bashrc
 
  2.1 添加如下两行
    export WORKON_HOME=$HOME/.virtualenvs     source /usr/local/bin/virtualenvwrapper.sh   3、运行     source ~/.bashrc
  
  如果报这个错  

If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON= and that PATH is
set properly.

  2.1前面 增加一个  

    export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3

  然后可以了。

  相关命令:

  创建名字为 test 的虚拟环境

  mkvirtualenv test

  查看虚拟环境列表

  lsvirtualenv

  workon

  启用虚拟名字为test环境

  workon  test

  退出虚拟环境

  deactivate

  删除名字为test的虚拟环境

  rmvirtualenv test

  进入当前环境目录

  cdvirtualenv